gpt4 book ai didi

ios - 向 UILabel 添加空格/填充

转载 作者:IT王子 更新时间:2023-10-29 07:27:00 24 4
gpt4 key购买 nike

我有一个 UILabel,我想在其中添加顶部和底部的空间。在限制最小高度的情况下,我将其修改为:

Enter image description here

为此我使用了:

override func drawTextInRect(rect: CGRect) {
var insets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}

但我必须找到不同的方法,因为如果我写多于两行,问题是一样的:

Enter image description here

最佳答案

我已经在 Swift 4.2 上试过了,希望它对你有用!

@IBDesignable class PaddingLabel: UILabel {

@IBInspectable var topInset: CGFloat = 5.0
@IBInspectable var bottomInset: CGFloat = 5.0
@IBInspectable var leftInset: CGFloat = 7.0
@IBInspectable var rightInset: CGFloat = 7.0

override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: rect.inset(by: insets))
}

override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + leftInset + rightInset,
height: size.height + topInset + bottomInset)
}

override var bounds: CGRect {
didSet {
// ensures this works within stack views if multi-line
preferredMaxLayoutWidth = bounds.width - (leftInset + rightInset)
}
}
}

或者您可以在这里使用 CocoaPods https://github.com/levantAJ/PaddingLabel

pod 'PaddingLabel', '1.2'

enter image description here

关于ios - 向 UILabel 添加空格/填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com