UIImage at the end of the text UILabel

how to find the coordinate of the last character in a UILabel, if it has more than 1 line of text? I would like to add an image at the end of the text.

enter image description here

+4
source share
4 answers

Not really, but what you can do is figure out how tall your label is for placing your text with -[NSString sizeWithFont:constrainedToSize:lineBreakMode:], and as soon as you have the height, you can work out there, knowing the right edge of the label and the height, how to position your image as a spy type of container.

.., , , x ( x + width), y, y + , , , , , ... , .

, .

0

, .

.

-(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;
}

...

0

sizeWithFont UILabel.

0
source

I think you are looking 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
0
source

Source: https://habr.com/ru/post/1762415/


All Articles