我有UILabel的子类,当用户键入内容时,它应该更新其文本。自然地,随着文本的长度增加,标签的大小必须调整以适应文本。我调用了sizeToFit方法,当标签正确调整宽度时,文本的底部被切掉。问题是该文本包括下标和上标,并且该标签没有考虑下标(例如,用H 2 O将两者的底部切断)。在具有下标的UILabel上调用方法sizeToFit不起作用
我可以重写sizeToFit或sizeThatFits:增加标签的高度吗?
编辑:在你的子类像下面的例子(CGSize)尺寸:
- (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]
}
重写sizeToFit和后[超级sizeToFit];更改高度 – SAKrisT 2012-01-07 00:16:03
@SAKrisT' - (void)sizeToFit { [super sizeToFit]; self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,100); }' – Mahir 2012-01-07 00:24:48
@SAKrisT我试过,但没有出现视图 – Mahir 2012-01-07 00:25:16