POPMUISE
我通过子类化UILabel和覆盖解决了这个问题drawTextInRect::- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];}Swift 3.1:override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))}Swift 4.2.1:override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
super.drawText(in: rect.inset(by: insets))}正如你可能已经聚集的那样,这是对tc。答案的改编。它有两个优点:没有必要通过发送sizeToFit消息来触发它它会单独留下标签框 - 如果您的标签有背景并且您不希望它缩小,则很方便