我有一个 UILabel,如果我调整文本的大小,我可以让它看起来像一个 UITextView,但是左边距不同,在 UIlabel 上,文本正好靠在 UITextView 有一点边距的左边框上。我如何调整 UILabel 以便当这些控件相互叠加时,它们看起来一致?
问问题
5526 次
1 回答
5
只需更改标签的框架:
CGRect frame = label.frame;
CGRect newFrame = CGRectMake(frame.origin.x + MARGIN, frame.origin.y, frame.size.width - MARGIN, frame.size.height);
label.frame = newFrame;
当然,用您想要的保证金替换 MARGIN。
或者您可以像这样子类化UILabel
和覆盖textRectForBounds:limitedToNumberOfLines:
:
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect newBounds = CGRectMake(bounds.origin.x + MARGIN, bounds.origin.y, bounds.size.width - MARGIN, bounds.size.height);
return [super textRectForBounds:newBounds limitedToNumberOfLines:numberOfLines];
}
希望这可以帮助!
于 2010-06-24T05:23:30.980 回答