NSMutableAttributedString+Extensions.swift 659 B

1234567891011121314151617181920212223
  1. //
  2. // UI+Extensions.swift
  3. // Nextcloud
  4. //
  5. // SPDX-FileCopyrightText: 2021 Marino Faggiana <marino.faggiana@nextcloud.com>
  6. // SPDX-License-Identifier: GPL-3.0-or-later
  7. //
  8. import Foundation
  9. import UIKit
  10. extension NSMutableAttributedString {
  11. func setColor(color: UIColor, font: UIFont? = nil, forText stringValue: String) {
  12. let range: NSRange = self.mutableString.range(of: stringValue, options: .caseInsensitive)
  13. self.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
  14. if let font = font {
  15. self.addAttribute(NSAttributedString.Key.font, value: font, range: range)
  16. }
  17. }
  18. }