Col*_*inE 18 objective-c uikit ios ios7 textkit
我正在使用新的Text Kit API为某些属性文本添加附件:
// create an attachment for each image
NSTextAttachment* ta = [NSTextAttachment new];
ta.image = [UIImage imageNamed:@"imageName"];
// add to the attributed text string
NSAttributedString* rep = [NSAttributedString attributedStringWithAttachment:ta];
[myAttributedTextString appendAttributedString:rep];
Run Code Online (Sandbox Code Playgroud)
这工作正常,我可以看到我的图像在输出中呈现.但是,我找不到任何方法来指定图像对齐方式,或者在图像周围包装文本.
有任何想法吗?
注意:文本附件与排除路径不同 - 文本附件是"模型"的一部分,即它是布局管理器执行文本布局的属性文本字符串的一部分.而排除路径是视图的一部分.
Mic*_*ano 20
NSTextAttachments
被视为单个字符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])];
Run Code Online (Sandbox Code Playgroud)
在此之后,您将附加imageAttrString
到现有的属性字符串,并可能在其后添加另一个.一个怪癖是因为附件是一个角色,所以它不被视为自己的段落.为了实现这种情况,您需要将其包围\n
(换行符).只需将这些附加到附件的属性字符串的两侧即可.
希望有所帮助,我花了很长时间才弄明白.
Leo*_*ica 10
尝试将bounds
属性设置为图像大小.
定义文本坐标系中接收器图形表示的布局边界.
所以它应该是:
ta.bounds = (CGRect) { 0, 0, ta.image.size };
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7929 次 |
最近记录: |