iOS开发之NSMutableParagraphStyle富文本

2023-05-16

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线行间距的需求。就需要富文本来实现。

一、实例化方法和使用方法

    NSMutableAttributedString *detailStr = [[NSMutableAttributedString alloc] initWithString:detailString];
NSRange stringRange = NSMakeRange(0, 5);
    [detailStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#999999"] range:stringRange];// 添加单个属性
    [detailStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, detailString.length)];// 添加多个属性
    [detailLabel setAttributedText:detailStr];

二、富文本属性

属性作用类型
NSFontAttributeName字号UIFont 默认12
NSParagraphStyleAttributeName段落样式NSParagraphStyle
NSForegroundColorAttributeName前景色UIColor
NSBackgroundColorAttributeName背景色UIColor
NSLigatureAttributeName连笔字1或0
NSKernAttributeName字间距CGFloat
NSStrikethroughStyleAttributeName删除线1或0
NSUnderlineStyleAttributeName下划线1或0
NSStrokeColorAttributeNamesame as foreground colorUIColor
NSStrokeWidthAttributeName字体描边CGFloat
NSShadowAttributeName阴影NSShawdow
NSTextEffectAttributeName设置文本特殊效果,目前只有图版印刷效果可用NSString
NSAttachmentAttributeName设置文本附件,常用插入图片NSTextAttachment
NSLinkAttributeName链接NSURL (preferred) or NSString
NSBaselineOffsetAttributeName基准线偏移NSNumber
NSUnderlineColorAttributeName下划线颜色UIColor
NSStrikethroughColorAttributeName删除线颜色UIColor
NSObliquenessAttributeName字体倾斜NSNumber
NSExpansionAttributeName字体加粗NSNumber 比例 0就是不变 1增加一倍
NSWritingDirectionAttributeName文字方向 分别代表不同的文字出现方向等等@[@(1),@(2)]
NSVerticalGlyphFormAttributeName水平或者竖直文本 在iOS,不支持竖版1竖直 0水平

三、段落样式

段落样式主要改行距、段距、首行缩进、最大最小行高、多倍行距等十几个属性

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS开发之NSMutableParagraphStyle富文本 的相关文章

随机推荐