NSTextAttachments
では 1 文字として扱われNSAttributedString
ます。そのため、配置を調整するには、テキストの場合と同様に行う必要があります。attachment.bounds
最終的にこれを理解するのに、何時間もいじりました(適切に動作することはありませんでした)。を水平方向に整列する方法の例を次に示しますNSTextAttachment
。
#def BETWEEN_SECTION_SPACING 10
// creates a text attachment with an image
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"sample_image.jpg"];
NSMutableAttributedString *imageAttrString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
// sets the paragraph styling of the text attachment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter]; // centers image horizontally
[paragraphStyle setParagraphSpacing:BETWEEN_SECTION_SPACING]; // adds some padding between the image and the following section
[imageAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [imageAttrString length])];
この後imageAttrString
、既存の属性付き文字列に追加し、おそらくその後に別の文字列を追加します。1 つの癖は、添付ファイルが文字であるため、それ自体の段落として扱われないことです。\n
そのためには、 (改行文字)で囲む必要があります。これらを添付ファイルの属性付き文字列の両側に追加するだけです。
それが役立つことを願っています。理解するのに何年もかかりました。