NSAttributedString/NSMutableAttributedString富文本的使用(NSMutableParagraphStyle)

时间:2023-02-01 00:21:07

图文混排使用时,可以使用类NSAttributedString,或NSMutableAttributedString进行字符串的创建和相关属性的设置。其中字符串属性的设置,包括设置文本段落排版格式NSMutableParagraphStyle,也包括文本属性。

NSAttributedString/NSMutableAttributedString富文本的使用(NSMutableParagraphStyle)

代码示例

// 文本段落排版格式
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle alloc] init];
textStyle.lineBreakMode = NSLineBreakByWordWrapping; // 结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
textStyle.alignment = NSTextAlignmentLeft; //(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)
textStyle.lineSpacing = 5; // 字体的行间距
textStyle.firstLineHeadIndent = 5.0; // 首行缩进
textStyle.headIndent = 0.0; // 整体缩进(首行除外)
textStyle.tailIndent = 0.0; //
textStyle.minimumLineHeight = 20.0; // 最低行高
textStyle.maximumLineHeight = 60.0; // 最大行高
textStyle.paragraphSpacing = 15; // 段与段之间的间距
textStyle.paragraphSpacingBefore = 22.0f; // 段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
textStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 从左到右的书写方向(一共➡️三种)
textStyle.lineHeightMultiple = 15; /* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
textStyle.hyphenationFactor = 1; //连字属性 在iOS,唯一支持的值分别为0和1
// 文本属性/*     使用注意:     1、字体背景颜色NSBackgroundColorAttributeName 与 字体阴影NSShadowAttributeName 不能同时设置,否则无效果;     2、超链接NSLinkAttributeName 在UILable中无法使用,需要在类型UITextView中使用;     3、NSAttachmentAttributeName属性NSTextAttachment的使用需要新建成一个attributedstring,然后再拼接在原attributedstring中使用,否则无效*/NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] init];// NSParagraphStyleAttributeName 文本段落排版格式[textAttributes setValue:textStyle forKey:NSParagraphStyleAttributeName];// NSFontAttributeName 字体名称和大小[textAttributes setValue:[UIFont systemFontOfSize:15.0] forKey:NSFontAttributeName];// NSForegroundColorAttributeNam 颜色[textAttributes setValue:[UIColor redColor] forKey:NSForegroundColorAttributeName];// NSBackgroundColorAttributeName 字体背景颜色//    [textAttributes setValue:[UIColor yellowColor] forKey:NSBackgroundColorAttributeName];// NSLigatureAttributeName 连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符[textAttributes setValue:[NSNumber numberWithInt:0] forKey:NSLigatureAttributeName];// NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄[textAttributes setValue:@(2) forKey:NSKernAttributeName];// 删除线(样式、颜色)// 删除线-样式:NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)[textAttributes setValue:[NSNumber numberWithInt:NSUnderlineStyleThick] forKey:NSStrikethroughStyleAttributeName];// 删除线-颜色:SStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色[textAttributes setValue:[UIColor greenColor] forKey:NSStrikethroughColorAttributeName];// 下划线(样式、颜色)// 下划线-样式:NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似[textAttributes setValue:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];// 下划线-颜色:NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色[textAttributes setValue:[UIColor brownColor] forKey:NSUnderlineColorAttributeName];// NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果[textAttributes setValue:@(1) forKey:NSStrokeWidthAttributeName];// NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象[textAttributes setValue:[UIColor blackColor] forKey:NSStrokeColorAttributeName];// NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象NSShadow *textShadow = [[NSShadow alloc] init];textShadow.shadowColor = [UIColor lightGrayColor];textShadow.shadowOffset = CGSizeMake(5.0, 5.0);[textAttributes setValue:textShadow forKey:NSShadowAttributeName];//    // NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用//    [textAttributes setValue:NSTextEffectLetterpressStyle forKey:NSTextEffectAttributeName];// NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏[textAttributes setValue:@(0.1) forKey:NSBaselineOffsetAttributeName];// NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾[textAttributes setValue:@(0.5) forKey:NSObliquenessAttributeName];// NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本[textAttributes setValue:@(0.1) forKey:NSExpansionAttributeName];// NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写[textAttributes setValue:NSWritingDirectionEmbedding forKey:NSWritingDirectionAttributeName];// NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本[textAttributes setValue:@(1) forKey:NSVerticalGlyphFormAttributeName];// NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址[textAttributes setValue:[NSURL URLWithString:@"http://www.hao123.com"] forKey:NSLinkAttributeName];
// 附件对象,如图片等// NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排NSTextAttachment *attachment = [[NSTextAttachment alloc] init];attachment.image = [UIImage imageNamed:@"girl.jpg"];attachment.bounds = CGRectMake(0.0, 0.0, 60.0, 30.0);//    [textAttributes setValue:attachment forKey:NSAttachmentAttributeName]; // 无效NSAttributedString *attributedAttachment = [NSAttributedString attributedStringWithAttachment: attachment];
// 字符串NSString *text = @"devZhang is an iOS developer";// 字符串转换NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:textAttributes];[attributedText insertAttributedString:attributedAttachment atIndex:2];// 在Lable中显示UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 300.0)];[self.view addSubview:label];label.backgroundColor = [UIColor clearColor];label.attributedText = attributedText;


使用注意事项:

1、字体背景颜色NSBackgroundColorAttributeName字体阴影NSShadowAttributeName不能同时设置,否则无效果;

2、超链接NSLinkAttributeNameUILable中无法使用,需要在类型UITextView中使用;

3NSAttachmentAttributeName属性NSTextAttachment的使用需要新建成一个attributedstring,然后再拼接在原attributedstring中使用,否则无效