美文网首页
UILabel 边距 2021-01-11

UILabel 边距 2021-01-11

作者: iOS打怪升级 | 来源:发表于2021-01-11 11:40 被阅读0次
  1. labal 边距设置
@interface HQPaddingLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets textInsets; // 控制字体与控件边界的间隙
@end
@implementation HQPaddingLabel
- (instancetype)init
{
    if (self = [super init]) {
        _textInsets = UIEdgeInsetsZero;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _textInsets = UIEdgeInsetsZero;
    }
    return self;
}

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


- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    if (self.text.length == 0) {
        return [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    }
    
    UIEdgeInsets insets = self.textInsets;
    CGRect rect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    rect.origin.x    -= insets.left;
    rect.origin.y    -= insets.top;
    rect.size.width  += (insets.left + insets.right);
    rect.size.height += (insets.top + insets.bottom);
    return rect;
    
}

  1. YYLabel 空白占位处理
 YYTextContainer *container = [YYLabel tq_getTextContainerInRect:CGRectMake(0, 0, kScreen_Width - 50, answerHeight) indentFootSize:CGSizeMake(width, 16)];
    self.answerLabel.textLayout = [YYTextLayout layoutWithContainer:container text:self.answerLabel.attributedText];



+ (YYTextContainer *)tq_getTextContainerInRect:(CGRect)rect indentFootSize:(CGSize)indentFootSize
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    UIBezierPath *headIndentPath = [UIBezierPath bezierPathWithRect:CGRectMake(rect.size.width - indentFootSize.width, rect.size.height - indentFootSize.height, indentFootSize.width, indentFootSize.height)];
    YYTextContainer *container = [YYTextContainer containerWithPath:path];
    container.exclusionPaths = @[headIndentPath];
    
    return container;
}

相关文章

  • UILabel 边距 2021-01-11

    labal 边距设置 YYLabel 空白占位处理

  • UILabel的边距计算

    小伙伴们在开发时可能经常遇到这样的需求 里面的内容是不固定的,四周都有间距,遇到这样的需求先不要急着用view嵌套...

  • uilabel 内边距

    重写一个方法 - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets...

  • 边距

  • 边距

    外边距margin 相当于平移,本身并无增加。 margin就像移动位置,他不管移动到哪,自身还是这么大小,只不过...

  • UILabel设置内边距

    CustomLabel.h #import @interface CustomLabel : UILabel @p...

  • UILabel内边边距

    在开发过程中,简单的使用UILabel属性,不能够达到我们显示一些特殊的要求。UILabel要显示边框时,不像UI...

  • uilabel改变内边距

    .h 直接代码 .m

  • UILabel两边对齐的实现(NSAttributedStrin

    标签(空格分隔): ios UILabel 最近有需求要让UILabel 实现两边的对齐,我们知道UIlabel默...

  • iOS给UILabel增加内边距

    UILabel默认不带内边距,调整内边距步骤: 1.制定一个空白区:UIEdgeInsets。 2. 重写draw...

网友评论

      本文标题:UILabel 边距 2021-01-11

      本文链接:https://www.haomeiwen.com/subject/xrtdaktx.html