Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
I need to concatenate text with an image then display the result string on a label. I have done this
Code:
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?
 
I need to concatenate text with an image then display the result string on a label. I have done this
Code:
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?

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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.