Extension+NSMutableAttributedString.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Extension+NSMutableAttributedString.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2018/10/19.
  6. // Copyright © 2018 南鑫林. All rights reserved.
  7. //
  8. import UIKit
  9. extension NSMutableAttributedString {
  10. // MARK: 完全自定义样式
  11. public func changeAttributeDict(_ dict: [AnyHashable : Any]?, range: NSRange) {
  12. if let dict = dict as? [NSAttributedString.Key : Any] {
  13. addAttributes(dict, range: range)
  14. }
  15. }
  16. // MARK: 设置字体颜色,取值为 UIColor,默认为黑色
  17. public func changeForegroundColor(_ color: UIColor?, range: NSRange) {
  18. addAttribute(.foregroundColor, value: color!, range: range)
  19. }
  20. // MARK: 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
  21. //#warning NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,跟前面介绍的 textColor 一样,哪个属性最后一次赋值,就会冲掉前面的效果
  22. public func changeBackgroundColor(_ color: UIColor?, range: NSRange) {
  23. addAttribute(.backgroundColor, value: color!, range: range)
  24. }
  25. // MARK: 改变某位置 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
  26. public func changeSystemFont(_ font: UIFont?, range: NSRange) {
  27. addAttribute(.font, value: font!, range: range)
  28. }
  29. // MARK: 设置字形倾斜度,取值为 NSNumber(float),正值右倾,负值左倾
  30. public func changeObliquenessValue(_ value: NSNumber?, range: NSRange) {
  31. addAttribute(.obliqueness, value: value!, range: range)
  32. }
  33. // MARK: NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本 在 iOS 中,总是使用横排文本,0 以外的值都未定义
  34. public func changeVerticalGlyphFormValue(_ value: NSNumber?, range: NSRange) {
  35. addAttribute(.verticalGlyphForm, value: value!, range: range)
  36. }
  37. // MARK: NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
  38. public func changeWritingDirectionStyle(_ style: NSWritingDirection) {
  39. addAttribute(.writingDirection, value: [NSWritingDirection.rightToLeft], range: NSRange(location: 0, length: length))
  40. }
  41. // MARK: 改变某位置的行距
  42. public func changeLineSpacing(_ spacing: CGFloat, from loc: Int, length: Int) {
  43. let range = NSRange(location: loc, length: length)
  44. let style = NSMutableParagraphStyle()
  45. // 行距
  46. style.lineSpacing = spacing
  47. addAttribute(.paragraphStyle, value: style, range: range)
  48. }
  49. // MARK: 改变某位置的段落距离
  50. public func changeParagraphSpacing(_ spacing: CGFloat, from loc: Int, length: Int) {
  51. let range = NSRange(location: loc, length: length)
  52. let style = NSMutableParagraphStyle()
  53. // 段落距离
  54. style.paragraphSpacing = spacing
  55. addAttribute(.paragraphStyle, value: style, range: range)
  56. }
  57. // MARK: 改变段落的顶部和文本内容的开头之间的距离
  58. public func change(beforeLparagraphSpacing spacing: CGFloat, from loc: Int, length: Int) {
  59. let range = NSRange(location: loc, length: length)
  60. let style = NSMutableParagraphStyle()
  61. // 段落的顶部和文本内容的开头之间的距离
  62. style.paragraphSpacingBefore = spacing
  63. addAttribute(.paragraphStyle, value: style, range: range)
  64. }
  65. // MARK: 设置下划线样式:根据枚举选择
  66. public func change(_ style: NSUnderlineStyle, color: UIColor?, range: NSRange) {
  67. addAttribute(.underlineStyle, value: style.rawValue, range: range)
  68. addAttribute(.underlineColor, value: color ?? "", range: range)
  69. }
  70. // MARK: 全部加下划线 设置下划线样式:根据枚举选择
  71. public func changeUnderline(atAllStyle style: NSUnderlineStyle, color: UIColor?) {
  72. let range = NSRange(location: 0, length: length)
  73. change(style, color: color, range: range)
  74. }
  75. // MARK: 设置删除线样式:根据枚举选择
  76. public func changeStrikethroughStyle(_ style: NSUnderlineStyle, color: UIColor?, range: NSRange) {
  77. addAttribute(.strikethroughStyle, value: style.rawValue, range: range)
  78. addAttribute(.strikethroughColor, value: color ?? "", range: range)
  79. }
  80. // MARK: 全部添加删除线 设置删除线样式:根据枚举选择
  81. public func changeStrikethrough(atAllStyle style: NSUnderlineStyle, color: UIColor?) {
  82. let range = NSRange(location: 0, length: length)
  83. changeStrikethroughStyle(style, color: color, range: range)
  84. }
  85. // MARK: 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
  86. public func changeKern(withValue value: NSNumber?, range: NSRange) {
  87. addAttribute(.kern, value: value ?? "", range: range)
  88. }
  89. // MARK: 根据位置设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
  90. public func changeExpansion(withValue value: NSNumber?, range: NSRange) {
  91. addAttribute(.expansion, value: value ?? "", range: range)
  92. }
  93. // MARK: 根据位置添加阴影效果
  94. public func changeShadow(with shadow: NSShadow?, range: NSRange) {
  95. addAttribute(.shadow, value: shadow ?? "", range: range)
  96. // 排版
  97. addAttribute(.verticalGlyphForm, value: NSNumber(value: 0), range: range)
  98. }
  99. // MARK: 根据位置修改描边颜色 垂直标志符号形式
  100. public func changeStrokeColor(with strokeColor: UIColor?, strokeWidth: NSNumber?, range: NSRange) {
  101. addAttribute(.strokeColor, value: strokeColor ?? "", range: range)
  102. addAttribute(.strokeWidth, value: strokeWidth ?? "", range: range)
  103. }
  104. /// 实例化 NSMutableAttributedString 字符串
  105. ///
  106. /// - Parameters:
  107. /// - texts: 用于存储所需设置字符串的数组(根据所需自定义类型分组)
  108. /// - attrsArray: 富文本属性样式的集合(数组元素是texts的相应文本的属性--字典)
  109. /// - space: texts文本之间的间距数组 1 代表一个空格的距离
  110. /// - Returns: NSMutableAttributedString
  111. public convenience init(strings texts: [String]?, attributesArray attrsArray: [[String : Any?]]?, space: [NSNumber]?) {
  112. self.init()
  113. go(texts, attributesArray: attrsArray, space: space)
  114. }
  115. /// 实例化 NSMutableAttributedString 字符串
  116. ///
  117. /// - Parameters:
  118. /// - texts: 用于存储所需设置字符串的数组(根据所需自定义类型分组)
  119. /// - attrsArray: 富文本属性样式的集合(数组元素是texts的相应文本的属性--字典)
  120. /// - space: texts文本之间的间距数组 1 代表一个空格的距离
  121. /// - Returns: NSMutableAttributedString
  122. public func go(_ textArray: [Any]?, attributesArray attrsArray: [Any]?, space: [NSNumber]?) {
  123. (textArray as NSArray?)?.enumerateObjects({[weak self] obj, idx, stop in
  124. let attrsCount: Int? = attrsArray?.count
  125. if idx <= ((attrsCount ?? 0) - 1) {
  126. self?.append(NSAttributedString(string: obj as? String ?? "", attributes: attrsArray?[(attrsCount ?? 0) - 1] as? [NSAttributedString.Key : Any]))
  127. } else {
  128. self?.append(NSAttributedString(string: obj as? String ?? "", attributes: attrsArray?[(attrsCount ?? 0) - 1] as? [NSAttributedString.Key : Any]))
  129. }
  130. if idx != (textArray?.count ?? 0) - 1 && idx <= (space?.count ?? 0) - 1 {
  131. if let space = self?.spaceWidth(withNumberBlackSpace: space?[idx]) {
  132. self?.append(space)
  133. }
  134. } else {
  135. if let space = self?.spaceWidth(withNumberBlackSpace: nil) {
  136. self?.append(space)
  137. }
  138. }
  139. })
  140. }
  141. /// 字间距
  142. ///
  143. /// - Parameter number: 空格
  144. /// - Returns: NSMutableAttributedString
  145. public func spaceWidth(withNumberBlackSpace number: NSNumber?) -> NSMutableAttributedString? {
  146. var string = ""
  147. let count: Int = number?.intValue ?? 0
  148. for _ in 0..<count {
  149. string = string + (" ")
  150. }
  151. let attributedString = NSMutableAttributedString(string: string)
  152. return attributedString
  153. }
  154. }