Last active
February 22, 2024 13:24
-
-
Save konnnn/d43af3bd525bb4c58f5c29fb14575b0d to your computer and use it in GitHub Desktop.
Swift 4: Adding space/padding to a UILabel Programmatically and Storyboard
This file contains 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
/* | |
If you use Storyboard, don't forget to set UIlabel to PaddingLabel | |
*/ | |
import UIKit | |
class PaddingLabel: UILabel { | |
var topInset: CGFloat | |
var bottomInset: CGFloat | |
var leftInset: CGFloat | |
var rightInset: CGFloat | |
required init(withInsets top: CGFloat, _ bottom: CGFloat, _ left: CGFloat, _ right: CGFloat) { | |
self.topInset = top | |
self.bottomInset = bottom | |
self.leftInset = left | |
self.rightInset = right | |
super.init(frame: CGRect.zero) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func drawText(in rect: CGRect) { | |
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset) | |
super.drawText(in: rect.inset(by: insets)) | |
} | |
override var intrinsicContentSize: CGSize { | |
get { | |
var contentSize = super.intrinsicContentSize | |
contentSize.height += topInset + bottomInset | |
contentSize.width += leftInset + rightInset | |
return contentSize | |
} | |
} | |
} | |
// Usage | |
import UIKit | |
class ViewController: UIViewController { | |
public static var label: UILabel = { | |
let label = PaddingLabel(withInsets: 8, 8, 18, 18) | |
label.text = "Some text" | |
label.textColor = .white | |
label.textAlignment = .center | |
label.font = UIFont.systemFont(ofSize: 26, weight: UIFont.Weight.regular) | |
label.layer.backgroundColor = UIColor.black.withAlphaComponent(0.14).cgColor | |
label.layer.cornerRadius = 24 | |
return label | |
}() | |
} | |
// Version 2 12.04.2020 | |
class PaddingLabel: UILabel { | |
var insets = UIEdgeInsets.zero | |
/// Добавляет отступы | |
func padding(_ top: CGFloat, _ bottom: CGFloat, _ left: CGFloat, _ right: CGFloat) { | |
self.frame = CGRect(x: 0, y: 0, width: self.frame.width + left + right, height: self.frame.height + top + bottom) | |
insets = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right) | |
} | |
override func drawText(in rect: CGRect) { | |
super.drawText(in: rect.inset(by: insets)) | |
} | |
override var intrinsicContentSize: CGSize { | |
get { | |
var contentSize = super.intrinsicContentSize | |
contentSize.height += insets.top + insets.bottom | |
contentSize.width += insets.left + insets.right | |
return contentSize | |
} | |
} | |
} | |
// Применение | |
class ViewController: UIViewController { | |
@IBOutlet weak var label: PaddingLabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
label.text = "Какой-то текст" | |
label.textColor = UIColor.white | |
label.padding(2, 2, 5, 5) | |
label.layer.cornerRadius = 5.0 | |
label.layer.backgroundColor = UIColor.red | |
} | |
} | |
/* | |
If you use Storyboard, don't forget to set UIlabel to PaddingLabel | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Swift 4 (Apr. 11, 2021), I had to use:
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
instead of:
super.drawText(in: rect.inset(by: insets))