Created
June 28, 2020 11:14
-
-
Save Ajimi/b18079b203fab4055b60e9b9a91dcc4c to your computer and use it in GitHub Desktop.
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 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment