Mah*_*hir 5 objective-c nsstring uilabel ios
我有一个UILabel的子类,它应该在用户输入内容时更新其文本.当然,随着文本长度的增加,标签的大小必须调整以适应文本.我调用了sizeToFit方法,当标签正确调整其宽度时,文本的底部被切断.问题是,该文本包括标和上标,标签不与在考虑下标调整本身(例如,用H 2 O两者的底部被切断).
我可以覆盖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]
}
Run Code Online (Sandbox Code Playgroud)
小智 2
您应该在子类中重写 UILabel 方法 (CGSize)sizeThatFits:(CGSize)size,如下例所示。我只是在 UILabel 计算的高度上加上 10pt 以容纳下标。
\n\n@implementation ESKLabel\n- (CGSize)sizeThatFits:(CGSize)size\n{\n CGSize theSize = [super sizeThatFits:size];\n return CGSizeMake(theSize.width, theSize.height + 10);\n}\n@end\nRun Code Online (Sandbox Code Playgroud)\n\n示例输出:
\n\nself.eskLabel.text = @"Hello Long\xc2\xb2 Long\\u2082 World";\nNSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));\n[self.eskLabel sizeToFit];\nNSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));\nRun Code Online (Sandbox Code Playgroud)\n\n来自 NSLog:
\n\nThis GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 864. \n2012-01-06 23:34:21.949 Stackoverflow4[864:f803] CGSize: {85, 61} \n2012-01-06 23:34:21.951 Stackoverflow4[864:f803] CGSize: {302, 44} \nkill \nquit\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
6379 次 |
| 最近记录: |