🌰 设置上下左右边距
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;
如果问题解决的话,求点赞..谢谢小主