CATextLayer wrapped sizeToFit?

If I set textLayer.wrapped = YES , how do I resize textLayer to contain wrapped text? Ie, how do I get a new textLayer height?

Basically, I want something like -[UILabel sizeToFit] .

+6
source share
1 answer

The first thing you need to do is get the size of the text.

Fortunately, the NCCtring UIKit link complements the link and offers a method for doing just that:

 - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode 

This will give you CGSize , which you can then use to set the frame of your UILabel or any other UIView subclass that you use.

So, if textLayer is a UILabel - and not a CALayer , you will get something like this:

 UIFont *myFont = [UIFont boldSystemFontOfSize:12.0f]; CGSize myFontSize = [myString sizeWithFont:myFont]; myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myFontSize.width, myFontSize.height)]; myLabel.text = newTitle; myLabel.font = myFont; 
+2
source

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


All Articles