Get the width of the last line of a multiline UILabel

I have a dynamic multi-sheeted UILabel and you need to know the end of the text (X Coordinate) of the visible text (and not the label) so that I can show something after the text. Is it possible?

Thank you

+6
source share
2 answers

You can have more control over a text layout with a CoreText framework. To issue documentation :

There are also some nice open source stuff that already does a lot of hard work for you, for example: https://github.com/Cocoanetics/DTCoreText

+1
source

As far as I know, the best solution for this is to use UIWebView instead of UILabel. You just need to format the HTML to load it, and then add whatever you want to add after.

Example:

[webView loadHTMLString:[NSString stringWithFormat:@"<html><body><font face=\"arial\" size=\"2\">%@</font><font face=\"arial\" size=\"1\"> %@</font></body></html>",text1 , text2] baseURL:nil]; 

If you want to continue using UILabel / UITextView / any UIView, I only know a way to determine the height correctly: [myView sizeToFit]; And then get it.

Hope this helps.

Hi,

George

+1
source

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


All Articles