我是一个
Android开发人员学习iOS.如何在iOS Swift中为UILabel添加填充(插入)?我没有成功找到一个简单的教程.谢谢.
另外,如何在Swift中为UI元素添加边距?当我尝试下面,没有任何更新(一旦点击“添加约束”).
解决方法
这是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
}
}