Created
July 15, 2020 02:35
-
-
Save hsleedevelop/b7e1f21e08f25756324968511340a123 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UILabel { | |
@objc | |
func set(underLineFor keyword: String) { | |
guard let text = self.text ?? self.attributedText?.string else { return } | |
let attributedString = self.attributedText.map(NSMutableAttributedString.init(attributedString:)) ?? NSMutableAttributedString(string: text) | |
let indices = attributedString.string.indicesOf(string: keyword) | |
let length = keyword.count | |
for index in indices { | |
attributedString.addAttribute(NSAttributedString.Key.underlineStyle , value: NSUnderlineStyle.single.rawValue, range: NSRange(location: index, length: length)) | |
} | |
attributedText = attributedString | |
} | |
@objc | |
func set(color: UIColor, forKeyword keyword: String) { | |
guard let text = self.text ?? self.attributedText?.string else { return } | |
let attributedString = self.attributedText.map(NSMutableAttributedString.init(attributedString:)) ?? NSMutableAttributedString(string: text) | |
let indices = attributedString.string.indicesOf(string: keyword) | |
let length = keyword.count | |
for index in indices { | |
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: NSRange(location: index, length: length)) | |
} | |
attributedText = attributedString | |
} | |
@objc | |
func set(font: UIFont, forKeyword keyword: String) { | |
guard let text = self.text ?? self.attributedText?.string else { return } | |
let attributedString = self.attributedText.map(NSMutableAttributedString.init(attributedString:)) ?? NSMutableAttributedString(string: text) | |
let indices = attributedString.string.indicesOf(string: keyword) | |
let length = keyword.count | |
for index in indices { | |
attributedString.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: index, length: length)) | |
} | |
attributedText = attributedString | |
} | |
func set(font: UIFont, color: UIColor, forKeyword keyword: String) { | |
guard let text = self.text ?? self.attributedText?.string else { return } | |
let attributedString = self.attributedText.map(NSMutableAttributedString.init(attributedString:)) ?? NSMutableAttributedString(string: text) | |
let indices = attributedString.string.indicesOf(string: keyword) | |
let length = keyword.count | |
for index in indices { | |
attributedString.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: index, length: length)) | |
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: NSRange(location: index, length: length)) | |
} | |
attributedText = attributedString | |
} | |
public func lines() -> Int { | |
guard let text = self.text ?? self.attributedText?.string, let cFont = self.font else { | |
return 0 | |
} | |
let constraintRect = CGSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude) | |
let boundingBox = text.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: cFont], context: nil) | |
let count = Int(lroundf(Float(boundingBox.height)) / lroundf(Float(font.lineHeight))) | |
return count | |
} | |
func set(lineHeight: CGFloat) { | |
guard let text = self.text ?? self.attributedText?.string else { return } | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.minimumLineHeight = lineHeight | |
paragraphStyle.alignment = self.textAlignment | |
let attribute = [.paragraphStyle: paragraphStyle] as [NSAttributedString.Key: Any] | |
let attributedString = self.attributedText.map(NSMutableAttributedString.init(attributedString:)) ?? NSMutableAttributedString(string: text) | |
attributedString.addAttributes(attribute, range: NSMakeRange(0, (self.text?.count)!)) | |
self.attributedText = attributedString | |
} | |
func set(letterSpacing: CGFloat) { | |
guard let text = self.text ?? self.attributedText?.string else { return } | |
let attributedString = self.attributedText.map(NSMutableAttributedString.init(attributedString:)) ?? NSMutableAttributedString(string: text) | |
attributedString.addAttribute(NSAttributedString.Key.kern, value: letterSpacing, range: NSMakeRange(0, (self.text?.count)!)) | |
self.attributedText = attributedString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment