获取 NSString 高度

发布于 2024-11-28 04:47:39 字数 969 浏览 2 评论 0原文

我有一个 NSString,我想知道它的高度以创建合适的 UILabel。

现在做这个

NSString *string = @"this is an example"; 
CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:10.0f] 
                          forWidth:353.0 
                     lineBreakMode:UILineBreakModeWordWrap];
float height = size.height;

高度是13.0。 如果我使用这个字符串

NSString *string = @"this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example "; 

高度始终为 13.0(宽度为 353,这是不可能的)...我做错了什么?

ADD:

size.width;

工作正常...所以就像 lineBreakMode 不正确一样...但确实如此,不是吗?

I have an NSString, and I want to know its height to create an appropriate UILabel.

Doing this

NSString *string = @"this is an example"; 
CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:10.0f] 
                          forWidth:353.0 
                     lineBreakMode:UILineBreakModeWordWrap];
float height = size.height;

height is now 13.0.
If I use this string

NSString *string = @"this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example this is an example this is an example 
                     this is an example "; 

height is always 13.0 (and with 353 as width, that's impossible)... what am I doing wrong?

ADD:

size.width;

works fine... so it's like if the lineBreakMode is not correct... but it is, isn't it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-12-05 04:47:39

您所做的事情无法按预期工作的原因是因为

– sizeWithFont:forWidth:lineBreakMode: 

is 用于“计算单行文本的指标”,而

-sizeWithFont:constrainedToSize:lineBreakMode:

is 用于“计算多行文本的指标”。来自 文档

计算单行文本的指标

– sizeWithFont:
– sizeWithFont:forWidth:lineBreakMode:
– sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

计算多行文本的指标

– sizeWithFont:constrainedToSize:
– sizeWithFont:constrainedToSize:lineBreakMode:

尝试使用 -sizeWithFont:constrainedToSize:lineBreakMode: 代替,例如,这是我通常所做的:

CGSize maximumLabelSize = CGSizeMake(353,9999);

CGSize expectedLabelSize = [string sizeWithFont:label.font                        
                              constrainedToSize:maximumLabelSize 
                                  lineBreakMode:label.lineBreakMode]; 

CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;

The reason what you're doing doesn't work as you would expect is because

– sizeWithFont:forWidth:lineBreakMode: 

is for "Computing Metrics for a Single Line of Text" whereas

-sizeWithFont:constrainedToSize:lineBreakMode:

is for "Computing Metrics for Multiple Lines of Text". From the documentation:

Computing Metrics for a Single Line of Text

– sizeWithFont:
– sizeWithFont:forWidth:lineBreakMode:
– sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

Computing Metrics for Multiple Lines of Text

– sizeWithFont:constrainedToSize:
– sizeWithFont:constrainedToSize:lineBreakMode:

Try using -sizeWithFont:constrainedToSize:lineBreakMode: instead, e.g. this is what I usually do:

CGSize maximumLabelSize = CGSizeMake(353,9999);

CGSize expectedLabelSize = [string sizeWithFont:label.font                        
                              constrainedToSize:maximumLabelSize 
                                  lineBreakMode:label.lineBreakMode]; 

CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
圈圈圆圆圈圈 2024-12-05 04:47:39

根据 文档

此方法返回限制为的字符串的宽度和高度
指定的宽度。虽然它计算换行符
发生时,此方法实际上并未将文本换行到附加的
线。如果字符串的大小超过给定的宽度,则此方法
使用指定行截断文本(仅用于布局目的)
中断模式,直到它符合最大宽度;然后它返回
生成的截断字符串的大小

您应该使用 -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] ,它具有类似的行为,但您可以使用 CGFLOAT_MAX 作为传入尺寸的高度。

According to the Documentation

This method returns the width and height of the string constrained to
the specified width. Although it computes where line breaks would
occur, this method does not actually wrap the text to additional
lines. If the size of the string exceeds the given width, this method
truncates the text (for layout purposes only) using the specified line
break mode until it does conform to the maximum width; it then returns
the size of the resulting truncated string
.

You should to use -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] which has similar behavior but you can uuse CGFLOAT_MAX as the height in the size passed in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文