画像の追加をサポートする TextKit ベースのエディターがあります。各画像を別々の行に表示したい。
NSTextStorage のサブクラスで、私のコードは次のようになります (TextEdit に感謝します)。
- (void)addImageAssets:(NSArray *)allAssets atRange:(NSRange)range;
{
NSMutableAttributedString *attachments = [NSMutableAttributedString new];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
paragraphStyle.lineSpacing = 20.0f;
for (ALAsset *newAsset in allAssets)
{
UIImage *theImage = [UIImage imageWithCGImage:newAsset.aspectRatioThumbnail];
NSTextAttachment *textAttachment = [NSTextAttachment new];
textAttachment.image = theImage;
NSMutableAttributedString *replacementString = [NSMutableAttributedString new];
[replacementString appendAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]];
[replacementString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [replacementString length])];
[attachments appendAttributedString:replacementString];
}
_isEditing = YES;
[self beginEditing];
[_backingStore replaceCharactersInRange:range
withAttributedString:attachments];
[self edited:NSTextStorageEditedAttributes
range:range changeInLength:allAssets.count];
[super processEditing];
[self endEditing];
_isEditing = NO;
}
(_isEditing は簿記に使用されるブール値のフラグです)
出力は次のようになります出力 http://take.ms/QCvRK
NSMutableParagraphStyle でさまざまなパラメーターを試しましたが、各画像の後に改行できませんでした。
添付テキストの前後に改行 ("\r") を追加すると、グリフ エラーが発生します。
!!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 1
!!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
!!! _NSGlyphTreeInvalidateGlyphsForCharacterRange character count mismatch
!!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 0
!!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 0
!!! _NSLayoutTreeLineFragmentUsedRectForGlyphAtIndex invalid glyph index 2147483647
幅がデバイスの幅に設定されるように、 NSTextAttachmentをサブクラス化してattachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex:をオーバーライドしようとしましたが、これにより添付画像が拡大して表示されました。
各画像の直後に改行を導入する方法について何か提案はありますか? また、画像の添付ファイルの周りにテキストを流すことができれば素晴らしいと思います.
前もって感謝します。