开发者

How to calculate the height of the text rectangle from an NSString?

I know there is this one:

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

But since the CGSize always has the same height and doesn't adjust to any shrinked text or whatsoever, the CGSize is not telling how heigh the text is.

Example: Make a UILabel with 320 x 55 points and put a loooooooooooooong text in there. Let the label shrink the text down. Surprise: CGSize.height remains the same height even if the text is so tiny that you need a microscope.

Ok so after banging my head against my macbook pro which is half way broken now, the only think that can help is that nasty actualFontSize. But the font size is in pica I think, it's not really what you get on the screen, isn't it?

When that font size is 10, is my tex开发者_运维问答t really 10 points heigh at maximum? Once in a while I tried exactly that, and as soon as the text had a y or some character that extends to below (like that tail of an y does), it is out of bounds and the whole text is bigger than 10 points.

So how would you calculate the real text height for a single line uilabel without getting a long beard and some hospital experience?


Try this code:

CGSize maximumSize = CGSizeMake(300, 9999);
NSString *myString = @"This is a long string which wraps";
UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont 
    constrainedToSize:maximumSize 
    lineBreakMode:self.myLabel.lineBreakMode];

from my answer here

It uses a different method, and sets up a very high CGSize at the start (which is then shrunk to fit the string)


Important: As of iOS 7.0 the following method is deprecated.

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

Use the below code instead

CGRect frame = [cellText boundingRectWithSize:CGSizeMake(568,320) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:32.0f]} context:nil];
float height = frame.size.height;


Sounds like after you get the actual font size from that function call, you need to call again with that new size:

NSString* yourString = @"SomeString";
float actualSize;
[yourString sizeWithFont:yourFont 
             minFontSize:minSize 
          actualFontSize:&actualSize 
                forWidth:rectWidth 
           lineBreakMode:breakMode];

CGSize size = [yourString sizeWithFont:[UIFont fontWithName:fontName size:actualSize]];


Also have you set label.numberOfLines = 0; ?


After running this code frame.size will have the height and width of your nsstring exactly..

NSString *text = @"This is my Mac";
textFont = [NSFont fontWithName:@"HelveticaNeue-Medium" size: 80.f];
textColor = [NSColor yellowColor];
NSDictionary *attribs = @{ NSForegroundColorAttributeName:textColor,
                           NSFontAttributeName: textFont };
CGRect frame = [text boundingRectWithSize:CGSizeMake(0,0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesDeviceMetrics attributes:attribs context:nil];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜