美文网首页
UILable 设置左右上下边距

UILable 设置左右上下边距

作者: 纯阳子_ | 来源:发表于2017-08-14 14:54 被阅读157次

1:自定义UILable
.h:

 @interface ContectLable : UILabel

// 用来决定上下左右内边距,也可以提供一个借口供外部修改,在这里就先固定写死
@property (assign, nonatomic) UIEdgeInsets edgeInsets;

@end

.m:

@implementation ContectLable

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
         self.edgeInsets = UIEdgeInsetsMake(0, 3, 0, 3);
    }
  return self;
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.edgeInsets = UIEdgeInsetsMake(0, 3, 0, 3);
    }
    return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    UIEdgeInsets insets = self.edgeInsets;
    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
                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;
}

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

@end

2: 自定义后,发现lable如果为自适应的话,就会显示不全,解决方法:

    [lable sizeToFit];
    lable.adjustsFontSizeToFitWidth =YES;

3:中划线

 NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
 NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"内容" attributes:attribtDic];
 label.attributedText = attribtStr;

4:下划线

 NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
 NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"内容" attributes:attribtDic];
 label.attributedText = attribtStr;

相关文章

  • UILable 设置左右上下边距

    1:自定义UILable.h: .m: 2: 自定义后,发现lable如果为自适应的话,就会显示不全,解决方法: ...

  • Masonry布局遇到的问题

    约束设置有问题的时候报这个提示,同时设置了高度和上下边距或者同时设置了宽度和左右边距

  • iOS 去除 UITextView 内边距

    去除 textView 左右边距: 去除 textView 上下边距:

  • 给UILable设置内边距

    前两天公司项目遇到标签内容距离边框保持一定距离的需求,如图: 以前遇到这种需求,我肯定先计算文字的内容大小,再在内...

  • 拦截布局iOS

    像这种布局还需要左右上下边距设置一下?no,no,no 我们只需要拦截布局即可 #pragma mark - 拦截...

  • 考题1

    1,盒子的框高和宽: 盒宽=width+左右边框距+左右内边距 盒高=height+上下边框距+上下内边距 2,盒...

  • UILable,UITextfield

    UIlable //设置lable上的字 lable.text = @"zhang"; //设置lable中心对齐...

  • CSS学习心得(三)

    CSS Margin(外边距) 简写margin:100px 50px;表示上下边距100px,左右边距50px....

  • 由display:inline-block导致的边距解决方案

    分类:CSS 设置display:inline-block之后会导致右边距(空格)及下边距(换行) 解决方案: 1...

  • 今天的一个学习上的问题

    如何取消图片默认的下边距 设置img为块状元素 img[display:block;]

网友评论

      本文标题:UILable 设置左右上下边距

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