如何使用iOS7文本工具包围绕附件包装文本?

时间:2022-04-13 08:51:53

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', 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.

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

3 个解决方案

#1


20  

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.

NSTextAttachments被NSAttributedString视为单个字符。因此,为了调整它们的对齐方式,您必须像对文本那样做。我花了好几个小时摆弄附件。边界(我从来都无法正确地工作)来最终解决这个问题。下面是一个如何水平对齐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. 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.

在此之后,您将把imageAttrString附加到一个现有的带属性字符串,或者在它之后再附加一个。一个奇怪的地方是,因为附件是一个字符,所以它没有被当作自己的段落来处理。为了实现这一点,您需要用\n(换行字符)包围它。只需将这些附加到附件的带属性字符串的两边。

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

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

#2


10  

Try setting the bounds property to the image size.

尝试将边界属性设置为图像大小。

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 };

#3


1  

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

change yPadding you need.
It can be negative when image's height is large than line height.

你需要改变yPadding。当图像的高度大于行高时,它可以为负。

#1


20  

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.

NSTextAttachments被NSAttributedString视为单个字符。因此,为了调整它们的对齐方式,您必须像对文本那样做。我花了好几个小时摆弄附件。边界(我从来都无法正确地工作)来最终解决这个问题。下面是一个如何水平对齐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. 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.

在此之后,您将把imageAttrString附加到一个现有的带属性字符串,或者在它之后再附加一个。一个奇怪的地方是,因为附件是一个字符,所以它没有被当作自己的段落来处理。为了实现这一点,您需要用\n(换行字符)包围它。只需将这些附加到附件的带属性字符串的两边。

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

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

#2


10  

Try setting the bounds property to the image size.

尝试将边界属性设置为图像大小。

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 };

#3


1  

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

change yPadding you need.
It can be negative when image's height is large than line height.

你需要改变yPadding。当图像的高度大于行高时,它可以为负。