0
点赞
收藏
分享

微信扫一扫

iOS小技能:富文本&图文混排

i奇异 2022-08-17 阅读 93

引言

  1. 封装富文本API,采用block实现链式编程
  2. 超链接属性
  3. HTML字符串与富文本互转
  4. 创建带有图片的富文本

I、基础知识&工具

1.1 封装富文本API,采用block实现链式编程

​​iOS开发效率工具:完整SDK源码【封装富文本API,采用block实现链式编程】(block 的妙用:结合block和方法的优点实现iOS的链式编程)​​


  • 部分API

iOS小技能:富文本&图文混排_富文本

  • 用法例子

NSMutableAttributedString *xx  = [[NSMutableAttributedString alloc]init];


xx.kn_addString(@"封装富文本API").kn_fontColor(UIColor.redColor).kn_addString(@"采用block实现链式编程").kn_fontColor(UIColor.blueColor).kn_addString(@"!");

iOS小技能:富文本&图文混排_富文本_02

1.2 属性

属性

用途

类型

NSFontAttributeName

字号

UIFont 默认12

NSParagraphStyleAttributeName

段落样式

NSParagraphStyle

NSForegroundColorAttributeName

前景色

UIColor

NSBackgroundColorAttributeName

背景色

UIColor

NSObliquenessAttributeName

字体倾斜

NSNumber

NSExpansionAttributeName

字体加粗

NSNumber 比例 0就是不变 1增加一倍

NSKernAttributeName

字间距

CGFloat

NSUnderlineStyleAttributeName

下划线

1或0

NSUnderlineColorAttributeName

下划线颜色

UIColor

NSStrikethroughStyleAttributeName

删除线

1或0

NSStrikethroughColorAttributeName

删除线颜色

UIColor

NSStrokeColorAttributeName

same as ForegroundColor

UIColor

NSStrokeWidthAttributeName

字体描边

CGFloat

NSLigatureAttributeName

连笔字

1或0

NSShadowAttributeName

阴影

NSShawdow

NSTextEffectAttributeName

设置文本特殊效果,目前只有图版印刷效果可用

NSString

NSAttachmentAttributeName

​设置文本附件,常用插入图片​

NSTextAttachment

NSLinkAttributeName

链接

NSURL (preferred) or NSString

NSBaselineOffsetAttributeName

​基准线偏移​

NSNumber,可用于布局

NSWritingDirectionAttributeName

文字方向

分别代表不同的文字出现方向@[@(1),@(2)]

NSVerticalGlyphFormAttributeName

水平或者竖直文本

1竖直 0水平

II、超链接属性的应用案例

  • ​​iOS 自定义视图:《用户协议及隐私政策》弹框(包含超链接属性)【本文包含完整demo源码,demo支持中英文切换】​​
​4.1、自定义TextView,采用富文本属性进行内容设置attributedText(包括下划线NSUnderlineStyleSingle、超链接NSLinkAttributeName 、颜色NSForegroundColorAttributeName 等信息) 4.2、实现代理方法textView:shouldInteractWithURL:inRange,处理点击超链接的回调(打开对应URL Webview)

III、HTML字符串与富文本互转

  • html->NSAttributedString

NSString *html = @"<p style='color:green'>博客<span style='color:#e83c36;'><a>https://kunnan.blog.csdn.net/<a/></span><br/>微信公众号:<span style='color:red'>iOS逆向</span><br/>“订阅”一次 ,享受终身服务的快乐。</br><span style='color:red'>专注《iOS应用逆向与安全》</span>(包括iOS基础)</p>";
NSAttributedString *attStr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];

IV、创建带有图片的富文本

iOS图文混排之【 NSAttachmentAttributeName 创建带有图片的富文本】(案例:展示信用卡标签)

V、富文本在适配系统API的应用例子

5.1 适配iOS13UI控件UITextField的_placeholderLabel 私有API

  • 问题:​​iOS13无法直接访问_placeholderLabel.textColor​​
  • 解决方案:​​使用 NSMutableAttributedString 富文本attributedPlaceholder来替代KVC访问 UITextField 的 _placeholderLabel。​

+ (void)setupUITextField4attributedPlaceholder:(UITextField*)textField{

textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"placeholder" attributes:@{NSForegroundColorAttributeName:kTextPlaceholderColor, NSFontAttributeName:kTextFont(13) }];
}

举报

相关推荐

0 条评论