gpt4 book ai didi

ios - 在 iOS 中的按钮下添加虚线

转载 作者:搜寻专家 更新时间:2023-11-01 07:13:57 25 4
gpt4 key购买 nike

我有按钮,我想在它下面添加虚线 - 所以它看起来像这样。我知道如何使用属性字符串来做到这一点,但我认为我应该使用 subLayers,因为我将有更多的配置选项:例如文本和线条之间的距离。

enter image description here

最佳答案

这是如何实现的示例:

class CustomButton: UIButton {

override func layoutSubviews() {
super.layoutSubviews()

layer.sublayers?.filter({ $0.name == "DashedTopLine" }).map({ $0.removeFromSuperlayer() })

let shapeLayer = CAShapeLayer()
shapeLayer.name = "DashedTopLine"
shapeLayer.bounds = bounds
shapeLayer.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 1
shapeLayer.lineDashPattern = [2, 2]

let path = CGMutablePath()
path.move(to: CGPoint.zero)
path.addLine(to: CGPoint(x: frame.width, y: 0))
shapeLayer.path = path

layer.addSublayer(shapeLayer)

}
}

关于ios - 在 iOS 中的按钮下添加虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43318740/

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