标签的大小在设置后会发生变化,具体取决于字符串

发布于 2024-11-09 14:44:57 字数 703 浏览 1 评论 0原文

我在为两个不同的字符串获取正确的框架时面临一个问题。我通过以下代码运行两个字符串:

UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 2;
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.font = [UIFont boldSystemFontOfSize:11];
myLabel.text = @"About Stores"
myLabel.text = @"About Rivers"

CGSize myLabelSize = CGSizeMake(70,28);
[myLabel sizeWithFont:myString.font     // <-- One of two strings
    constrainedToSize:aLabelSize 
        lineBreakMode:UILineBreakModeWordWrap];

Result for myString1 =>宽度:70,高度:28
myString2 的结果 =>宽度:70,高度:14

为什么这里有差异?

I am facing an issue in getting the correct frame for two different strings. I am running two strings through the below code:

UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 2;
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.font = [UIFont boldSystemFontOfSize:11];
myLabel.text = @"About Stores"
myLabel.text = @"About Rivers"

CGSize myLabelSize = CGSizeMake(70,28);
[myLabel sizeWithFont:myString.font     // <-- One of two strings
    constrainedToSize:aLabelSize 
        lineBreakMode:UILineBreakModeWordWrap];

Result for myString1 => Width: 70, Height: 28
Result for myString2 => Width: 70, Height: 14

Why there is a difference here?

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

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

发布评论

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

评论(3

裸钻 2024-11-16 14:44:57

我看到的问题:

1

NSString 没有字体属性。您可能希望将“myString.font”替换为 UILabel.font,其中 UILabel 将替换为您计划用于显示字符串的 UILabel 实例。

2

sizeWithFont 应该返回您存储在 CGSize 实例中的 CGSize。

看来你对这个的使用还不是很清楚。我已经发布了有关如何正确使用 sizeWithFont 的答案。您应该在这里检查一下:在不知道左侧标签中文本字符串长度的情况下,左右并排放置两个 UILabels

顺便说一句,这就是为什么你会看到高度差异......我认为垃圾值作为字体大小参数传入。如果对字体大小参数进行硬编码,您将看到一致的结果。

Issues I see:

1

NSString has no font property. You might want to replace "myString.font" with UILabel.font where the UILabel is to be replaced by the instance of UILabel you plan to use to display the string.

2

sizeWithFont is supposed to return a CGSize that you store in an instance of CGSize.

Seems like you're not very clear on using this. I've posted an answer on how to correctly use sizeWithFont. You should check that out here : Place two UILabels side by side left and right without knowing string length of text in left label

By the way, that's why you see a difference in the heights... I think a garbage value is being passed in as the font size argument. If you hardcode the font size argument, you'll see consistent results.

在风中等你 2024-11-16 14:44:57

只需替换代码并尝试一下......我不知道它是否有效,只需检查:)

[myLabel sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:11]     
constrainedToSize:myLabelSize  lineBreakMode:UILineBreakModeWordWrap];

以及“;”标签中缺少

myLabel.text = @"About Stores";
myLabel.text = @“关于河流”;`

问候..

just replace the code n give a try.....i don kno if it works jus check :)

[myLabel sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:11]     
constrainedToSize:myLabelSize  lineBreakMode:UILineBreakModeWordWrap];

and also ";" is missing in label`

myLabel.text = @"About Stores";
myLabel.text = @"About Rivers";`

regards..

锦上情书 2024-11-16 14:44:57

这是有区别的,因为系统字体是 Helvetica,它是一种可变大小的字体。 myString1 中的文本实际上比 myString2 中的文本宽,因此该字符串需要换行到第二行才能适合。由于您指定了 2 行 max 和 UILineBreakModeWordWrap,这就是您所得到的。如果您希望它适合一行,请使 myLabelSize 稍宽一点,或者使用较小的字体大小。

There's a difference because the system font is Helvetica, which is a variable-sized font. The text in myString1 is maginally wider than the text in myString2, so the string needs to wrap to a second line in order to fit. Since you specified 2 lines max and UILineBreakModeWordWrap, that's what you get. If you want it to fit in one line, make myLabelSize a little wider or else use a smaller font size.

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