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

自己重写一个label class JJLabel: UILabel {public var textInsets: UIEdgeInsets = .zerooverride 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)

    推荐阅读