UILabel的子类化和internalContentSize,并且UIEdgeInsets截断了我的文本

问题描述 投票:1回答:1

这是我的子类:

class LabelWithBlackBackGround: UILabel {
var topInset = CGFloat(3)
var bottomInset = CGFloat(3)
var leftInset = CGFloat(15)
var rightInset = CGFloat(10)

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

override public var intrinsicContentSize: CGSize {

    var intrinsicSuperViewContentSize = super.intrinsicContentSize
    intrinsicSuperViewContentSize.height += topInset + bottomInset
    intrinsicSuperViewContentSize.width += leftInset + rightInset
    return intrinsicSuperViewContentSize
}

deinit {
}
}

问题是,当我有多行时,很多时候它会截断我的文本,就像这样:

enter image description here

可能是我的internalContentSize不正确,但是我找不到它出了什么问题。

当然,我尝试了那些:

label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
swift uilabel
1个回答
0
投票
我想您不再寻找答案了,但是我遇到了一个与您类似的问题,并且该问题已由本主题解决:

Resizing a UILabel to accommodate insets

© www.soinside.com 2019 - 2024. All rights reserved.