我在UITableView中有一个UILabel,它应该最多只有2行,并且周围有一点填充(左右各7个像素,上下各2个像素)。我正在使用autolayout,仅针对iOS6及以上版本进行测试。所有视图都是通过编程方式创建和添加的。
我已经对我的UILabel进行了子类化,这里是init
方法:
- (id)init
{
self = [super init];
self.translatesAutoresizingMaskIntoConstraints = NO;
self.numberOfLines = 2;
self.backgroundColor = UIColorFromARGB(0x99000000);
self.textColor = [UIColor whiteColor];
self.font = [UIFont boldSystemFontOfSize:14.0f];
return self;
}
如果我添加这个,我会得到正确的填充,但它会变成一行:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {2, 7, 2, 7};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
我已经看到过这个答案几次,但是对我没有作用(没有效果):
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
UIEdgeInsets insets = {2, 7, 2, 7};
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,insets) limitedToNumberOfLines:numberOfLines];
}
它在表格视图中有区别吗?任何帮助将不胜感激。