The task is: how to calculate the frame substring (there is an NSRange) for a UILabel when the text is centered - NSTextAlignmentCenter?

ie, the origin position is x, y and the size is wight, height.

Everything works well when we use TextKit classes (NSTextStorage, NSLayoutManager, NSTextContainer) using the method: boundingRectForGlyphRange:inTextContainer: but with center alignment does not work.

Is there an elegant solution for UILabel? or if not, how to calculate the same in a UITextView?

Thank.

    1 answer 1

    I understand you can recognize the frame of the substring when aligning to the left, right? In this case, you can subtract the width of the entire line from the width of the textView and divide it by 2. This will be the offset to the left, you will need to increase the frame.origin.x.