如果我们设置UILabel的高度比较高,但是文字的显示是居中的,我们要的是显示在左上角
- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
textRect.origin.y = bounds.origin.y;
return textRect;
}
-(void)drawTextInRect:(CGRect)requestedRect {
CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
[super drawTextInRect:actualRect];
}
本文介绍了一种在UILabel中将文本强制显示在左上角而非居中的方法。通过重写textRectForBounds和drawTextInRect方法,可以实现文本在高高度UILabel中的特定位置显示。

被折叠的 条评论
为什么被折叠?



