在 UILabel 上设置 insets
Set insets on UILabel
我正在尝试在 UILabel 中设置一些插图。它工作得很好,但现在 UIEdgeInsetsInsetRect
已被 CGRect.inset(by:)
取代,我不知道如何解决这个问题。
当我尝试将 CGRect.inset(by:)
与我的插图一起使用时,我收到消息称 UIEdgeInsets
无法转换为 CGRect
。
我的代码
class TagLabel: UILabel {
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: CGRect.insetBy(inset))
// super.drawText(in: UIEdgeInsetsInsetRect(rect, inset)) // Old code
}
}
有人知道如何设置 UILabel 的插图吗?
请更新您的代码如下
class TagLabel: UILabel {
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: rect.insetBy(inset))
}
}
恕我直言,您还必须更新 intrinsicContentSize
:
class InsetLabel: UILabel {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: inset))
}
override var intrinsicContentSize: CGSize {
var intrinsicContentSize = super.intrinsicContentSize
intrinsicContentSize.width += inset.left + inset.right
intrinsicContentSize.height += inset.top + inset.bottom
return intrinsicContentSize
}
}
this code differs from the accepted answer because the accepted answer uses insetBy(inset) and this answer uses inset(by: inset). When I added this answer in iOS 10.1 and Swift 4.2.1 autocomplete DID NOT give you rect.inset(by: ) and I had to manually type it in. Maybe it does in Swift 5, I'm not sure
对于 iOS 10.1
和 Swift 4.2.1
使用 rect.inset(by:
)
这个:
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: rect.inset(by: inset))
}
Swift 5
将方法 drawText(...) 替换为 draw(...)
extension UILabel {
open override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.draw(rect.inset(by: inset))
}}
我正在尝试在 UILabel 中设置一些插图。它工作得很好,但现在 UIEdgeInsetsInsetRect
已被 CGRect.inset(by:)
取代,我不知道如何解决这个问题。
当我尝试将 CGRect.inset(by:)
与我的插图一起使用时,我收到消息称 UIEdgeInsets
无法转换为 CGRect
。
我的代码
class TagLabel: UILabel {
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: CGRect.insetBy(inset))
// super.drawText(in: UIEdgeInsetsInsetRect(rect, inset)) // Old code
}
}
有人知道如何设置 UILabel 的插图吗?
请更新您的代码如下
class TagLabel: UILabel {
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: rect.insetBy(inset))
}
}
恕我直言,您还必须更新 intrinsicContentSize
:
class InsetLabel: UILabel {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: inset))
}
override var intrinsicContentSize: CGSize {
var intrinsicContentSize = super.intrinsicContentSize
intrinsicContentSize.width += inset.left + inset.right
intrinsicContentSize.height += inset.top + inset.bottom
return intrinsicContentSize
}
}
this code differs from the accepted answer because the accepted answer uses insetBy(inset) and this answer uses inset(by: inset). When I added this answer in iOS 10.1 and Swift 4.2.1 autocomplete DID NOT give you rect.inset(by: ) and I had to manually type it in. Maybe it does in Swift 5, I'm not sure
对于 iOS 10.1
和 Swift 4.2.1
使用 rect.inset(by:
)
这个:
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: rect.inset(by: inset))
}
Swift 5 将方法 drawText(...) 替换为 draw(...)
extension UILabel {
open override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.draw(rect.inset(by: inset))
}}