Rich text NSAttributedString/NSMutableAttributedString (mixed graphics and text)

NSAttributedString/NSMutableAttributedString

  • Method 1: Set the text first, and then add corresponding text effects according to the range

insert image description here

    NSString *string = @"NSMutableAttributedString\n NSMutableAttributedString\n";
    // NSMutableAttributedString
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:string];
    // 设置字体
    [attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)];
    [attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(5, 20)];
    // 设置文字颜色
    [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, 10)];
    // 设置文字背景
    [attribute addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(15, 20)];
  • Method 2: NSAttributedStringYou can set the corresponding effect and text according to the dictionary, and then splicing them NSAttributedStringintoNSMutableAttributedString
    insert image description here
    // attribute1
    NSDictionary *dict1 = @{
    
    NSForegroundColorAttributeName:UIColor.redColor, NSFontAttributeName:[UIFont systemFontOfSize:20]};
    NSAttributedString *attribute1 = [[NSAttributedString alloc] initWithString:@"测试字段1" attributes:dict1];
    // attribute2
    NSDictionary *dict2 = @{
    
    NSForegroundColorAttributeName:UIColor.blueColor, NSFontAttributeName:[UIFont systemFontOfSize:30]};
    NSAttributedString *attribute2 = [[NSAttributedString alloc] initWithString:@"测试字段2" attributes:dict2];
	// NSMutableAttributedString
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] init];
    [attribute appendAttributedString:attribute1];
    [attribute appendAttributedString:attribute2];

paragraph properties

You can also add corresponding paragraph effects according to the range, or after NSAttributedStringsetting the splicing

    // 段落样式
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    // 行间距
    paragraph.lineSpacing = 10;
    // 段落间距
    paragraph.paragraphSpacing = 20;
    // 对齐方式
    paragraph.alignment = NSTextAlignmentLeft;
    // 指定段落开始的缩进像素
    paragraph.firstLineHeadIndent = 30;
    // 调整全部文字的缩进像素
    paragraph.headIndent = 10;
    // 添加段落设置
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, 20)];
    NSDictionary *dict1 = @{
    
    NSForegroundColorAttributeName:UIColor.redColor, NSParagraphStyleAttributeName:paragraph};
    NSAttributedString *attribute1 = [[NSAttributedString alloc] initWithString:@"测试字段1" attributes:dict1];

Mixed graphics and text

  • NSTextAttachmentGenerate an NSAttributedStringobject according to
    insert image description here
    // attribute1
    NSDictionary *dict1 = @{
    
    NSForegroundColorAttributeName:UIColor.redColor, NSFontAttributeName:[UIFont systemFontOfSize:20]};
    NSAttributedString *attribute1 = [[NSAttributedString alloc] initWithString:@"测试字段1"
                                                                    attributes:dict1];
    // attribute2
    NSDictionary *dict2 = @{
    
    NSForegroundColorAttributeName:UIColor.blueColor, NSFontAttributeName:[UIFont systemFontOfSize:30]};
    NSAttributedString *attribute2 = [[NSAttributedString alloc] initWithString:@"测试字段2"
                                                                    attributes:dict2];
    
    // imageAttribute
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"study_loading"];;
    textAttachment.bounds = CGRectMake(0, 0, 17, 17);
    NSAttributedString *imageAttribute = [NSAttributedString attributedStringWithAttachment:textAttachment];
	// NSMutableAttributedString
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] init];
    [attribute appendAttributedString:attribute1];
    [attribute appendAttributedString:imageAttribute];
    [attribute appendAttributedString:attribute2];

Related dictionary fields

    NSFontAttributeName // 文字大小,UIFont类型
    NSParagraphStyleAttributeName // 段落样式,NSParagraphStyle类型
    NSForegroundColorAttributeName // 文字颜色,UIColor类型
    NSBackgroundColorAttributeName // 文字背景颜色,UIColor类型
    NSLigatureAttributeName // 连体属性,NSNumber类型,1为连体,0为不连
    NSKernAttributeName // 字符之间的间距,NSNumber类型
    NSTrackingAttributeName // 跟踪,NSNumber类型(浮点),0表示禁用
    NSStrikethroughStyleAttributeName // 删除线,NSNumber类型,枚举NSUnderlineStyle
    NSUnderlineStyleAttributeName // 下划线,NSNumber类型,0无下划线
    NSStrokeColorAttributeName // 笔画填充部分颜色,UIColor类型
    NSStrokeWidthAttributeName // 笔画的宽度,NSNumber类型(整数),负值填充效果,正值中空效果
    NSShadowAttributeName // 阴影属性,NSShadow类型
    NSTextEffectAttributeName // 文本特殊效果,NSString类型,目前只有一个NSTextEffectLetterpressStyle 凸版印刷效果

    NSAttachmentAttributeName // 图片属性,NSTextAttachment类型,参考图文混编使用
    NSLinkAttributeName // 设置链接属性 NSURL or NSString
    NSBaselineOffsetAttributeName // 基线偏移值,NSNumber类型,正值上偏,负值下偏
    NSUnderlineColorAttributeName // 下划线的颜色,UIColor类型
    NSStrikethroughColorAttributeName // 删除线的颜色,UIColor类型
    NSObliquenessAttributeName // 设置字体倾斜度,NSNumber类型,正值右倾,负值左倾
    NSExpansionAttributeName // 字体横向拉伸,NSNumber类型,正值拉伸,负值压缩

    NSWritingDirectionAttributeName // 书写方向 类型为下面四种
    // @[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)]
 	// @[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)]
 	// @[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]
 	// @[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
 	
    NSVerticalGlyphFormAttributeName // 文字排版方向,NSNumber类型,0表示横排文本,1表示竖排文本

References

Summary of Rich Text AttributedString Summary of Tencent Cloud
Rich Text AttributedString Brief Book

Guess you like

Origin blog.csdn.net/weixin_46926959/article/details/129372170