4

how to find coordinate of the last character in UILabel if we have more then 1 line of text in it? I would like to add an image in the end of the text.

enter image description here

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
victor
  • 53
  • 3
  • I think you're better of using a webview. – Nick Aug 31 '10 at 07:59
  • Solved. wholeSize - sizeWithBounding = X; Thanks all! – victor Aug 31 '10 at 11:42
  • Hey @victor, how exactly you are calculating the end point ? I've similar kind of issue to place another view just after the last character of the multiline text. I was searching and trying with CGContextGetTextPosition but I'm not able to make it to work. Could you please reply ? – Tushar Oct 28 '12 at 10:56
  • http://i.stack.imgur.com/1fKjy.png That's what I mean. – victor Aug 31 '10 at 08:49
  • 1
    Same problem. How did you solved it? What "sizeWithBounding" exactly is? – Gusev Andrey Apr 28 '15 at 14:26

4 Answers4

1

I think you are looking for NSTextAttachment

// create an NSMutableAttributedString
let fullString = NSMutableAttributedString(string: "Your text")

// create our NSTextAttachment
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "icon")

// wrap the attachment in its own attributed string so we can append it
let imageString = NSAttributedString(attachment: imageAttachment)

// add the NSTextAttachment wrapper to our full string, then add some more text.
fullString.append(imageString)

// draw the result in a label
yourLabel.attributedText = fullString
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
0

Not exactly, but what you can do is find out how high your label will have to be to accommodate your text using -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] and once you have the height, you can work out from there, knowing the right edge of the label, and the height, how to position your image as a subview of the container view.

I.e., you may want to add it immediately to the right of the label at the bottom of the label, in which case, add it as a subview where its x axis is the right edge of the label (label's x axis + width), and where the imageview has its y axis set to the y axis of the label + the label's height, minus the size of your font should put it in the right spot, however, you may want to instead of using the label's font height property in the last calculation, to use the height of the imageview instead so it is flush with the bottom of the label and the bottom of the image view... hard to say really without seeing a mockup.

That should give you enough to go on anyway.

jer
  • 20,094
  • 5
  • 45
  • 69
0

You can use this code to get the height of your text as per the width and content.

Try this code and inser the image at the given height.

-(float)getHeightByWidth:(NSString*)myString:(UIFont*)mySize:(int)myWidth

{

    CGSize boundingSize = CGSizeMake(myWidth, CGFLOAT_MAX);
    CGSize requiredSize = [myString sizeWithFont:mySize constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];  
    return requiredSize.height;
}

hAPPY cODING...

Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
0

sizeWithFont does not take care of the UILabel edges.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
Mayosse
  • 696
  • 1
  • 7
  • 16