如何在Swift中对标签上产生阴影效果?

时间:2022-08-22 21:25:47

I can't figure out how to code a drop shadow on a label. I have a score label that changes so just photoshopping text with shadows wont be possible. I need to code it so it automatically has a blurry shadow behind the text at all times. Can anyone come with some examples or help?

我无法弄清楚如何在标签上编码投影。我有一个分数标签,改变所以只有photoshopping文本与阴影是不可能的。我需要对其进行编码,以便它始终在文本后面自动显示模糊的阴影。有人可以提供一些例子或帮助吗?


People saying this is a duplicate, the "duplicate" is about drop shadows on UIView, mine is about UILabel. It's not the same thing.

人们说这是重复的,“重复”是关于UIView的阴影,我的是关于UILabel。这不是一回事。

5 个解决方案

#1


23  

Give this a try - you can run it directly in a Playground page:

尝试一下 - 您可以直接在Playground页面中运行它:

import UIKit
import PlaygroundSupport

let container = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 400))

container.backgroundColor = UIColor.lightGray

PlaygroundPage.current.liveView = container

var r = CGRect(x: 40, y: 40, width: 300, height: 60)

let label = UILabel(frame: r)
label.font = UIFont.systemFont(ofSize: 44.0)
label.textColor = .white
label.frame = r
label.text = "Hello Blur"

container.addSubview(label)

label.layer.shadowColor = UIColor.black.cgColor
label.layer.shadowRadius = 3.0
label.layer.shadowOpacity = 1.0
label.layer.shadowOffset = CGSize(width: 4, height: 4)
label.layer.masksToBounds = false

Play around with different values for the shadow Color, Opacity, Radius and Offset

使用阴影颜色,不透明度,半径和偏移的不同值

Result:

结果:

如何在Swift中对标签上产生阴影效果?

#2


6  

You can write an extension and use it. Place the extension code outside of class ViewController.

您可以编写扩展程序并使用它。将扩展代码放在ViewController类之外。

I like subtle shadow.

我喜欢微妙的阴影。

extension UILabel {
    func textDropShadow() {
        self.layer.masksToBounds = false
        self.layer.shadowRadius = 2.0
        self.layer.shadowOpacity = 0.2
        self.layer.shadowOffset = CGSize(width: 1, height: 2)
    }

    static func createCustomLabel() -> UILabel {
        let label = UILabel()
        label.textDropShadow()
        return label
    }
}

On your label simply call this method

在您的标签上只需调用此方法即可

myLabel.textDropShadow()

如何在Swift中对标签上产生阴影效果?

#3


4  

UILabel has a property for changing its shadow, the image below shows the property in attributes inspector and the result.

UILabel有一个用于更改其阴影的属性,下面的图像显示属性检查器中的属性和结果。

如何在Swift中对标签上产生阴影效果?

Result of that effect on label 如何在Swift中对标签上产生阴影效果?

对标签产生影响的结果

#4


0  

Swift 4 - Extension with shadow parameters:

Swift 4 - 带阴影参数的扩展:

 // Label Shadow
    extension UILabel {
        func lblShadow(color: UIColor , radius: CGFloat, opacity: Float){
            self.textColor = color
            self.layer.masksToBounds = false
            self.layer.shadowRadius = radius
            self.layer.shadowOpacity = opacity

            self.layer.shadowOffset = CGSize(width: 1, height: 1)
            self.layer.shouldRasterize = true
            self.layer.rasterizationScale = UIScreen.main.scale
        }
    }

On your label simply call this method

在您的标签上只需调用此方法即可

let titleColor = UIColor(red:0.08, green:0.08, blue:0.08, alpha:1.0)
titleLbl.lblShadow(color: titleColor, radius: 3, opacity: 0.25)

#5


0  

Swift 4, IBInspectable using extension

Swift 4,IBInspectable使用扩展

extension UILabel {

    @IBInspectable var isShadowOnText: Bool {
        get {
            return self.isShadowOnText
        }
        set {
            guard (newValue as? Bool) != nil else {
                return
            }

            if newValue == true{

                self.layer.shadowColor = UIColor.black.cgColor
                self.layer.shadowRadius = 2.0
                self.layer.shadowOpacity = 1.0
                self.layer.shadowOffset = CGSize(width: 2, height: 2)
                self.layer.masksToBounds = false
            }
        }
    }
}

#1


23  

Give this a try - you can run it directly in a Playground page:

尝试一下 - 您可以直接在Playground页面中运行它:

import UIKit
import PlaygroundSupport

let container = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 400))

container.backgroundColor = UIColor.lightGray

PlaygroundPage.current.liveView = container

var r = CGRect(x: 40, y: 40, width: 300, height: 60)

let label = UILabel(frame: r)
label.font = UIFont.systemFont(ofSize: 44.0)
label.textColor = .white
label.frame = r
label.text = "Hello Blur"

container.addSubview(label)

label.layer.shadowColor = UIColor.black.cgColor
label.layer.shadowRadius = 3.0
label.layer.shadowOpacity = 1.0
label.layer.shadowOffset = CGSize(width: 4, height: 4)
label.layer.masksToBounds = false

Play around with different values for the shadow Color, Opacity, Radius and Offset

使用阴影颜色,不透明度,半径和偏移的不同值

Result:

结果:

如何在Swift中对标签上产生阴影效果?

#2


6  

You can write an extension and use it. Place the extension code outside of class ViewController.

您可以编写扩展程序并使用它。将扩展代码放在ViewController类之外。

I like subtle shadow.

我喜欢微妙的阴影。

extension UILabel {
    func textDropShadow() {
        self.layer.masksToBounds = false
        self.layer.shadowRadius = 2.0
        self.layer.shadowOpacity = 0.2
        self.layer.shadowOffset = CGSize(width: 1, height: 2)
    }

    static func createCustomLabel() -> UILabel {
        let label = UILabel()
        label.textDropShadow()
        return label
    }
}

On your label simply call this method

在您的标签上只需调用此方法即可

myLabel.textDropShadow()

如何在Swift中对标签上产生阴影效果?

#3


4  

UILabel has a property for changing its shadow, the image below shows the property in attributes inspector and the result.

UILabel有一个用于更改其阴影的属性,下面的图像显示属性检查器中的属性和结果。

如何在Swift中对标签上产生阴影效果?

Result of that effect on label 如何在Swift中对标签上产生阴影效果?

对标签产生影响的结果

#4


0  

Swift 4 - Extension with shadow parameters:

Swift 4 - 带阴影参数的扩展:

 // Label Shadow
    extension UILabel {
        func lblShadow(color: UIColor , radius: CGFloat, opacity: Float){
            self.textColor = color
            self.layer.masksToBounds = false
            self.layer.shadowRadius = radius
            self.layer.shadowOpacity = opacity

            self.layer.shadowOffset = CGSize(width: 1, height: 1)
            self.layer.shouldRasterize = true
            self.layer.rasterizationScale = UIScreen.main.scale
        }
    }

On your label simply call this method

在您的标签上只需调用此方法即可

let titleColor = UIColor(red:0.08, green:0.08, blue:0.08, alpha:1.0)
titleLbl.lblShadow(color: titleColor, radius: 3, opacity: 0.25)

#5


0  

Swift 4, IBInspectable using extension

Swift 4,IBInspectable使用扩展

extension UILabel {

    @IBInspectable var isShadowOnText: Bool {
        get {
            return self.isShadowOnText
        }
        set {
            guard (newValue as? Bool) != nil else {
                return
            }

            if newValue == true{

                self.layer.shadowColor = UIColor.black.cgColor
                self.layer.shadowRadius = 2.0
                self.layer.shadowOpacity = 1.0
                self.layer.shadowOffset = CGSize(width: 2, height: 2)
                self.layer.masksToBounds = false
            }
        }
    }
}