`

Swift -> UILable 设置内边距 UIEdgeInsets , UITextField 设置内边距

 
阅读更多

 UILabel

 

class UILabel_2:UILabel{
    open var insetEdge:UIEdgeInsets!;
    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, insetEdge))
    }
}

 swift 4.0 更新

    class UILabel_2: UILabel {
        open var insetEdge:UIEdgeInsets!;
        override func drawText(in rect: CGRect) {
            super.drawText(in: rect.inset(by: insetEdge))
        }
    }

 

 

使用

let testText = UILabel_2(frame: CGRect(x: 10, y: 8, width: 20, height: 34));
testText.insetEdge = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 20)

 

 

UITextField

class UITextField_2:UITextField{
    open var paddingHor:CGFloat = 0;    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        self.leftView = UIView(frame: CGRect(x: 0, y: 0, width: paddingHor, height: 1));
        self.leftViewMode = .always
        self.rightView = UIView(frame: CGRect(x: 0, y: 0, width: paddingHor, height: 1));
        self.rightViewMode = .always
    }
}

 

 

 

分享到:
评论
1 楼 wu1238 2018-02-25  
2c2ab98030fc767cf89051f369a97c5a内边距使用记录

相关推荐

Global site tag (gtag.js) - Google Analytics