ZLTextView.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // ZLTextView.swift
  3. // O2Platform
  4. //
  5. // Created by 程剑 on 2017/7/13.
  6. // Copyright © 2017年 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. extension UITextView {
  10. //添加链接文本(链接为空时则表示普通文本)
  11. func appendLinkString(string:String, withURLString:String = "") {
  12. //原来的文本内容
  13. let attrString:NSMutableAttributedString = NSMutableAttributedString()
  14. attrString.append(self.attributedText)
  15. //新增的文本内容(使用默认设置的字体样式)
  16. let textFont = UIFont(name: "PingFangTC-Regular", size: 16.0)
  17. let attrs = [NSAttributedString.Key.foregroundColor : base_color, NSAttributedString.Key.font : textFont!]
  18. let appendString = NSMutableAttributedString(string: string, attributes:attrs)
  19. //判断是否是链接文字
  20. if withURLString != "" {
  21. let range:NSRange = NSMakeRange(0, appendString.length)
  22. appendString.beginEditing()
  23. appendString.addAttribute(NSAttributedString.Key.link, value:withURLString, range:range)
  24. appendString.endEditing()
  25. }
  26. //合并新的文本
  27. attrString.append(appendString)
  28. //设置合并后的文本
  29. self.attributedText = attrString
  30. }
  31. }