如何从NSString计算文本矩形的高度?

时间:2023-02-01 07:56:34

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.

但由于CGSize始终具有相同的高度,并且不适应任何缩小的文本或任何缩小的文本,因此CGSize并没有说明文本的高度。

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.

示例:制作一个320 x 55点的UILabel并在其中放置一个loooooooooooooong文本。让标签缩小文本。惊喜:CGSize.height保持相同的高度,即使文字很小,你需要一个显微镜。

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?

好吧,在我的脑袋撞到我的macbook pro后,现在已经有一半了,唯一可以帮助的是那个令人讨厌的realFontSize。但我认为字体大小是异食癖,它不是你在屏幕上得到的,不是吗?

When that font size is 10, is my text 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.

当那个字体大小是10时,我的文字真的是10点最高吗?偶尔我尝试了一下,一旦文本有y或某个字符延伸到下面(就像y的那个尾部那样),它就会超出界限,整个文本大于10个点。

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

那么如何计算单线uilabel的真实文本高度,而不会留下长胡子和一些医院经验?

5 个解决方案

#1


2  

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]];

#2


3  

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)

它使用不同的方法,并在开始时设置一个非常高的CGSize(然后缩小以适应字符串)

#3


3  

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

重要提示:从iOS 7.0开始,不推荐使用以下方法。

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

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;

#4


1  

Also have you set label.numberOfLines = 0; ?

还有你设置label.numberOfLines = 0; ?

#5


0  

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

运行此代码后,frame.size将完全具有nsstring的高度和宽度。

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];

#1


2  

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]];

#2


3  

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)

它使用不同的方法,并在开始时设置一个非常高的CGSize(然后缩小以适应字符串)

#3


3  

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

重要提示:从iOS 7.0开始,不推荐使用以下方法。

sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

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;

#4


1  

Also have you set label.numberOfLines = 0; ?

还有你设置label.numberOfLines = 0; ?

#5


0  

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

运行此代码后,frame.size将完全具有nsstring的高度和宽度。

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];