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.
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.
Try setting the
bounds
property to the image size.So it should be:
NSTextAttachments
are treated as a single character byNSAttributedString
. So, in order to adjust their alignment you must do so as you would for text. It took me hours of fiddling withattachment.bounds
(which I never could get to work properly) to finally figure this out. Here's an example of how to horizontally align anNSTextAttachment
.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.
change yPadding you need.
It can be negative when image's height is large than line height.