如何使用iOS7 Text Kit在附件中包装文本? [英] How to wrap text around attachments using iOS7 Text Kit?

查看:83
本文介绍了如何使用iOS7 Text Kit在附件中包装文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的Text Kit API为某些属性文本添加附件:

I am using the new Text Kit API to add attachments to some attributed text:

// 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.

任何想法?

注意:文本附件与排除路径不同 - 文本附件是模型的一部分,即它是布局管理器执行文本布局的属性文本字符串的一部分。而排除路径是视图的一部分。

NOTE: Text attachments are different from exclusions paths - a text attachment is part of the 'model', i.e. 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 被<$ c $视为单个字符C> NSAttributedString 。因此,为了调整它们的对齐方式,您必须像处理文本一样。花了我几个小时摆弄 attachment.bounds (我永远无法正常工作)才能最终解决这个问题。以下是如何水平对齐 NSTextAttachment 的示例。

NSTextAttachments are treated as a single character by 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. Here's an example of how to horizontally align an 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 附加到一个现有的属性字符串,也许在它之后追加另一个。一个怪癖是因为附件是一个角色,所以它不被视为自己的段落。为了实现这种情况,您需要使用 \ n (换行符)来包围它。只需将这些附加到附件的属性字符串的两边。

After this, you would append imageAttrString to an existing attributed string and perhaps append another after it. 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). Just append these to both sides of the attachment's attributed string.

希望有所帮助,我花了很长时间才弄明白。

Hope that helps, it took me ages to figure out.

这篇关于如何使用iOS7 Text Kit在附件中包装文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆