UILabel 设置 text Insets(padding)

2021-08-04  本文已影响0人  TyroneLin
@interface UALabel : UILabel

@property (nonatomic, assign) UIEdgeInsets insets;

@end
@implementation UALabel

#pragma mark - override

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
}

- (CGSize)intrinsicContentSize {
    CGSize size = [super intrinsicContentSize];
    size.width += (self.insets.left + self.insets.right);
    size.height += (self.insets.top + self.insets.bottom);
    return size;
}

#pragma mark - getter && setter

- (void)setInsets:(UIEdgeInsets)insets {
    _insets = insets;
    [self layoutSubviews];
}

@end
上一篇下一篇

猜你喜欢

热点阅读