69_iOS干货36_label空隙留白和文本缩进

1,富文本属性相关介绍,

详见链接:https://mp.csdn.net/postedit/80391519

2,UIFont的详细分解

//UIFont的属性
@property(nonatomic,readonly,strong) NSString *familyName;
@property(nonatomic,readonly,strong) NSString *fontName;
@property(nonatomic,readonly)        CGFloat   pointSize;
@property(nonatomic,readonly)        CGFloat   ascender;
@property(nonatomic,readonly)        CGFloat   descender;
@property(nonatomic,readonly)        CGFloat   capHeight;
@property(nonatomic,readonly)        CGFloat   xHeight;
@property(nonatomic,readonly)        CGFloat   lineHeight ;
@property(nonatomic,readonly)        CGFloat   leading;

//属性简介
familyName 字体家族的名字
fontName 字体的名字
pointSize 字体大小
ascender 基准线以上的高度
descender 基准线以下的高度
capHeight 大小的高度
xHeight 小写x的高度
lineHeight 当前字体下的行高
leading 行间距(一般为0)

具体代码运行

UIFont *font = [UIFont systemFontOfSize:14];
NSLog(@"font.pointSize = %f,
font.ascender = %f,
font.descender = %f,
font.capHeight = %f,
font.xHeight = %f,
font.lineHeight = %f,
font.leading=%f",
font.pointSize,font.ascender,font.descender,font.capHeight,font.xHeight,font.lineHeight,font.leading);

打印结果
font.pointSize = 14.000000,
font.ascender = 13.330078,
font.descender = -3.376953,
font.capHeight = 9.864258,
font.xHeight = 7.369141,
font.lineHeight = 16.707031,
font.leading = 0.000000

结论:

  1. 设置的字体大小就是 pointSize
  2. ascender + descender = lineHeight,每一行的总高度
  3. 间隙大小即为 lineHeight - pointSize,在富文本中设置行高的时候,其实际文字间的距离就是加上这个距离的。

3,设置每行上下留白的方法

#pragma mark -  设置上下留白方案1:修改行距,文字高度默认占满字体
- (CGRect )getSpaceWithLineSpace:(CGFloat )lineSpace {
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = lineSpace - (self.font.lineHeight - self.font.pointSize);
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    [attributes setObject:self.font forKey:NSFontAttributeName];
    self.attributedText = [[NSAttributedString alloc] initWithString:self.text attributes:attributes];
    
     CGRect rect = [self.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];

    
    return rect;
}

#pragma mark -  上下文字留白方案2:修改文字高度,文字行距默认是0
- (CGRect )getSpaceWithLineHeight:(CGFloat )lineHeight {
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.maximumLineHeight = lineHeight;
    paragraphStyle.minimumLineHeight = lineHeight;
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    CGFloat baselineOffset = (lineHeight - self.font.lineHeight) / 4;
    [attributes setObject:@(baselineOffset) forKey:NSBaselineOffsetAttributeName];
    [attributes setObject:self.font forKey:NSFontAttributeName];
    self.attributedText = [[NSAttributedString alloc] initWithString:self.text attributes:attributes];
    
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-40, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    
    return rect;
    
}

4,设置每行左右缩进的方法

(上下缩进通过留白来控制,左右缩进,还要适当调整frame,不建议)

// 新增属性
@property (assign, nonatomic) UIEdgeInsets contentInsets;

//重写方法
-(void) drawTextInRect:(CGRect)rect {
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.contentInsets)];
}

5,设置首行缩进的方法

 NSMutableAttributedString *contentString = [[NSMutableAttributedString alloc]  initWithString:string];
   //首行缩进
    paragraphStyle.firstLineHeadIndent = font.pointSize * 2;
    [contentString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentString length])];

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值