Get physical text layout in UILabel

I know how to find the size of text using sizeWithFont, but what I really want to do is find the actual highest point and lowest point of the text that was drawn in UILabel.

I need to add an artificial cursor to the view from UILabelinside it. The problem is that the text in UILabel, apparently, changes its upper and lower margins arbitrarily. I can’t understand how the OS works, where to place the text in UILabel.

So, it would be great to create a category or method that will process the original Y-axis pixel of the actual text drawn on the view.

Is this even possible? How does a UITextField correctly position text and cursor?

UPDATE

I thought it was possible by getting a UILabel context and finding a space at the top, which should give me the original Y value of the pixel of the actual text? I have no idea where to even start with this, or make it effective enough not to impede frequent changes to UILabel.

Many thanks.

+5
source share
1 answer

Do you need to use UILabel or can you use a disabled UITextView?

For a text view, you can do something like

CGRect rect = [textView.layoutManager usedRectForTextContainer:textView.textContainer];

That should give you what you want. If the memory serves the rectangle that it returns, it will refer to the text view.

Hope this helps :)

0
source

All Articles