NSMutableAttributedString+Extensions.swift 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // NSMutableAttributedString+Extensions.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2019 Nathan Tannar.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. // Created by Nathan Tannar on 8/25/17.
  26. //
  27. import UIKit
  28. internal extension NSMutableAttributedString {
  29. @discardableResult
  30. func bold(_ text: String, fontSize: CGFloat = UIFont.preferredFont(forTextStyle: .body).pointSize, textColor: UIColor = .black) -> NSMutableAttributedString {
  31. let attrs: [NSAttributedString.Key:AnyObject] = [
  32. NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: fontSize),
  33. NSAttributedString.Key.foregroundColor : textColor
  34. ]
  35. let boldString = NSMutableAttributedString(string: text, attributes: attrs)
  36. self.append(boldString)
  37. return self
  38. }
  39. @discardableResult
  40. func medium(_ text: String, fontSize: CGFloat = UIFont.preferredFont(forTextStyle: .body).pointSize, textColor: UIColor = .black) -> NSMutableAttributedString {
  41. let attrs: [NSAttributedString.Key:AnyObject] = [
  42. NSAttributedString.Key.font : UIFont.systemFont(ofSize: fontSize, weight: UIFont.Weight.medium),
  43. NSAttributedString.Key.foregroundColor : textColor
  44. ]
  45. let mediumString = NSMutableAttributedString(string: text, attributes: attrs)
  46. self.append(mediumString)
  47. return self
  48. }
  49. @discardableResult
  50. func italic(_ text: String, fontSize: CGFloat = UIFont.preferredFont(forTextStyle: .body).pointSize, textColor: UIColor = .black) -> NSMutableAttributedString {
  51. let attrs: [NSAttributedString.Key:AnyObject] = [
  52. NSAttributedString.Key.font : UIFont.italicSystemFont(ofSize: fontSize),
  53. NSAttributedString.Key.foregroundColor : textColor
  54. ]
  55. let italicString = NSMutableAttributedString(string: text, attributes: attrs)
  56. self.append(italicString)
  57. return self
  58. }
  59. @discardableResult
  60. func normal(_ text: String, fontSize: CGFloat = UIFont.preferredFont(forTextStyle: .body).pointSize, textColor: UIColor = .black) -> NSMutableAttributedString {
  61. let attrs:[NSAttributedString.Key:AnyObject] = [
  62. NSAttributedString.Key.font : UIFont.systemFont(ofSize: fontSize),
  63. NSAttributedString.Key.foregroundColor : textColor
  64. ]
  65. let normal = NSMutableAttributedString(string: text, attributes: attrs)
  66. self.append(normal)
  67. return self
  68. }
  69. }
  70. internal extension NSAttributedString {
  71. func replacingCharacters(in range: NSRange, with attributedString: NSAttributedString) -> NSMutableAttributedString {
  72. let ns = NSMutableAttributedString(attributedString: self)
  73. ns.replaceCharacters(in: range, with: attributedString)
  74. return ns
  75. }
  76. static func += (lhs: inout NSAttributedString, rhs: NSAttributedString) {
  77. let ns = NSMutableAttributedString(attributedString: lhs)
  78. ns.append(rhs)
  79. lhs = ns
  80. }
  81. static func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSAttributedString {
  82. let ns = NSMutableAttributedString(attributedString: lhs)
  83. ns.append(rhs)
  84. return NSAttributedString(attributedString: ns)
  85. }
  86. }