富文本NSAttributedString/NSMutableAttributedString(图文混编)

NSAttributedString/NSMutableAttributedString

  • 方法一:先设置文字,再按照范围添加对应文字效果

在这里插入图片描述

    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)];
  • 方法二:NSAttributedString可以根据字典设置对应效果和文字,再拼接NSAttributedStringNSMutableAttributedString
    在这里插入图片描述
    // 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];

段落属性

同样可以按照范围添加对应段落效果,或者在NSAttributedString设置完拼接

    // 段落样式
    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];

图文混编

  • 根据NSTextAttachment生成一个NSAttributedString对象
    在这里插入图片描述
    // 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];

相关字典字段

    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表示竖排文本

参考资料

富文本AttributedString的总结 腾讯云
富文本AttributedString的总结 简书

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值