在自动布局中向 UILabel 添加填充
Add padding to UILabel in Auto Layout
如何在使用自动布局定位和调整大小时向 UILabel
添加填充?
我尝试将其子类化并重写 drawTextInRect:
方法,如下所示:
- (void)drawTextInRect:(CGRect)rect {
CGFloat borderWidth = 5;
CGFloat doubleBigSquareRadius = CGRectGetWidth(rect);
CGFloat doubleSmallSquareRadius = doubleBigSquareRadius - borderWidth * 2;
CGFloat smallSquareSideSize = doubleSmallSquareRadius / sqrt(2);
CGFloat smallSquareOffset = (doubleBigSquareRadius - smallSquareSideSize) / 2;
CGSize smallSquareSize = CGSizeMake(smallSquareSideSize, smallSquareSideSize);
CGPoint smallSquareOrigin = CGPointMake(smallSquareOffset, smallSquareOffset);
CGRect smallSquareFrame = CGRectMake(smallSquareOrigin.x, smallSquareOrigin.y, smallSquareSize.width, smallSquareSize.height);
[super drawTextInRect:smallSquareFrame];
}
但它根本没有被调用。
but it doesn't get called at all.
可能是因为你的标签不是你子类的实例?
在这里,从这个简单的示例开始:
- (void)drawTextInRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextStrokeRect(context, CGRectInset(self.bounds, 1.0, 1.0));
[super drawTextInRect:CGRectInset(rect, 5.0, 5.0)];
}
您需要一个 UILabel 子类。在你的子类中实现它。然后确保 your UILabel is 该子类的实际实例。 (如果您在笔尖中创建标签,默认情况下不会。)
(另请注意,自动布局不会神奇地知道此标签的行为与普通标签有任何不同,因此您可能 也 必须覆盖 intrinsicContentSize
,具体取决于在你的用例中,为了让标签按照你想要的方式调整自己的大小。)
我的drawTextInRect:
方法是因为KAProgressLabel
直接调用了super
方法here.
如何在使用自动布局定位和调整大小时向 UILabel
添加填充?
我尝试将其子类化并重写 drawTextInRect:
方法,如下所示:
- (void)drawTextInRect:(CGRect)rect {
CGFloat borderWidth = 5;
CGFloat doubleBigSquareRadius = CGRectGetWidth(rect);
CGFloat doubleSmallSquareRadius = doubleBigSquareRadius - borderWidth * 2;
CGFloat smallSquareSideSize = doubleSmallSquareRadius / sqrt(2);
CGFloat smallSquareOffset = (doubleBigSquareRadius - smallSquareSideSize) / 2;
CGSize smallSquareSize = CGSizeMake(smallSquareSideSize, smallSquareSideSize);
CGPoint smallSquareOrigin = CGPointMake(smallSquareOffset, smallSquareOffset);
CGRect smallSquareFrame = CGRectMake(smallSquareOrigin.x, smallSquareOrigin.y, smallSquareSize.width, smallSquareSize.height);
[super drawTextInRect:smallSquareFrame];
}
但它根本没有被调用。
but it doesn't get called at all.
可能是因为你的标签不是你子类的实例?
在这里,从这个简单的示例开始:
- (void)drawTextInRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextStrokeRect(context, CGRectInset(self.bounds, 1.0, 1.0));
[super drawTextInRect:CGRectInset(rect, 5.0, 5.0)];
}
您需要一个 UILabel 子类。在你的子类中实现它。然后确保 your UILabel is 该子类的实际实例。 (如果您在笔尖中创建标签,默认情况下不会。)
(另请注意,自动布局不会神奇地知道此标签的行为与普通标签有任何不同,因此您可能 也 必须覆盖 intrinsicContentSize
,具体取决于在你的用例中,为了让标签按照你想要的方式调整自己的大小。)
我的drawTextInRect:
方法是因为KAProgressLabel
直接调用了super
方法here.