富文本

1.普通富文本:
(1)富文本创建 & 赋值:

let content = "1、免费解锁所选任教学段内的全部绘本。\n2、可申请加入分级阅读教师交流圈"
let attributedString = NSMutableAttributedString(string: content)

self.contentTextView.attributedText=attributedString

(2)获取range:

let range0 = NSMakeRange(0, attributedString.length)
let range1 = attributedString.mutableString.range(of: "免费解锁")
let range2 = attributedString.mutableString.range(of: "全部绘本")
let range3 = NSMakeRange(28, attributedString.length - 28)

(3)常用样式:

attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range: range0)//字体大小
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.hexColor(0xfd7d08), range: range2)//设置文字颜色
attributedString.addAttribute(NSKernAttributeName, value: 10, range: range1)//字间距

(4)设置段落样式(行间距、段间距、首行缩进...等):

let paraph = NSMutableParagraphStyle()
paraph.lineSpacing = 10//行间距
paraph.paragraphSpacing = 20//段间距
paraph.firstLineHeadIndent = 40//首行缩进
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paraph, range: range0)

(5)文字中添加图片:

let attachment = NSTextAttachment(data: nil, ofType: nil)
attachment.image = UIImage.init(named: "TSafe_3")
attachment.bounds = CGRect.init(x: 0, y: 0, width: 20, height: 20)
attributedString.addAttribute(NSAttachmentAttributeName, value: attachment, range: range0)//文字中添加图片(自测没有效果)

(6)其他:

attributedString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: range1)//下划线
attributedString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.blue, range: range1)//下划线颜色

attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: range2)//删除线
attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.red, range: range2)//删除线颜色

attributedString.addAttribute(NSLigatureAttributeName, value: 1, range: range3)//连体字(自测没有效果)

attributedString.addAttribute(NSBaselineOffsetAttributeName, value: 10, range: range2)//基准线偏移

attributedString.addAttribute(NSLinkAttributeName, value: "www.baidu.com", range: range1)//链接(自测没有效果)
//遵守UITextViewDelegate代理之后,实现此方法
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
    if URL.scheme == "www.baidu.com" {
        let viewController = AJGetTopViewControllerTool.topViewController()
        viewController?.navigationController?.pushViewController(TValidateController(), animated: true)
        return false
    }
    return true
}

2.带有html标签的富文本:

let contenString = "恭喜您已成功注册!可免费试用<font size=\"20\" color=\"red\"> 10 </font>天,体验全套绘本哦!"

do {
    let attributedString = try NSAttributedString.init(data: (contenString?.data(using: String.Encoding.unicode))!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
    
    self.contentTextView.attributedText = attributedString
} catch { }
  • 注:
    当 self.contentTextView.attributedText=attributedString 之后(在设置attributedText之后),会覆盖掉对contentTextView之前相关属性(比如 font、textColor等属性)的设置!这也就是为什么有时候单独设置了属性,不生效的原因,需要在赋 attributedText 值之后,重新设置一下!
self.contentTextView.attributedText=attributedString
self.contentTextView.font = UIFont.systemFont(ofSize: 13)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容