0

I wanted to add image in UILabel along with text. However I used NSTextAttachment to add image at the end of UILabel text. I have subclass it to to fit the size of image to my text.

Now I want to add one more image in the beginning of the text, we can say as a prefix of UILabel (Please see the image attached).

I tried it but I do not found any good solution. can anybody please help me to get this done.

enter image description here

Larme
  • 18,203
  • 5
  • 42
  • 69
Parth Pandya
  • 1,422
  • 1
  • 18
  • 30
  • Why are you attaching image inside label? Why don't you make a UIView , and add label and UIImagView (with image inside it)? – Munahil Aug 07 '15 at 07:59
  • I have tried it already but it sometimes it does not image in proper position and also using that way, everytime I have to calculate the last character position of text in label. – Parth Pandya Aug 07 '15 at 08:04
  • What have you tried? What's your current code for inserting a `NSTextAttachment` at the end? It shouldn't be more complicated to add it at the beginning, or wherever you want in fact. – Larme Aug 07 '15 at 08:16
  • You can only add one image to your "NSTextAttachment". You can create a "UIImageView", and add it before your "UILabel" – Munahil Aug 07 '15 at 08:20

3 Answers3

2

Attachment it is character, you can present it as AttributedString and use it for replace some character in the string. You can add space as first character and use replaceCharactersInRange: method to replace the character to attachment:

<...>
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@" with attachments " attributes:[self attributesWithAttachment:nil]];

[attrString replaceCharactersInRange:NSMakeRange(0, 1) withAttributedString:[self attachmentWithImage:[UIImage imageNamed:@"1.png"]]];
[attrString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:[self attachmentWithImage:[UIImage imageNamed:@"2.png"]]];
[attrString replaceCharactersInRange:NSMakeRange([attrString length] - 1, 0) withAttributedString:[self attachmentWithImage:[UIImage imageNamed:@"3.png"]]];

self.label.attributedText = attrString;
<...>

- (NSAttributedString*)attachmentWithImage:(UIImage*)attachment
{
NSTextAttachment* textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
textAttachment.image = attachment;

NSAttributedString* string = [NSAttributedString attributedStringWithAttachment:textAttachment];

return string;
}

Result:

enter image description here

Andrew Romanov
  • 4,058
  • 2
  • 21
  • 36
  • can we resize the image when replacing it to character or will it keep image original size? – Parth Pandya Aug 07 '15 at 13:46
  • 1
    Anyways I managed the size with subclassing NSTextAttachement and overriding the "attachmentBoundsForTextContainer" method, thanks for the solution. – Parth Pandya Aug 07 '15 at 13:50
0

try somethik like this:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
UIImageView *imgViewPrefix = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"feedbackLocItemActiveIco"]];
imgViewPrefix.frame = CGRectMake(0, 0, 20, 20);

UIImageView *imgViewPostfix = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"feedbackLocItemActiveIco"]];
imgViewPostfix.frame = CGRectMake(80, 0, 20, 20);

label.text = @"    my text";
label.textColor = [UIColor whiteColor];
[label addSubview:imgViewPrefix];
[label addSubview:imgViewPostfix];
Jiri Zachar
  • 459
  • 3
  • 7
0

I see you are using a UITableView. For the prefix ImageView, why not use the property of the UITableViewCell, imageView?

That will simplify your solution hopefully.

Reference

Nikita P
  • 4,066
  • 5
  • 27
  • 53