Created
March 18, 2020 15:07
-
-
Save thilong/397dde8661df940b20a4622cf12b554a to your computer and use it in GitHub Desktop.
支持设置内边距的UILabel
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
import UIKit | |
class Label: UILabel { | |
var contentInset: UIEdgeInsets = .zero { | |
didSet { | |
invalidateIntrinsicContentSize() | |
setNeedsLayout() | |
} | |
} | |
override func drawText(in rect: CGRect) { | |
super.drawText(in: UIEdgeInsetsInsetRect(rect, contentInset)) | |
} | |
override var intrinsicContentSize: CGSize { | |
var size = super.intrinsicContentSize | |
size.height += contentInset.top + contentInset.bottom | |
size.width += contentInset.left + contentInset.right | |
return size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment