iOS 7 TextKit - How to insert images inline with text? iOS 7 TextKit - How to insert images inline with text? ios ios

iOS 7 TextKit - How to insert images inline with text?


You'll need to use an attributed string and add the image as instance of NSTextAttachment:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"like after"];NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];textAttachment.image = [UIImage imageNamed:@"whatever.png"];NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];


@bilobatum's code converted to Swift for those in need:

let attributedString = NSMutableAttributedString(string: "like after")let textAttachment = NSTextAttachment()textAttachment.image = UIImage(named: "whatever.png")let attrStringWithImage = NSAttributedString(attachment: textAttachment)attributedString.replaceCharacters(in: NSMakeRange(4, 1), with: attrStringWithImage)


You could try using NSAttributedString and NSTextAttachment. Take a look at the following link for more details on customising the NSTextAttachment in order to resize the image.http://ossh.com.au/design-and-technology/software-development/implementing-rich-text-with-images-on-os-x-and-ios/

In my example I resize the image to fit the width, in your case you may want to resize the image to match the line height.