iOS开发笔记-66:swift4-UILabel设置内边距te

2017-12-13  本文已影响107人  原味蛋炒饭
自己重写一个label
class JJLabel: UILabel {

    public var textInsets: UIEdgeInsets = .zero

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    internal init(frame: CGRect, textInsets: UIEdgeInsets) {
        self.textInsets = textInsets
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, textInsets))
    }

}

使用:
beginTitle = JJLabel(frame: CGRect(x: 13, y: 114 + height!, width: 230, height: 44))
beginTitle?.attributedText = NSAttributedString(string: "开始日期   请选择开始日期", attributes: [NSAttributedStringKey.foregroundColor: kMainColor33, NSAttributedStringKey.font: FONT13])

beginTitle?.textInsets = UIEdgeInsets(top: 0, left: 13, bottom: 0, right: 13)
上一篇 下一篇

猜你喜欢

热点阅读