How to embed small icon in UILabel

You can do this with iOS 7’s text attachments, which are part of TextKit. Some sample code: NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@”MyIcon.png”]; NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@”My label text”]; [myString appendAttributedString:attachmentString]; myLabel.attributedText = myString;

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

How do I locate the CGRect for a substring of text in a UILabel?

Following Joshua’s answer in code, I came up with the following which seems to work well: – (CGRect)boundingRectForCharacterRange:(NSRange)range { NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[self attributedText]]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [textStorage addLayoutManager:layoutManager]; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self bounds].size]; textContainer.lineFragmentPadding = 0; [layoutManager addTextContainer:textContainer]; NSRange glyphRange; // Convert the range for glyphs. [layoutManager … Read more

Detecting taps on attributed text in a UITextView in iOS

I just wanted to help others a little more. Following on from Shmidt’s response it’s possible to do exactly as I had asked in my original question. 1) Create an attributed string with custom attributes applied to the clickable words. eg. NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@”a clickable word” attributes:@{ @”myCustomTag” : @(YES) }]; [paragraph … Read more