https://developer.apple.com/documentation/uikit/uiedgeinsets
UILabel
UILabelのsub classを定義して、初期化時にinsetを渡すdrawText methodでinsetを適用
class IndentLabel: UILabel {
private let insets: UIEdgeInsets
init(insets: UIEdgeInsets, frame: CGRect) {
self.insets = insets
super.init(frame: frame)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: insets))
}
}
UIImageView
let insets = UIEdgeInsets(top: -4, left: -4, bottom: -4, right: -4)
let imageView = UIImageView(image: UIImage().withAlignmentRectInsets(insets))
UIButton
UIButtonにimageをセットした時のimage inset、及びUIButton自体のinset設定let button = UIButton(frame: CGRect())
let img = UIImage()
btn.setImage(img, for: .normal)
btn.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
btn.contentEdgeInsets = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 4)