简体   繁体   English

如何使用iOS7 Text Kit在附件中包装文本?

[英]How to wrap text around attachments using iOS7 Text Kit?

I am using the new Text Kit API to add attachments to some attributed text: 我正在使用新的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];

This works fine, I can see my image rendered in the output. 这工作正常,我可以看到我的图像在输出中呈现。 However, I cannot find any way to specify the image alignment, or wrap text around the image. 但是,我找不到任何方法来指定图像对齐方式,或者在图像周围包装文本。

Any ideas? 有任何想法吗?

NOTE: Text attachments are different from exclusions paths - a text attachment is part of the 'model', ie it is part of the attributed text string that the layout manager performs text layout on. 注意:文本附件与排除路径不同 - 文本附件是“模型”的一部分,即它是布局管理器执行文本布局的属性文本字符串的一部分。 Whereas an exclusion path is part of the view. 而排除路径是视图的一部分。

NSTextAttachments are treated as a single character by NSAttributedString . NSTextAttachments被视为由单个字符NSAttributedString So, in order to adjust their alignment you must do so as you would for text. 因此,为了调整它们的对齐方式,您必须像处理文本一样。 It took me hours of fiddling with attachment.bounds (which I never could get to work properly) to finally figure this out. 我花了几个小时摆弄attachment.bounds (我永远无法正常工作)才能最终解决这个问题。 Here's an example of how to horizontally align an NSTextAttachment . 这是一个如何水平对齐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])];

After this, you would append imageAttrString to an existing attributed string and perhaps append another after it. 在此之后,您将imageAttrString附加到现有的属性字符串,并可能在其后附加另一个。 One quirk is that because the attachment is a character it is not treated as its own paragraph. 一个怪癖是因为附件是一个角色,所以它不被视为自己的段落。 In order for that to be the case you will need to surround it with \\n (newline characters). 为了实现这种情况,您需要使用\\n (换行符)将其包围。 Just append these to both sides of the attachment's attributed string. 只需将这些附加到附件的属性字符串的两侧即可。

Hope that helps, it took me ages to figure out. 希望有所帮助,我花了很长时间才弄明白。

Try setting the bounds property to the image size. 尝试将bounds属性设置为图像大小。

Defines the layout bounds of the receiver's graphical representation in the text coordinate system. 定义文本坐标系中接收器图形表示的布局边界。

So it should be: 所以它应该是:

ta.bounds = (CGRect) { 0, 0, ta.image.size };
ta.bounds = (CGRect) { 0, yPadding, ta.image.size }; 

change yPadding you need. 改变你需要的yPadding。
It can be negative when image's height is large than line height. 当图像的高度大于行高时,它可以是负数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM