图文混排之NSAttributedString(超级详细的富文本大全)

富文本(图文混排)—— TextKit & CoreText 富文本 图文混排 3种方式: 1.webView 使用简单 负载大 2.CoreText 性能好,功能强大 使用起来复杂 3.TextKit iOS7以后 是对CoreText做了封装,集成在UIKit框架中 使用比较简单,功能有限 //NSAttributedString.h 中文本属性key的说明 /* NSFontAttributeName 设置字体属性,默认值:字体 阅读详情

最近项目中对图文混排有一定的需求,例如价格,文字链接,文字颜色变化等要求,翻了很多资料,咱们对这些属性做了如下的总结,希望能在方便自己查阅!

    NSFontAttributeName               设置字体大小和字体的类型 默认12 Helvetica(Neue)
    NSForegroundColorAttributeName    设置字体颜色,默认黑色 UIColor对象
    NSBackgroundColorAttributeName    设置字体所在区域的背景颜色,默认为nil,透明色
    NSLigatureAttributeName           设置连体属性,NSNumber对象 默认0 没有连体
    NSKernAttributeName               设置字符间距, NSNumber浮点型属性 正数间距加大,负数间距缩小
    NSStrikethroughStyleAttributeName 设置删除线,NSNumber对象
    NSStrikethroughColorAttributeName 设置删除线颜色,UIColor对象,默认是黑色
    NSUnderlineStyleAttributeName     设置下划线,NSNumber对象 NSUnderlineStyle枚举值
    NSUnderlineColorAttributeName     设置下划线颜色,UIColor对象,默认是黑色
    NSStrokeWidthAttributeName        设置笔画宽度,NSNumber对象 正数中空 负数填充
    NSStrokeColorAttributeName        设置填充部分颜色,不是指字体颜色,UIColor对象
    NSShadowAttributeName             设置阴影属性,取值为NSShadow对象
    NSTextEffectAttributeName         设置文本特殊效果 NSString对象 只有图版印刷效果可用
    NSBaselineOffsetAttributeName     设置基线偏移量,NSNumber float对象 正数向上偏移,负数向下偏移
    NSObliquenessAttributeName        设置字体倾斜度,NSNumber float对象,正数右倾斜,负数左倾斜
    NSExpansionAttributeName          设置文本横向拉伸属性,NSNumber float对象,正数横向拉伸文本,负数压缩
    NSWritingDirectionAttributeName   设置文字书写方向,从左向右或者右向左
    NSVerticalGlyphFormAttributeName  设置文本排版方向,NSNumber对象。0 横向排版,1 竖向排版
    NSLinkAttributeName               设置文本超链接,点击可以打开指定URL地址
    NSAttachmentAttributeName         设置文本附件,取值为NSTextAttachment对象,一般为图文混排
    NSParagraphStyleAttributeName     设置文本段落排版,为NSParagraphStyle对象

11.18新增NSParagraphStyleAttributeName的属性介绍

paragraphStyle.lineSpacing = 0.0;//增加行高
paragraphStyle.headIndent = 0;//头部缩进,相当于左padding
paragraphStyle.tailIndent = 0;//相当于右padding
paragraphStyle.lineHeightMultiple = 0;//行间距是多少倍
paragraphStyle.alignment = NSTextAlignmentLeft;//对齐方式
paragraphStyle.firstLineHeadIndent = 0;//首行头缩进
paragraphStyle.paragraphSpacing = 0;//段落后面的间距
paragraphStyle.paragraphSpacingBefore = 0;//段落之前的间距

效果图:

1.NSFontAttributeName

说明:该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)

2.NSForegroundColorAttributeName

说明:该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色

3.NSBackgroundColorAttributeName

说明:设置文字背景颜色

NSString *string = @"落霞与孤鹜齐飞,秋水共长天一色";
    NSMutableAttributedString *mutableAttriteStr = [[NSMutableAttributedString alloc] init];
    NSAttributedString *attributeStr1 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(0, 3)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"futura" size:12],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor yellowColor]}];
    NSAttributedString *attributeStr4 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(3, 4)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"futura" size:22],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor yellowColor]}];
    NSAttributedString *attributeStr2 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(8, 4)] attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:22],NSForegroundColorAttributeName : [UIColor blackColor],NSBackgroundColorAttributeName : [UIColor lightGrayColor]}];
     NSAttributedString *attributeStr5 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(12, 3)] attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName : [UIColor blackColor],NSBackgroundColorAttributeName : [UIColor lightGrayColor]}];
    NSAttributedString *attriteStr3 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(7, 1)] attributes:@{NSBackgroundColorAttributeName : [UIColor greenColor]}];
    [mutableAttriteStr appendAttributedString:attributeStr1];
    [mutableAttriteStr appendAttributedString:attributeStr4];
    [mutableAttriteStr appendAttributedString:attriteStr3];
   
    [mutableAttriteStr appendAttributedString:attributeStr2];
     [mutableAttriteStr appendAttributedString:attributeStr5];
    self.label1.attributedText = mutableAttriteStr;

4.NSLigatureAttributeName

连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1

表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)

// 连体艺术字,不是每个都能连起的,f和l  f和i就可以,其他各位可以自己去试试
    self.label2.attributedText = [[NSAttributedString alloc] initWithString:@"flush and fily" attributes:@{NSLigatureAttributeName : [NSNumber numberWithInt:1],NSFontAttributeName : [UIFont fontWithName:@"futura" size:30]}];
    
    NSMutableAttributedString *mutableAttributeStr2 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string6 = [[NSAttributedString alloc] initWithString:@"ABCDE " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:-3],NSForegroundColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string7 = [[NSAttributedString alloc] initWithString:@"FGHIJ " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:0],NSForegroundColorAttributeName : [UIColor yellowColor]}];
    NSAttributedString *string8 = [[NSAttributedString alloc] initWithString:@"KLMNO " attributes:@{NSKernAttributeName : @(15),NSForegroundColorAttributeName : [UIColor blueColor]}];
    [mutableAttributeStr2 appendAttributedString:string6];
    [mutableAttributeStr2 appendAttributedString:string7];
    [mutableAttributeStr2 appendAttributedString:string8];
    self.label3.attributedText = mutableAttributeStr2;

5.NSKernAttributeName

字符间距正值间距加宽,负值间距变窄

NSMutableAttributedString *mutableAttributeStr2 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string6 = [[NSAttributedString alloc] initWithString:@"ABCDE " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:-3],NSForegroundColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string7 = [[NSAttributedString alloc] initWithString:@"FGHIJ " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:0],NSForegroundColorAttributeName : [UIColor yellowColor]}];
    NSAttributedString *string8 = [[NSAttributedString alloc] initWithString:@"KLMNO " attributes:@{NSKernAttributeName : @(15),NSForegroundColorAttributeName : [UIColor blueColor]}];
    [mutableAttributeStr2 appendAttributedString:string6];
    [mutableAttributeStr2 appendAttributedString:string7];
    [mutableAttributeStr2 appendAttributedString:string8];
    self.label3.attributedText = mutableAttributeStr2;

6.NSStrikethroughStyleAttributeName 和 NSStrikethroughColorAttributeName

NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),

NSStrikethroughColorAttributeName 设置删除线颜色

枚举常量 NSUnderlineStyle中的值:

NSUnderlineStyleNone =0x00, 不设置

NSUnderlineStyleSingle =0x01, 设置单细删除线

NSUnderlineStyleThickNS_ENUM_AVAILABLE(10_0,7_0) = 0x02, 设置粗单删除线

NSUnderlineStyleDoubleNS_ENUM_AVAILABLE(10_0,7_0) = 0x09,双细删除线

NSMutableAttributedString *mutableAttributeStr3 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string9 = [[NSAttributedString alloc] initWithString:@"只要998  " attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle),NSStrikethroughColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string10 = [[NSAttributedString alloc] initWithString:@"真的998  " attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleThick),NSStrikethroughColorAttributeName : [UIColor blueColor]}];
    NSAttributedString *string11 = [[NSAttributedString alloc] initWithString:@"好吧9块拿走" attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleDouble),NSStrikethroughColorAttributeName : [UIColor yellowColor]}];
    
    [mutableAttributeStr3 appendAttributedString:string9];
    [mutableAttributeStr3 appendAttributedString:string10];
    [mutableAttributeStr3 appendAttributedString:string11];
    self.label4.attributedText = mutableAttributeStr3;

7.NSUnderlineStyleAttributeName 和 NSUnderlineColorAttributeName

给文字加下划线和更换下划线颜色,属性和上面的删除线都是一样用的

NSMutableAttributedString *mutableAttributeStr4 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string12 = [[NSAttributedString alloc] initWithString:@"只要888  " attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle),NSUnderlineColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string13 = [[NSAttributedString alloc] initWithString:@"真的88  " attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleThick),NSUnderlineColorAttributeName : [UIColor purpleColor]}];
    NSAttributedString *string14 = [[NSAttributedString alloc] initWithString:@"好吧8块拿走" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleDouble),NSUnderlineColorAttributeName : [UIColor yellowColor]}];
    
    [mutableAttributeStr4 appendAttributedString:string12];
    [mutableAttributeStr4 appendAttributedString:string13];
    [mutableAttributeStr4 appendAttributedString:string14];
    self.label5.attributedText = mutableAttributeStr4;

8.NSStrokeWidthAttributeName 和 NSStrokeColorAttributeName

设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心.NSStrokeWidthAttributeName 这个属性所对应的值是一个 NSNumber 对象(小数)。该值改变笔画宽度(相对于字体 size 的百分比),负值填充效果,正值中空效果,默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为 3.0。
同时设置了空心的两个属性,并且 NSStrokeWidthAttributeName 属性设置为整数,文字前景色就无效果了

NSMutableAttributedString *mutableAttributeStr5 = [[NSMutableAttributedString alloc] init];
    // 如果这个stroke的width正数的时候,字体就中空了,那么前景色就设置无效了
    NSAttributedString *string15 = [[NSAttributedString alloc] initWithString:@"HELLO " attributes:@{NSStrokeWidthAttributeName : @(5),NSStrokeColorAttributeName : [UIColor yellowColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor]}];
    NSAttributedString *string16 = [[NSAttributedString alloc] initWithString:@"YUKD  " attributes:@{NSStrokeWidthAttributeName : @(0),NSStrokeColorAttributeName : [UIColor redColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30]}];
    NSAttributedString *string17 = [[NSAttributedString alloc] initWithString:@"GOOGLE" attributes:@{NSStrokeWidthAttributeName : @(-5),NSStrokeColorAttributeName : [UIColor greenColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor lightGrayColor]}];
    
    [mutableAttributeStr5 appendAttributedString:string15];
    [mutableAttributeStr5 appendAttributedString:string16];
    [mutableAttributeStr5 appendAttributedString:string17];
    self.label6.attributedText = mutableAttributeStr5;

9.NSShadowAttributeName

设置文字阴影,取值为NSShadow对象

// 设置阴影
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowBlurRadius = 5.0f; // 模糊度
    shadow.shadowColor = [UIColor blueColor];
    shadow.shadowOffset = CGSizeMake(1, 5);
    NSAttributedString *string20 = [[NSAttributedString alloc] initWithString:@"HELLO_HALY_璟" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow}];
    
    self.label7.attributedText = string20;

10.NSTextEffectAttributeNam

NSTextEffectAttributeName //设置文本特殊效果,取值为NSString类型,目前只有一个可用效果 NSTextEffectLetterpressStyle(凸版印刷效果)

// 文本印刷,我也不知道是什么鬼
    // 设置阴影
    NSAttributedString *string21 = [[NSAttributedString alloc] initWithString:@"HELLO_HALY_璟" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow,NSTextEffectAttributeName : NSTextEffectLetterpressStyle}];
    
    self.label8.attributedText = string21;

11.NSBaselineOffsetAttributeName

文字基线偏移,想要达到以下图片的效果,就要设置需要的文字偏移,正数上移,负数下移

// 设置文本的基线 负数向下  正数向上  0不变
    UIFont *bigFont = [UIFont boldSystemFontOfSize:36];
    UIFont *smallFont = [UIFont boldSystemFontOfSize:bigFont.pointSize / 2];
    CGFloat capHeight = bigFont.pointSize - smallFont.pointSize;
    NSMutableAttributedString *attributeString6 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string22 = [[NSAttributedString alloc] initWithString:@"¥" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string23 = [[NSAttributedString alloc] initWithString:@"9" attributes:@{NSFontAttributeName : bigFont,NSForegroundColorAttributeName : [UIColor blueColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string24 = [[NSAttributedString alloc] initWithString:@".99" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    
    
    NSAttributedString *string28 = [[NSAttributedString alloc] initWithString:@"    七夕大促销    " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:24],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0),NSShadowAttributeName : shadow}];
    
    NSAttributedString *string25 = [[NSAttributedString alloc] initWithString:@"¥" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(capHeight - 5)}];
    NSAttributedString *string26 = [[NSAttributedString alloc] initWithString:@"0" attributes:@{NSFontAttributeName : bigFont,NSForegroundColorAttributeName : [UIColor redColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string27 = [[NSAttributedString alloc] initWithString:@".09" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(capHeight - 5)}];
    [attributeString6 appendAttributedString:string22];
    [attributeString6 appendAttributedString:string23];
    [attributeString6 appendAttributedString:string24];
    [attributeString6 appendAttributedString:string28];
    [attributeString6 appendAttributedString:string25];
    [attributeString6 appendAttributedString:string26];
    [attributeString6 appendAttributedString:string27];
    self.label9.attributedText = attributeString6;
    
    NSMutableAttributedString *attributeString7 = [[NSMutableAttributedString alloc] init];
    
    NSAttributedString *string29 = [[NSAttributedString alloc] initWithString:@"Hello  " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:40],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string30 = [[NSAttributedString alloc] initWithString:@"  World    " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    
    NSAttributedString *string31 = [[NSAttributedString alloc] initWithString:@"Hello  " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:40],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}];
    NSAttributedString *string32 = [[NSAttributedString alloc] initWithString:@"  World" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(6)}];
    
    
    [attributeString7 appendAttributedString:string29];
    [attributeString7 appendAttributedString:string30];
    [attributeString7 appendAttributedString:string31];
    [attributeString7 appendAttributedString:string32];
    self.label10.attributedText = attributeString7;

12.NSObliquenessAttributeName

NSObliquenessAttributeName 设置字体倾斜度,取值为 NSNumber(float),正值右倾,负值左倾

13.NSExpansionAttributeName

NSExpansionAttributeName 设置字体的横向拉伸,取值为NSNumber (float),正值拉伸 ,负值压缩

14.NSVerticalGlyphFormAttributeNameNSVerticalGlyphFormAttributeName 设置文字排版方向,取值为NSNumber对象(整数),0表示横排文本,1表示竖排文本 在iOS中只支持0

//    说明:NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为NSNumber对象(整数),0表示横排文本,1表示竖排文本 在iOS中只支持0
//说明:NSObliquenessAttributeName 设置字体倾斜度,取值为 NSNumber(float),正值右倾,负值左倾
//    NSExpansionAttributeName
//    说明:NSExpansionAttributeName 设置字体的横向拉伸,取值为NSNumber (float),正值拉伸 ,负值压缩
    NSMutableAttributedString *mutableAttriteStr = [[NSMutableAttributedString alloc] init];
    NSAttributedString *attributeStr1 = [[NSAttributedString alloc] initWithString:@"HeHe_XiXi_mm" attributes:@{NSFontAttributeName :[UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow,NSObliquenessAttributeName : @(1),NSExpansionAttributeName : @(1),NSVerticalGlyphFormAttributeName : @(1)}];
    [mutableAttriteStr appendAttributedString:attributeStr1];
    self.label1.attributedText = mutableAttriteStr;

15.NSWritingDirectionAttributeName

文字书写方向取值为以下组合为例 他是一个数组包含NSNumber 一般书写就是L ---R

那么我们多个需求也就是从R --- L

The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of leftToRight and rightToLeft withNSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride

     // 文字书写方向
//    The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of leftToRight and rightToLeft with NSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride, as shown in Table 1.
//    NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding
//    NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding
//    NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride
//    NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride
    NSMutableAttributedString *attributuStr2 = [[NSMutableAttributedString alloc] init];
    NSAttributedString *string1 = [[NSAttributedString alloc] initWithString:@"一切皆有可能" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:24],NSForegroundColorAttributeName : [UIColor redColor],NSWritingDirectionAttributeName : @[@(3)]}];
    [attributuStr2 appendAttributedString:string1];
    self.label.attributedText = attributuStr2;

16.NSLinkAttributeName

这货有点奇葩,所以很多人用第三方例如YYLabel来做,这东西不能在UILabel和UITextField使用,只能用UITextView来进行,实现他的代理,在代理方法里面进行URL跳转

该方法返回YES就能打开URL,NO不做任何事情

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange

注:

1.一定要实现UITextView的代理才能进行URL跳转

2.textView的editable属性修改为NO,在编辑时不可点击

//    把 NSLinkAttributeName 属性单独列出来,是因为在 UILabel 和 UITextField 中是无法使用该属性的。更准确点说是在UILabel 和 UITextField 中无法实现点击链接启动浏览器打开一个URL地址,因为在此过程中用到了一个代理函数。只能用在 UITextView 中
    self.textView.delegate = self;
    self.textView.scrollEnabled = NO;
    self.textView.editable = NO;
    self.textView.textContainer.lineFragmentPadding = 0;
    self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
    NSString *str = @"  跳转到宓珂璟的博客";
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str];
    [attrStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://blog.csdn.net/deft_mkjing"] range:[str rangeOfString:@"宓珂璟的博客"]];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30] range:NSMakeRange(0, str.length)];
    self.textView.attributedText = attrStr;

17.NSTextAttachment

设置文本附件,取值为NSTextAttachment对象 常用于图文混排

 NSString *words = @"天才";
    NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:words attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:25]}];
    NSTextAttachment *attatch = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    attatch.bounds = CGRectMake(0, 0, 40, 30);
    attatch.image = [UIImage imageNamed:@"$DCZ2WLN9RWI6JF(Q`P_(NH.jpg"];
    NSTextAttachment *attatch1 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    attatch1.bounds = CGRectMake(0, 0, 50, 30);
    attatch1.image = [UIImage imageNamed:@"%5T@J(4WKWSOX2~~PY0{4M0.jpg"];
    NSAttributedString *string8 = [NSAttributedString attributedStringWithAttachment:attatch];
    NSAttributedString *string9 = [NSAttributedString attributedStringWithAttachment:attatch1];
    [strAtt insertAttributedString:string8 atIndex:1];
    [strAtt insertAttributedString:string9 atIndex:0];
    self.label3.attributedText = strAtt;

基本的就介绍到这里了,上面那些已经足够用了,而且可以任意组合出想要的图文混排,

如果遇到特殊的需求或者不能满足的,那么就需要各位去试试往上的第三方富文本了

给个好用的大家可以试试点击打开链接

本次Demo地址:Demo传送门

iOS图文混排之【 NSAttachmentAttributeName 创建带有图片的富文本】(案例:展示信用卡标签) 文章目录引言I、富文本如何添加图片?see also 引言 需求:特性标签存在多个 特性标签字段labelTitle "labelTitle" : "核卡105\r\n首刷后再补贴65", 应用场景:存在图片和文字并排展示,例如特性标签 I、富文本如何添加图片? see also iOS富文本使用指南【持续更新中】: 1、封装富文本API,采用block实现链式编程 2、 超链接属性 3、HTML字符串与富文本互转 4、在适配系统API的应用 ———————————————— 版权 阅读详情

相关推荐

NSAttributedString 的小例子

NSAttributedString的小例子 http://blog.csdn.net/zhangao0086/article/details/7616385

NSAttributedString

字符属性在Standard Attributes中列举的Standard Attributes如下: 在NSAttributedString描述中,列举的字符属性有:/* 字符属性 字符属性可以应用于 attributed string 的文本中。 NSString *const NSFontAttributeName;(字体) NSString *const NSParagraphStyl

了凡 1986

安卓端目标检测模型部署血泪史:从NCNN/TFLite/MNN全军覆没到Torchscipts终见光明

公司需在Android设备部署目标检测模型,要求: ✅ 实时检测速度≥15FPS ✅ 模型精度损失<5% ✅ 兼容中低端安卓设备👉 GitHub项目地址:pytorch/android-demo-app/ObjectDetection我已上传这份源码到csdn下载准备模型(必须走这步才能跑通安卓端源码)如果您没有设置PyTorch环境来运行该脚本,可以下载模型文件此处转到“android演示app/ObjectDetection/app/src/main/assets”文件夹,然后跳过此步骤的其余部分,直

机器学习、深度学习、强化学习、迁移学习 1582

富文本

ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线、删除线、斜体、空心字体、背景色、阴影以及图文混排(一种文字中夹杂图片的显示效果)。通常想要实现这些效果要使用到iOS的Foundation框架提供的NSAttributedString类,NSAttributedString类中有许多属性,不同属性对应不同的文本样式。本文主要对这些属性做一个解释说明,并会结合实际代码来应用它们。 ...

雨夜枫起的博客 914

AD20/Altium designer——如何进行DRC检查、冲突的错误如何改正

对于一个画完的PCB,我们常常需要进行DRC检查,确保板子的电器连接及制作工艺在设定规则的范围内,本篇将介绍如何对PCB进行后期DRC检查处理,确保电路板出现不必要错误。 1.DRC检查入口 2.DRC设置 3.错误分析 对于错误的内容,依据个人实际情况不同,其出现的原因都是因为与设计规则中的设定标准冲突,这里按遇到的错误进行修改讲解,其他错误同理。 4.常见错误分析及解决方法 (1) 解决方法:将未连接网络连接(...

嵌入式创客工坊的博客 5万+

ubuntu18.04 升级Ubuntu 20.04

Ubuntu 18.04升级20.04指南:首先备份重要数据,更新当前系统并检查新版本可用性(sudo do-release-upgrade -c)。确认升级后执行sudo do-release-upgrade,按提示完成操作。升级过程需耐心等待,完成后重启系统。注意:升级前务必备份数据,确保系统为最新版本,以降低风险。升级后即可使用Ubuntu 20.04系统。

https://ophome.blog.csdn.net,在IT行业有多年的工作经验,尤其是在Python、Linux、负载均衡、Nginx和服务器运维等领域。 1062

iOS--NSAttributedString超全属性详解及应用(富文本、图文混排)

ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线、删除线、斜体、空心字体、背景色、阴影以及图文混排(一种文字中夹杂图片的显示效果)。通常想要实现这些效果要使用到iOS的Foundation框架提供的NSAttributedString类,NSAttributedString类中有许多属性,不同属性对应不同的文本样式。本文主要对这些属性做一个解释说明,并会结合实际代码来应用它们。 ...

小天空的博客 3911

新手必看:SMIC 0.18um PDK安装保姆级教程(附Cadence IC617环境配置)

本文提供SMIC 0.18um PDK在Cadence IC617环境下的详细安装教程,涵盖环境准备、文件解压、安装脚本运行、环境变量配置及Cadence集成验证等关键步骤,特别针对初学者常见问题提供解决方案,助力快速掌握芯片设计工具配置。

weixin_30522183的博客 509

14、iOS里面的富文本

iOS里面的富文本巧用UITextView实现富文本的点击效果 巧用UITextView实现富文本的点击效果 class RegisterController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() //用UItextView 实现富文本的功能 let textView = UITextView(frame: CGRect(x:

重楼的博客 3503

科研智能体平台搭建与实战:大模型如何驱动科研自动化

大模型技术的快速发展,正推动AI智能体从单纯对话工具向主动执行任务的科研伙伴演进。智能体的核心原理在于整合模型调用、工具使用、知识库检索与工作流编排,使其能够自主拆解目标、执行操作并迭代优化。在科研场景中,这一技术价值尤为突出:文献初筛、实验数据清洗、代码调试、结果汇总等高频重复劳动均可实现自动化,大幅释放研究者精力。从基础组件配置到多智能体协同,从平台选型到提示词调优,实际落地需要关注模型匹配、工具稳定性、记忆管理与人机校验机制。本文基于真实工程经验,系统梳理科研智能体平台的搭建路径、应用案例与高频问题排

weixin_32597009的博客 296

手把手教你用STM32F429+FreeRTOS搭建开源SIP电话(附代码与避坑指南)

本文详细介绍了如何使用STM32F429和FreeRTOS搭建开源SIP电话系统,涵盖硬件选型、FreeRTOS任务划分、PJSIP协议栈移植、音频系统优化及安全实践。通过实战案例和代码示例,帮助开发者快速实现嵌入式VoIP通信,特别适合物联网和音视频通信领域的应用开发。

weixin_33582311的博客 363

iOS自己总结的超级详细分解富文本大全(AttributedString),图文混排很轻松

最近项目中对图文混排有一定的需求,例如价格,文字链接,文字颜色变化等要求,翻 了很多资料,咱们对这些属性做了如下的总结,希望能在方便自己查阅! NSFontAttributeName 设置字体大小和字体的类型 默认12 Helvetica(Neue) NSForegroundColorAttributeName 设置字体颜色,默认黑色 UICo

Deft_MKJing的博客 1万+

黑马Java第二阶段:JavaWeb(笔记)

黑马Java第二阶段:JavaWeb(笔记)

chen的博客 5681

iOS 富文本效果整理 基于NSAttributedString

利用 UILabel 的 attributedText 属性可设置不同的文本样式 首先初始化一个NSAttributedString NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc] initWithString:@"你看我是富文本"]; 这里初始加给label.attributedText,看起来是平平无奇的样子 这里要先设置label的初始textColor和font等,展示基本样式 然后选择一种样式.

RollingPin的博客 1146

uniapp - web-view嵌套H5网页白屏问题,<web-view>页面跳转出现白屏情况,强力解决uniapp嵌套webview页面时出现无法加载并白屏(无论本地还是线上H5网页链接都能解决)

uniapp微信小程序,uniapp APP端,uni-app web-view嵌套H5项目白屏问题分析及解决,uniapp嵌套webview页面时,出现白屏,uniapp <web-view>页面跳转出现白屏解决方法,uniapp <web-view>页面跳转出现白屏解决方法,解决uniapp的web-view打开网页在老手机上出现白屏的问题,uni-app web-view白屏处理,引入本地网页白屏,WebView 加载不出网页,一片空白webview白屏加载不出来,使用webview加载链接后白屏,u

🏆 前端领域优质创作者 1万+

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

NSAttributedString,简单图文混排,UITextKit

图文混排一般都会用CoreText来实现,单对于初学者来说,却不是那么容易能够接受,所以我在这里分享一下简单的实现方法。 最终可以实现 如下的效果。 注:这里的实现并未用到CoreText, 而用到了TextKit里的NSAttributedString和NSTextAttachment 一、小试牛刀: //1.先创建 NSMutableAttributedString, N

stree7cleaner的博客 1004

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

图文混排的四种方案

转载自:http://lemon2well.top/2018/08/15/iOS%20开发/图文混排的四种方案/ 图文混排有多种方式可以实现,下面我会用四种不同的方式来实现以下界面的效果,并且说明他们的优缺点。 NSAttributedString NSAttributedString提供了自由并且多样式的富文本设置,图文混排的实现是通过插入NSTestAttachment来实现。 talk is cheap ,here is the code 1 2 3 4 5 6 7 .

沸腾的泪水的专栏 4058

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

NSAttributedString实现图文混排

/*            常用属性      // 字体      NSFontAttributeName               // UIFont, default Helvetica(Neue) 12      // 段落      NSParagraphStyleAttributeName     // NSParagraphStyle, default defaultP

quanhaoH的专栏 518

ios 解析html图文混排,iOS 之 图文混排(富文本)

学习自MJ,分享给大家!分类// 富文本技术:// 1.图文混排// 2.随意修改文字样式与CoreText,textKit的区别/**iOS 6之前:CoreText,纯C语言,极其蛋疼iOS 6开始:NSAttributedString,简单易用iOS 7开始:TextKit,功能强大,简单易用*/代码示例及注释讲解- (void)setupTextView{// 富文本技术:// 1.图文混...

weixin_36210213的博客 603

iOS_NSAttributedString 的21种属性详细介绍(图文混排)

说明: NSAttributedString 可以非常方便的实现文字排版和图文混排功能. 共有21中效果(API), 本文将较详细的介绍21种的属性的使用 核心API:类: NSAttributedString, NSMutableAttributedString API: /** * API: Character Attributes , NSAttributedString

Sponge_CMZ的博客 3497

iOS 富文本、图文混排

说明:前几天突然要用到一行字的不同显示,例如下面这行字的样式,然后我就上百度搜索了下关于富文本,总结了一些关于如何处理这些文字的例子。后面我将会继续更新,图文混排的样式例子。 1.富文本(NSMutableAttributedString实现) 1.)使用的关键代码 NSMutableAttributedString *string = [[NSMutableAttributedSt

赵前的博客 1185

如何快速掌握iOS富文本编辑:YYText与NSAttributedString完整指南

TimLiu-iOS是iOS开发常用三方库、插件、知名博客等资源的整合项目,其中富文本编辑相关工具能帮助开发者轻松实现复杂文本展示效果。本文将带你快速掌握YYText与NSAttributedString的使用方法,让你的iOS应用文本展示更具吸引力。 ## 为什么选择YYText进行富文本开发? YYText是功能强大的iOS富文本框架,相比系统原生的NSAttributedString,它

gitblog_01388的博客 355

NSAttributedString,并富文本,正则表达式,超链接,水印

重载NSAttachment传送门 正则表达式传送门 码: #import "ViewController.h" //NSAttributedString.h 中文本属性key的说明 /*  NSFontAttributeName                设置字体属性,默认值:字体:Helvetica(Neue) 字号:12  NSFo

LivesXu的博客 4717
上一篇: iOS Xcode 升级Xcode15报错: SDK does not contain ‘libarclite‘
下一篇: 解决终端无法访问github.com 的错误
VKOOY
博客等级 码龄11年 26粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值