- Xcode: 11.3.1
- Swift: 5
class PaddingLabel: UILabel {
// paddingの値
let padding = UIEdgeInsets(top: 4, left: 16, bottom: 4, right: 16)
override func drawText(in rect: CGRect) {
let newRect = rect.inset(by: padding)
super.drawText(in: newRect)
}
override var intrinsicContentSize: CGSize {
var intrinsicContentSize = super.intrinsicContentSize
intrinsicContentSize.height += padding.top + padding.bottom
intrinsicContentSize.width += padding.left + padding.right
return intrinsicContentSize
}
}
@IBDesignable
class DesignableLabel: UILabel {
@IBInspectable var topInset: CGFloat = 5.0
@IBInspectable var bottomInset: CGFloat = 5.0
@IBInspectable var leftInset: CGFloat = 10.0
@IBInspectable var rightInset: CGFloat = 10.0
override func drawText(in rect: CGRect) {
let newRect = rect.inset(by: UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset))
super.drawText(in: newRect)
}
override var intrinsicContentSize: CGSize {
var contentSize = super.intrinsicContentSize
contentSize.height += topInset + bottomInset
contentSize.width += leftInset + rightInset
return contentSize
}
}
extension UIView {
// 角丸設定
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}