brandonmcansh
11/07/2023, 1:28 PMEXC_BAD_ACCESS
when updating a UIEdgeInsets 🧵@ExperimentalForeignApi
class UIPaddingLabel(rect: CValue<CGRect>) : UILabel(rect) {
constructor(): this(CGRectZero.readValue())
private var textEdgeInsets = UIEdgeInsetsZero
set(value) {
field = value
invalidateIntrinsicContentSize()
}
override fun drawRect(rect: CValue<CGRect>) {
super.drawRect(
UIEdgeInsetsInsetRect(
rect,
UIEdgeInsetsMake(
<http://textEdgeInsets.top|textEdgeInsets.top>,
textEdgeInsets.left,
textEdgeInsets.bottom,
textEdgeInsets.right
)
)
)
}
var paddingLeft: CGFloat
get() = textEdgeInsets.left
set(value) {
textEdgeInsets.left = value
}
var paddingTop: CGFloat
get() = <http://textEdgeInsets.top|textEdgeInsets.top>
set(value) {
<http://textEdgeInsets.top|textEdgeInsets.top> = value
}
var paddingRight: CGFloat
get() = textEdgeInsets.right
set(value) {
textEdgeInsets.right = value
}
var paddingBottom: CGFloat
get() = textEdgeInsets.bottom
set(value) {
textEdgeInsets.bottom = value
}
var paddingHorizontal: CGFloat
get() = (textEdgeInsets.left) / 2 + (textEdgeInsets.right / 2)
set(value) {
textEdgeInsets.left = value
textEdgeInsets.right = value
}
var paddingVertical: CGFloat
get() = (<http://textEdgeInsets.top|textEdgeInsets.top>) / 2 + (textEdgeInsets.bottom / 2)
set(value) {
<http://textEdgeInsets.top|textEdgeInsets.top> = value
textEdgeInsets.bottom = value
}
}
EXC_BAD_ACCESS (code=2, address=0x1859da9c8)
is thrown when setting paddingHorizontal when creating my label like so:
val label = UIPaddingLabel().apply {
backgroundColor = UIColor.whiteColor
text = "$$price"
font = UIFont.systemFontOfSize(13.0, UIFontWeightSemibold)
layer.apply {
cornerRadius = 10.0
masksToBounds = true
shadowColor = UIColor.blackColor.CGColor
shadowRadius = 2.0
shadowOpacity = 0.2f
shadowOffset = CGSizeMake(1.0, 2.0)
}
paddingHorizontal = 16.0
paddingVertical = 8.0
sizeToFit()
}
Landry Norris
11/07/2023, 3:24 PMbrandonmcansh
11/07/2023, 3:42 PMLandry Norris
11/07/2023, 3:46 PMbrandonmcansh
11/07/2023, 3:46 PMget() = textEdgeInsets.useContents { return top }
Landry Norris
11/07/2023, 3:48 PMbrandonmcansh
11/07/2023, 3:49 PM@OptIn(BetaInteropApi::class)
@ExperimentalForeignApi
@ExportObjCClass
class UIPaddingLabel(rect: CValue<CGRect>) : UILabel(rect) {
constructor(): this(CGRectZero.readValue())
private var textEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
set(value) {
field = value
invalidateIntrinsicContentSize()
}
override fun textRectForBounds(
bounds: CValue<CGRect>,
limitedToNumberOfLines: NSInteger
): CValue<CGRect> {
val insetRect = UIEdgeInsetsInsetRect(
bounds,
textEdgeInsets
)
val textRect = super.textRectForBounds(insetRect, limitedToNumberOfLines)
val invertedInsets = textEdgeInsets.useContents {
UIEdgeInsetsMake(
top = -top,
left = -left,
right = -right,
bottom = -bottom
)
}
return UIEdgeInsetsInsetRect(textRect, invertedInsets)
}
override fun drawTextInRect(rect: CValue<CGRect>) {
super.drawTextInRect(
UIEdgeInsetsInsetRect(
rect,
textEdgeInsets
)
)
}
var paddingLeft: CGFloat
get() = textEdgeInsets.useContents { return left }
set(value) {
textEdgeInsets = textEdgeInsets.copy { left = value }
}
var paddingRight: CGFloat
get() = textEdgeInsets.useContents { return right }
set(value) {
textEdgeInsets = textEdgeInsets.copy { right = value }
}
var paddingTop: CGFloat
get() = textEdgeInsets.useContents { return top }
set(value) {
textEdgeInsets = textEdgeInsets.copy { top = value }
}
var paddingBottom: CGFloat
get() = textEdgeInsets.useContents { return bottom }
set(value) {
textEdgeInsets = textEdgeInsets.copy { bottom = value }
}
}
updated class for those following along