在具有下标的 UILabel 上调用方法 sizeToFit 不起作用

发布于 2024-12-25 21:33:05 字数 721 浏览 1 评论 0原文

我有一个 UILabel 的子类,它应该在用户键入内容时更新其文本。当然,随着文本长度的增加,标签的大小必须调整以适应文本。我调用了 sizeToFit 方法,当标签正确调整其宽度时,文本的底部被切断。问题是文本包含下标和上标,并且标签没有根据考虑的下标进行自我调整(例如,使用 H2O 时,两者的底部被切断)。

我可以覆盖 sizeToFit 或 sizeThatFits: 以增加标签的高度吗?

编辑:

- (void) addCompound {

self.currentLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];

[self addSubview:self.currentLabel];

[self.currentLabel sizeToFit];

// Right now self.currentlabel.text = "". However, I've confirmed thru NSLogging that letters are added to self.currentLabel.text as the user types on the keyboard. Also, the text displays properly (as long as it's within the original frame) when I remove [sel.currentLabel sizeToFit]

}

I have a subclass of UILabel, which is supposed to update its text when the user types something. Naturally, as the length of text increases, the size of the label must adjust to accommodate the text. I called the sizeToFit method, and while the label adjusts its width correctly, the bottom of the text is cut off. The problem is that the text includes subscripts and superscripts , and the label is not adjusting itself with the subscripts in consideration (for example, with H₂O the bottom of the two is cut off).

Can I override sizeToFit or sizeThatFits: to increase the height of the label?

EDIT:

- (void) addCompound {

self.currentLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];

[self addSubview:self.currentLabel];

[self.currentLabel sizeToFit];

// Right now self.currentlabel.text = "". However, I've confirmed thru NSLogging that letters are added to self.currentLabel.text as the user types on the keyboard. Also, the text displays properly (as long as it's within the original frame) when I remove [sel.currentLabel sizeToFit]

}

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

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

发布评论

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

评论(2

蓝天 2025-01-01 21:33:05

您应该在子类中重写 UILabel 方法 (CGSize)sizeThatFits:(CGSize)size,如下例所示。我只是在 UILabel 计算的高度上加上 10pt 以容纳下标。

@implementation ESKLabel
- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize theSize = [super sizeThatFits:size];
    return CGSizeMake(theSize.width, theSize.height + 10);
}
@end

示例输出:

self.eskLabel.text = @"Hello Long² Long\u2082 World";
NSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));
[self.eskLabel sizeToFit];
NSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));

来自 NSLog:

This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 864. 
2012-01-06 23:34:21.949 Stackoverflow4[864:f803] CGSize: {85, 61} 
2012-01-06 23:34:21.951 Stackoverflow4[864:f803] CGSize: {302, 44} 
kill 
quit

You should override the UILabel method (CGSize)sizeThatFits:(CGSize)size in your subclass like example below. I just add 10pt to the height calculated by UILabel to accommodate the subscript.

@implementation ESKLabel
- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize theSize = [super sizeThatFits:size];
    return CGSizeMake(theSize.width, theSize.height + 10);
}
@end

Sample output:

self.eskLabel.text = @"Hello Long² Long\u2082 World";
NSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));
[self.eskLabel sizeToFit];
NSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));

From the NSLog:

This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 864. 
2012-01-06 23:34:21.949 Stackoverflow4[864:f803] CGSize: {85, 61} 
2012-01-06 23:34:21.951 Stackoverflow4[864:f803] CGSize: {302, 44} 
kill 
quit
小傻瓜 2025-01-01 21:33:05

这应该是诀窍:

self.eskLabel.adjustsFontSizeToFitWidth = YES;

This should to the trick:

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