UILable 设置左右上下边距&首行缩进&行间距

🌰 设置上下左右边距
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;

🌰 设置首行缩进

//显示内容
NSString *content = @"您好您好.....小乖乖,小机灵鬼,哎呀呀....哎呀呀";

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.alignment = NSTextAlignmentLeft;  //对齐
paraStyle.headIndent = 0.0f;//行首缩进
//参数:(字体大小17号字乘以2,34f即首行空出两个字符)
CGFloat emptylen = self.contentLB.font.pointSize * 2;
paraStyle.firstLineHeadIndent = emptylen;//首行缩进
paraStyle.tailIndent = 0.0f;//行尾缩进
paraStyle.lineSpacing = 1.5f;//行间距
NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:content attributes:@{NSParagraphStyleAttributeName:paraStyle}];
self.contentLB.attributedText = attrText;

如果问题解决的话,求点赞..谢谢小主

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 5,235评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,037评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,858评论 0 17
  • 宝宝现在越来越懂事,想拉臭臭时就拉着妈妈指厕所,或者自己在哪拉臭臭了我们不知道,她会不停地拉我们的衣服到臭臭那去清...
    jr812阅读 1,432评论 0 0
  • 今日一早最新消息,卓普科技了解到:摩拜意图收购小品牌共享单车——由你单车。虽然收购资金尚不明确,但双方意向很足,摩...
    暗月圣雪阅读 2,924评论 0 1

友情链接更多精彩内容