Skip to content

Instantly share code, notes, and snippets.

@konnnn
Last active February 22, 2024 13:24
Show Gist options
  • Save konnnn/d43af3bd525bb4c58f5c29fb14575b0d to your computer and use it in GitHub Desktop.
Save konnnn/d43af3bd525bb4c58f5c29fb14575b0d to your computer and use it in GitHub Desktop.
Swift 4: Adding space/padding to a UILabel Programmatically and Storyboard
/*
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
*/
@GabrieleCicconetti
Copy link

Thank you :)

@DevFatani
Copy link

😍

@yoosa3004
Copy link

thank you

@paulmaxgithub
Copy link

It works!

@MillerMedia
Copy link

MillerMedia commented Apr 12, 2021

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))

@kiku-991
Copy link

thank u

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