Skip to content

Commit

Permalink
Fixed UIGradientWithGradient issue when setting background
Browse files Browse the repository at this point in the history
  • Loading branch information
byerlyb20 committed Jul 15, 2024
1 parent aab9280 commit 3fcfeab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ class DesiredSizeView(context: Context) : ViewGroup(context) {
private var clickListenerForChild: OnClickListener? = null
override fun setOnClickListener(l: OnClickListener?) {
clickListenerForChild = l
getChildAt(0)?.let {
it.setOnClickListener(l)
clickListenerForChild = null
}
getChildAt(0)?.setOnClickListener(l)
}

override fun onViewAdded(child: View?) {
super.onViewAdded(child)
setOnClickListener(clickListenerForChild)

if (child != null && clickListenerForChild != null) {
child.setOnClickListener(clickListenerForChild)
}
}

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ class UILabelWithGradient : UIView(CGRectZero.readValue()) {
userInteractionEnabled = false
}

private val uiViewWithLabelMask = UIView(bounds).also(::addSubview)

val label = UILabel().also {
addSubview(it)
maskView = it
uiViewWithLabelMask.addSubview(it)
uiViewWithLabelMask.maskView = it
}

private var gradientLayer: CALayer? = null
set(value) {
field?.removeFromSuperlayer()
value?.let {
it.frame = this@UILabelWithGradient.bounds
layer.insertSublayer(it, atIndex = 0.toUInt())
uiViewWithLabelMask.layer.insertSublayer(it, atIndex = 0.toUInt())
}
field = value
}
Expand All @@ -44,7 +46,7 @@ class UILabelWithGradient : UIView(CGRectZero.readValue()) {
when (f) {
is Color -> {
gradientLayer = null
backgroundColor = f.toUiColor()
uiViewWithLabelMask.backgroundColor = f.toUiColor()
}
is LinearGradient -> gradientLayer = CAGradientLayer().apply {
this.type = kCAGradientLayerAxial
Expand Down Expand Up @@ -73,12 +75,14 @@ class UILabelWithGradient : UIView(CGRectZero.readValue()) {
super.layoutSubviews()
gradientLayer?.frame = bounds
bounds.useContents {
label.setFrame(cValue<CGRect> {
val childFrame = cValue<CGRect> {
origin.x = 0.0
origin.y = 0.0
size.width = this@useContents.size.width
size.height = this@useContents.size.height
})
}
uiViewWithLabelMask.setFrame(childFrame)
label.setFrame(childFrame)
}
}
}

0 comments on commit 3fcfeab

Please sign in to comment.