Created
February 1, 2019 13:31
-
-
Save roman-wb/3cdd034a927ddc28a288b3a339a667ee to your computer and use it in GitHub Desktop.
IBDesignable for UILabel (including kerning attributed)
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
| import UIKit | |
| @IBDesignable class MyLabel: UILabel { | |
| @IBInspectable var kerning: Float { | |
| get { | |
| let string = text ?? "" | |
| var range = NSMakeRange(0, string.count) | |
| guard let kern = attributedText?.attribute(.kern, at: 0, effectiveRange: &range), | |
| let value = kern as? NSNumber | |
| else { | |
| return 0 | |
| } | |
| return value.floatValue | |
| } | |
| set { | |
| var attText: NSMutableAttributedString | |
| if let attributedText = attributedText { | |
| attText = NSMutableAttributedString(attributedString: attributedText) | |
| } else if let text = text { | |
| attText = NSMutableAttributedString(string: text) | |
| } else { | |
| attText = NSMutableAttributedString(string: "") | |
| } | |
| let range = NSMakeRange(0, attText.length) | |
| attText.addAttribute(.kern, value: NSNumber(value: newValue), range: range) | |
| self.attributedText = attText | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment