Text Shadow - iOS,swift 3.0

时间:2017-05-30 08:55:01

标签: ios swift3 uilabel border

我正在尝试使阴影尺寸更大但我无法做到。

到目前为止:

findAPlace.titleLabel?.layer.shadowOffset = CGSize(width: -1, height: 1)
findAPlace.titleLabel?.layer.shouldRasterize = true
findAPlace.titleLabel?.layer.shadowRadius = 1
findAPlace.titleLabel?.layer.shadowOpacity = 1
findAPlace.titleLabel?.layer.shadowColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0).cgColor

如何将阴影缩放为比文本本身更大?

像这样的东西。

example

也许边框可以完成,我的文字是UIButton的标题!!!我希望它是uiButton文本的全部

2 个答案:

答案 0 :(得分:6)

你可以这样做

实际上您需要使用setTitleShadowColor代替titleLabel?.layer.shadowColor

这是完整的工作代码

    let btnTemp = UIButton(type: .custom)
    btnTemp.frame = CGRect(x: 50, y: 200, width: 150, height: 40)
    btnTemp.setTitle("Hello", for: .normal)
    btnTemp.titleLabel?.layer.shouldRasterize = true
    btnTemp.titleLabel?.layer.shadowRadius = 1.0
    btnTemp.titleLabel?.layer.shadowOpacity = 1.0
    btnTemp.setTitleColor(UIColor.blue, for: .normal)
    btnTemp.backgroundColor = UIColor.gray
    btnTemp.titleLabel?.shadowOffset = CGSize(width: -1, height: 1)
    btnTemp.setTitleShadowColor(UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0), for: .normal)
    self.view.addSubview(btnTemp)

希望有所帮助

<强>输出:

enter image description here

答案 1 :(得分:1)

您可以按照这种方式来实现概述文本。

您必须使用属性字符串并使用按钮的setAttributedTitle属性来获取所需的结果。

以下是代码:

Swift 4

let strokeTextAttributes: [NSAttributedStringKey : Any] = [
    NSAttributedStringKey.strokeColor : UIColor.red,
    NSAttributedStringKey.foregroundColor : UIColor.gray,
    NSAttributedStringKey.strokeWidth : -2.0,
]

let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)

Swift 3

let strokeTextAttributes = [
    NSStrokeColorAttributeName : UIColor.red,
    NSForegroundColorAttributeName : UIColor.gray,
    NSStrokeWidthAttributeName : -2.0,
] as [String : Any]

let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)

输出:

enter image description here

相关问题