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
结论:
- 设置的字体大小就是 pointSize
- ascender + descender = lineHeight,每一行的总高度
- 间隙大小即为 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])];