Skip to content

Instantly share code, notes, and snippets.

@thilong
Created March 18, 2020 15:07
Show Gist options
  • Save thilong/397dde8661df940b20a4622cf12b554a to your computer and use it in GitHub Desktop.
Save thilong/397dde8661df940b20a4622cf12b554a to your computer and use it in GitHub Desktop.
支持设置内边距的UILabel
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