从阴影按钮中删除标题阴影

时间:2018-08-08 19:11:28

标签: ios swift uibutton

我正在尝试从按钮标题中删除阴影。

我如何制作阴影和拐角半径:

func makeShadowRounded() {
    layer.cornerRadius = 25
    layer.shadowColor = UIColor.darkGray.cgColor
    layer.shadowOffset = CGSize(width: 0, height: 2)
    layer.shadowRadius = 3
    layer.shadowOpacity = 0.15
}

我的白色背景按钮:

let signInButton: UIButton = {
    let button = UIButton()
    button.layer.borderColor = UIColor.customBlue.cgColor
    button.layer.borderWidth = 0.5
    button.setTitle("Sign In", for: .normal)
    button.setTitleColor(.customBlue, for: .normal)
    button.makeShadowRounded()
    return button
}()

为了去除(/替换)阴影(用不同的颜色),我用以下方法替换了setTitle and setTitleColor

    let shadow = NSShadow()
    shadow.shadowColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    shadow.shadowOffset = CGSize(width: 0, height: 2)
    shadow.shadowBlurRadius = 2

    let attributedString = NSAttributedString(string: "Sign In", attributes: [
            NSAttributedStringKey.shadow : shadow,
            NSAttributedStringKey.foregroundColor : UIColor.customBlue
        ])

    button.setAttributedTitle(attributedString, for: .normal)

但是,它不能正常工作。解决该问题的更好解决方案是什么?

1 个答案:

答案 0 :(得分:0)

我的疑问是,在您的代码中,您正在为按钮添加阴影,而您正在为属性字符串中的按钮标题添加阴影。如果要将阴影从按钮减少为属性文本(深灰色),可以通过减小其半径或更改其偏移量来实现