Text Shadow - iOS,swift 3.0

Dpe*_*rov 4 border uilabel ios swift3

我试图让阴影尺寸更大一点,但我不能这样做.

至今:

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
Run Code Online (Sandbox Code Playgroud)

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

这样的事情.

也许有边框可以做,我的文字是一个UIButton!!! 的标题我期望它是围绕uiButton的文本

Jan*_*aya 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)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你

输出:

在此输入图像描述


Jan*_*aya 1

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

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

这是代码:

雨燕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)
Run Code Online (Sandbox Code Playgroud)

雨燕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)
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述