Skip to content

Instantly share code, notes, and snippets.

@roman-wb
Created February 1, 2019 13:31
Show Gist options
  • Select an option

  • Save roman-wb/3cdd034a927ddc28a288b3a339a667ee to your computer and use it in GitHub Desktop.

Select an option

Save roman-wb/3cdd034a927ddc28a288b3a339a667ee to your computer and use it in GitHub Desktop.
IBDesignable for UILabel (including kerning attributed)
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