我是一个
Android开发人员学习iOS.如何在iOS Swift中为UILabel添加填充(插入)?我没有成功找到一个简单的教程.谢谢.
解决方法
这是Swift 3中需要添加的代码:
class InsetLabel: UILabel { let topInset = CGFloat(0) let bottomInset = CGFloat(0) let leftInset = CGFloat(20) let rightInset = CGFloat(20) override func drawText(in rect: CGRect) { let insets: UIEdgeInsets = UIEdgeInsets(top: topInset,left: leftInset,bottom: bottomInset,right: rightInset) super.drawText(in: UIEdgeInsetsInsetRect(rect,insets)) } override public var intrinsicContentSize: CGSize { var intrinsicSuperViewContentSize = super.intrinsicContentSize intrinsicSuperViewContentSize.height += topInset + bottomInset intrinsicSuperViewContentSize.width += leftInset + rightInset return intrinsicSuperViewContentSize } }