如何在 UILabel 的文本周围创建填充?

How do I create padding around the text of a UILabel?

我正在以编程方式设置 UILabel 的样式,但在获取标签文本周围的任何填充时遇到问题:

在研究了关于该主题的许多 SO 线程(特别是 this one)之后,我认为我找到了解决方案,但它对标签没有影响。我为 subclasses UILabel:

的标签创建了自定义 class
// Swift 3 (I know, I know...)

class PublicationTypeBadge: UILabel {
    var textInsets = UIEdgeInsets.zero {
        didSet { invalidateIntrinsicContentSize() }
    }

    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
        let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
        let invertedInsets = UIEdgeInsets(top: -textInsets.top,
                                          left: -textInsets.left,
                                          bottom: -textInsets.bottom,
                                          right: -textInsets.right)
        return UIEdgeInsetsInsetRect(textRect, invertedInsets)
    }

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

我这样调用它:

func setPubTypeBadge(publication: PublicationModel, indexPathRow: Int) {
    if let pubType = publication.publicationType,
        let pubEnum = PublicationType(rawValue: pubType) {
        pubTypeBadge.attributedText = pubEnum.getBadgeLabel(using: pubType)
    }

    let rect = pubTypeBadge.textRect(forBounds: pubTypeBadge.frame, limitedToNumberOfLines: 1)
    pubTypeBadge.drawText(in: rect)
    pubTypeBadge.layer.borderColor = UIColor.dsShade3.cgColor
    pubTypeBadge.layer.borderWidth = 1
    pubTypeBadge.layer.cornerRadius = 4
}

就 padding/insets 而言,它绝对没有任何作用。我只想在文本和边框之间的 left/right 上使用 6pts space,在 top/bottom 上使用 4pts。非常感谢任何帮助。

下面的代码可以满足您的需求(创建新的 LiveView playground 并粘贴)。

但是我提供它只是为了演示,并认为将 UILabel 放入容器中并进行适当的约束将是完成此任务的合适解决方案。

导入 UIKit 导入 PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let label = BorderedLabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.text = "Hello World!"
        label.textColor = .black
        // Border settings
        label.layer.cornerRadius = 4 
        label.layer.borderColor = UIColor.black.cgColor
        label.layer.borderWidth = 1
        label.sizeToFit() // Important

        view.addSubview(label)
        self.view = view
    }
}

class BorderedLabel: UILabel {
    var sidePadding = CGFloat(10) // Needed padding, add property observers

    override func sizeToFit() {
        super.sizeToFit()
        bounds.size.width += 2 * sidePadding
    }

    override func drawText(in rect: CGRect) {
        print(rect)
        super.drawText(in: rect.insetBy(dx: sidePadding, dy: 0))
        invalidateIntrinsicContentSize()
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

但是您可以尝试并测试此解决方案或提高便利性和可靠性(以及自动布局支持)

您可以在文本前后添加空白 space:

titleLabel.text = " \(itemName) "