UILabel with image

Discussion in 'iOS Programming' started by erdinc27, Dec 26, 2017.

  1. erdinc27 macrumors regular

    Jul 20, 2011
    I need to concatenate text with an image then display the result string on a label. I have done this
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        attachment.image = [UIImage imageNamed:@"1"];
        NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
        NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"My label text"];
        [myString appendAttributedString:attachmentString];
        textLabel.attributedText = myString;
    But at the end label displays like "My label text..."
    How can i display that image with the text on UILabel?
  2. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    Cool subject!

    I found a blog that suggests you need to set the bounds of the attachment. See Asynchronous NSTextAttachments at cocoanetics.com.

    Also, consider increasing the width and height of your label.
  3. bjet767 macrumors 6502a

    Oct 2, 2010
    You code is right off of Stack Overflow, so what I have to assume is your image has not been added to the “bundle” for the code to find.

    XCode often does not add an image file to the bundle when you drag and drop it to the file list, even when it’s listed in the left handed column where are your files and resources are. Also give the file extension (like .png) when you use “imageNamed” that might help.

Share This Page