在自动布局中将填充物添加到Uilabel



如何在用自动布局定位和尺寸的同时,如何将填充物添加到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];
}

但根本没有被调用。

,但根本没有被调用。

也许是因为您的标签不是您的子类的实例?

在这里,从这个简单的样本开始:

- (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子类。在您的子类中实现。然后确保您的您的 uilabel 是该子类的实际实例。(如果您在笔尖中创建标签,默认情况下不会。)

(还请注意,AutoLayout不会神奇地知道该标签的行为与普通标签不同,因此您可能还必须 必须覆盖intrinsicContentSize,具体取决于您的用例标签以您想要的方式尺寸。)

我的 drawTextInRect:方法是由KAProgressLabel直接调用super方法的事实引起的。

最新更新