iosobjective-ccocoa-touchios7textkit

How to embed small icon in UILabel


I need to embed small icons ( sort of custom bullets ) to my UILabel in iOS7. How can I do this in interface designer? Or at least in code?

In Android there are leftDrawable and rightDrawable for labels, but how it is done in iOS? Sample in android :

android sample


Solution

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