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

时间:2018-11-14 14:21:53

标签: swift uilabel uiedgeinsets

我正在以编程方式设置UILabel的样式,无法在标签文本周围填充任何内容:

enter image description here

在研究了关于该主题的许多SO线程(特别是this one之后),我以为我找到了一种解决方案,但它对标签没有影响。我为子类UILabel的标签创建了一个自定义类:

// 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
}

就填充/插入而言绝对不起作用。我简单地希望在文本和边框之间的左/右空间为6pts,在顶部/底部为4pts。任何帮助,不胜感激。

2 个答案:

答案 0 :(得分:2)

下面的代码可以满足您的要求(创建新的 LiveView 游乐场并将其粘贴)。

但是我仅出于演示目的而提供,并认为将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()

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

答案 1 :(得分:-2)

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

titleLabel.text =“ \(itemName)”