Skip to content

Instantly share code, notes, and snippets.

@projectxcappe
Created December 21, 2017 20:21
Show Gist options
  • Select an option

  • Save projectxcappe/9d037aaf89dcebbdcdeb005419818334 to your computer and use it in GitHub Desktop.

Select an option

Save projectxcappe/9d037aaf89dcebbdcdeb005419818334 to your computer and use it in GitHub Desktop.
Dynamic UILabel with Rounded Background
class LabelWithBgImage : UILabel {
var topInset: CGFloat = 0.0
var bottomInset: CGFloat = 0.0
var leftInset: CGFloat = 10.0
var rightInset: CGFloat = 10.0
public override init(frame: CGRect) {
super.init(frame: frame)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
layer.cornerRadius = self.frame.size.height/2
layer.masksToBounds = true
self.numberOfLines = 0
self.adjustsFontForContentSizeCategory = true
self.lineBreakMode = .byWordWrapping
self.backgroundColor = UIColor(patternImage: UIImage(named: "divider_fade")!)
}
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override var intrinsicContentSize: CGSize {
get {
var contentSize = super.intrinsicContentSize
contentSize.height += topInset + bottomInset
contentSize.width += leftInset + rightInset
return contentSize
}
}
}
@projectxcappe
Copy link
Author

screen shot 2017-12-21 at 1 21 47 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment