Created
December 21, 2017 20:21
-
-
Save projectxcappe/9d037aaf89dcebbdcdeb005419818334 to your computer and use it in GitHub Desktop.
Dynamic UILabel with Rounded Background
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
| 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 | |
| } | |
| } | |
| } |
Author
projectxcappe
commented
Dec 21, 2017

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