iOS 富文本效果整理 基于NSAttributedString

利用 UILabel 的 attributedText 属性可设置不同的文本样式

  • 首先初始化一个NSAttributedString
NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc] initWithString:@"你看我是富文本"];

这里初始加给label.attributedText,看起来是平平无奇的样子
这里要先设置label的初始textColorfont等,展示基本样式
在这里插入图片描述

1. 设置字体和大小

NSFontAttributeName
在这里插入图片描述

//设置前三个字 [UIFont systemFontOfSize:15]
[abStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 3)];

2. 设置间距

NSParagraphStyleAttributeName
在这里插入图片描述

// 这里要用NSMutableParagraphStyle来设置各种属性
// 下面只是简单常用的处理行间距
// 还可以设置首行缩进,行高,对齐方式,段落,连字属性等
NSMutableParagraphStyle *pgpStyle = [[NSMutableParagraphStyle alloc] init];
pgpStyle.lineSpacing = 30; //行间距,注意:实际视觉行距要计算到实际行高
// 行间距是多少倍
// pgpStyle.lineHeightMultiple = 1.5f;
// 首行缩进  
// pgpStyle.firstLineHeadIndent = 30.0f;    
// 对齐方式 
// pgpStyle.alignment = NSTextAlignmentLeft; 
// 内容超出宽度时,内容有...省略的位置 ( "...abc" ,"abc..." ,"ab...c")   
// pgpStyle.lineBreakMode = NSLineBreakByTruncatingTail; 
// 整体缩进
// pgpStyle.headIndent = 30;//左边距    
// pgpStyle.tailIndent = 20;//右边距  
// 行高  
// 针对不同的字型与字号,可以透过指定最大与最小行距来避免过高或过窄的状况发生
// pgpStyle.minimumLineHeight = 10;//最低行高    
// pgpStyle.maximumLineHeight = 20;//最大行高    
// 段落 
// pgpStyle.paragraphSpacing = 15; //段落间距  
// pgpStyle.paragraphSpacingBefore = 30;//段首行空白空间 
// pgpStyle.paragraphSpacing = 30; //段落后面的间距  
[abStr addAttribute:NSParagraphStyleAttributeName
              value:pgpStyle
              range:NSMakeRange(0, 5)];

3. 设置文本颜色

NSForegroundColorAttributeName在这里插入图片描述

[abStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];

4. 设置文字背景色

NSBackgroundColorAttributeName在这里插入图片描述

[abStr addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];

5. 设置连字

NSLigatureAttributeName在这里插入图片描述

说明:
1.PingFangSC_Bold 和 PingFangSC_Regular 一般不会有连字问题
2.PingFangSC-Semibold等字体或者三方字体就有默认连字现象
3.也不是所有的字都会连,一般出现在fi和fl这种字符才会连,具体"连字"的概念问百度or谷歌

NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc]initWithString:@"fiflfi"];
//0表示不连 1表示连 实际中PingFangSC-Semibold等是默认连的
[abStr addAttribute:NSLigatureAttributeName value:@(1) range:NSMakeRange(0, abStr.length)];

6. 设置文字间距

NSKernAttributeName在这里插入图片描述

[abStr addAttribute:NSKernAttributeName value:@(10) range:NSMakeRange(0, 3)];

7. 设置删除线

NSStrikethroughColorAttributeName
NSStrikethroughStyleAttributeName
在这里插入图片描述

说明:
1.可设置单.双删除线
2.value赋值 [1, 7]是单线, [9, 15]是双线,区间内数值越大,线越粗,注意超出区间内的值,删除线就会失效

[abStr addAttribute:NSStrikethroughStyleAttributeName value:@(5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrikethroughStyleAttributeName value:@(13) range:NSMakeRange(3, 4)];
    
[abStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 4)];

8. 设置下划线

NSUnderlineStyleAttributeName
NSUnderlineColorAttributeName在这里插入图片描述

一般常用:value赋值
NSUnderlineStyleSingle //单根细线
NSUnderlineStyleThick //单根粗线
NSUnderlineStyleDouble //双根细线

//样式
[abStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, 3)];
//颜色
[abStr addAttribute:NSUnderlineColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
[abStr addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:NSMakeRange(4, 3)];

9. 文字描边

NSStrokeWidthAttributeName
NSStrokeColorAttributeName在这里插入图片描述

NSStrokeWidthAttributeName 这个设置描边字符的宽度,(-∞, 0)区间向内,(0, +∞)区间向外, 注意value设置过大就糊了哟
NSStrokeColorAttributeName 这个设置描边字符的颜色

[abStr addAttribute:NSStrokeWidthAttributeName value:@(2) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 3)];
    
[abStr addAttribute:NSStrokeWidthAttributeName value:@(-2) range:NSMakeRange(3, 3)];
[abStr addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(3, 3)];

10. 设置文字阴影

NSShadowAttributeName在这里插入图片描述

NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(5, 5);
shadow.shadowColor = [UIColor orangeColor];
// shadow.shadowBlurRadius = 5; 阴影模糊度
//貌似range设置无效了,都是这个文本
[abStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, abStr.length)];

11. 设置文本特殊效果(用处少)

NSTextEffectAttributeName

只有一直效果类型为NSTextEffectLetterpressStyle //凸版样式

12. 设置文件附件(图文混排)

NSAttachmentAttributeName在这里插入图片描述

NSTextAttachment *tathment = [[NSTextAttachment alloc]init];
tathment.image = [UIImage imageNamed:@"absImg.png"];
tathment.bounds = CGRectMake(0, 0, 35, 35);
NSAttributedString *imgAbs = [NSAttributedString attributedStringWithAttachment:tathment];
    
[abStr insertAttributedString:imgAbs atIndex:3];

13. 设置链接(只有UITextView可用)

NSLinkAttributeName

value 赋值类型是 NSURL or NSString

在这里插入图片描述

UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 200)];
tv.font = [UIFont systemFontOfSize:25];
tv.textAlignment = NSTextAlignmentCenter;
tv.editable = NO;
[self.view addSubview:tv];
    
NSMutableAttributedString *abStr = [[NSMutableAttributedString alloc]initWithString:@"你看我是富文本"];
[abStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, abStr.length)];
NSMutableParagraphStyle *pgpStyle = [[NSMutableParagraphStyle alloc] init];
pgpStyle.alignment = NSTextAlignmentCenter;
[abStr addAttribute:NSParagraphStyleAttributeName value:pgpStyle range:NSMakeRange(0, abStr.length)];
    
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
[abStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(abStr.length-3, 3)];
tv.attributedText = abStr;

14. 文字上下偏移

NSBaselineOffsetAttributeName在这里插入图片描述

vlaue 赋值 正负对应上下偏移

[abStr addAttribute:NSBaselineOffsetAttributeName value:@(6) range:NSMakeRange(2, 1)];
[abStr addAttribute:NSBaselineOffsetAttributeName value:@(-6) range:NSMakeRange(4, 1)];

15. 文字斜体

NSObliquenessAttributeName
在这里插入图片描述

vlaue 赋值 正负对应左右斜

[abStr addAttribute:NSObliquenessAttributeName value:@(-0.5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSObliquenessAttributeName value:@(0.5) range:NSMakeRange(4, 3)];

15. 文字拉伸

NSExpansionAttributeName // 横向拉伸
在这里插入图片描述

vlaue 赋值 正负对应变胖瘦

[abStr addAttribute:NSExpansionAttributeName value:@(-0.5) range:NSMakeRange(0, 3)];
[abStr addAttribute:NSExpansionAttributeName value:@(0.5) range:NSMakeRange(4, 3)];

16. 文字书写方向(一般用于自右向左)

NSWritingDirectionAttributeName
在这里插入图片描述

[abStr addAttribute:NSWritingDirectionAttributeName value:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)] range:NSMakeRange(0, abStr.length)];

17. 文字排版(iOS上无效果)

NSVerticalGlyphFormAttributeName

0为水平排版的字,1为垂直排版的字。iOS上效果一直为1

### 回答1: iOS 富文本可以通过使用 NSAttributedStringNSTextAttachment 类加载 GIF 图片,达到将富文本与动态效果结合的效果。 首先,需要将 GIF 图片转换为 NSData 格式,可通过使用 NSData 的类方法 dataWithContentsOfFile 或者 dataWithContentsOfURL 来实现。 接着,创建 NSTextAttachment 实例,并以 NSData 格式将 GIF 图片作为参数传入。然后,创建 NSMutableAttributedString 实例,并将富文本内容包含在其中。 最后,使用 UILabel、UITextView 或者 UIWebView 等组件来展示富文本内容,并加以控制动态效果的播放周期及重复次数等属性,达到更丰富的用户体验。 需要注意的是,在使用富文本加载 GIF 图片时,应考虑对应用性能及网络耗费等问题进行优化调整,使得应用流畅稳定,并能够节省用户移动数据流量等资源。 ### 回答2: iOS开发中,要加载gif动图作为富文本,可以使用如下的步骤: 1. 引入SDWebImage库:在项目中添加SDWebImage库,这是一个常用的图片加载库,可以方便地加载并显示网络上的图片。 2. 下载gif图片:从网络上找到合适的gif图片,并将其下载到本地。 3. 将gif图片添加到富文本中:使用SDWebImage提供的方法将gif图片添加到富文本中,可以使用`UIImage sd_animatedGIFWithData:`方法将本地的gif图片转换为UIImage对象,并使用该UIImage对象创建一个`NSTextAttachment`对象。 4. 将NSTextAttachment对象添加到NSAttributedString中:将创建好的NSTextAttachment对象添加到NSMutableAttributedString对象中,可以使用`appendAttributedString:`方法将其追加到NSMutableAttributedString对象的末尾。 5. 将NSAttributedString对象显示在界面上:通过UILabel、UITextView等界面控件,将NSMutableAttributedString对象显示在界面上,即可完成富文本加载gif的功能。 需要注意的是,为了保证gif动图的流畅播放,SDWebImage库会将gif图片展示为一系列的静态图片,然后再按照正确的帧率进行播放。另外,对于高性能的gif加载,也可以使用其他优化库,如FLAnimatedImage等。 综上所述,通过引入SDWebImage库、下载gif图片、将图片添加到富文本中,并将富文本显示在界面上等一系列步骤,即可实现在iOS中加载gif动图作为富文本显示的功能。 ### 回答3: 在iOS中,要加载GIF图像并将其显示在富文本中,我们可以采取以下步骤: 1. 首先,我们需要获取GIF图像文件的URL或路径。可以从互联网上下载或从应用程序的资源文件中获取。例如,如果GIF图像保存在应用程序的资源文件中,则可以使用`Bundle.main.url(forResource: "myGif", withExtension: "gif")`方法获取该文件的URL。 2. 接下来,我们需要将GIF图像文件加载到`Data`对象中,以便将其与`NSAttributedString`富文本一起使用。我们可以使用`Data(contentsOf: gifURL)`方法将URL转换为Data对象。 3. 然后,我们可以创建一个`NSTextAttachment`对象,并将GIF图像数据设置为其`image`属性。例如,可以使用`NSTextAttachment(image: UIImage(data: gifData)!)`来创建`NSTextAttachment`对象。 4. 然后,我们可以使用`NSAttributedString`的`append`方法将`NSTextAttachment`对象添加到富文本中,并设置其适当的位置。例如,可以使用`attributedString.append(NSAttributedString(attachment: textAttachment))`语句将`NSTextAttachment`对象添加到`attributedString`富文本字符串中。 5. 最后,我们可以将包含GIF图像的富文本字符串应用于文本视图或标签等UI元素,以便在界面上显示GIF图像。例如,对于UILabel,可以使用`label.attributedText = attributedString`将富文本字符串应用于标签。 综上所述,以上是利用iOS富文本加载GIF的简要步骤。通过将GIF图像文件加载到`Data`对象中,然后将其作为`NSTextAttachment`对象添加到富文本字符串中,我们可以在富文本中显示GIF图像,并将其应用于iOS界面中的相应UI元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值