如何将UIVibrancyEffect添加到现有的UILabel(IBOutlet)?

时间:2015-03-03 12:03:33

标签: swift ios8 uiblureffect uivibrancyeffect

我有一个工作的ViewController,工作UIBlurEffect。这是我的代码:

@IBOutlet weak var nameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let blurEffect = UIBlurEffect(style: .Light)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.frame = self.view.frame
        blurView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.insertSubview(blurView, atIndex: 0)
}

现在我想在nameLabel中添加一个UIVibranyEffect。 如何以编程方式将UIVibrancyEffect添加到现有的UILabel?

1 个答案:

答案 0 :(得分:7)

您需要实例化一个UIVisualEffectView并在其中添加您想要充满活力的内容。例如:

    // Blur Effect
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    view.addSubview(blurEffectView)

    // Vibrancy Effect
    let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)
    let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
    vibrancyEffectView.frame = view.bounds

    // Label for vibrant text
    let vibrantLabel = UILabel()
    vibrantLabel.text = "Vibrant"

    // Add label to the vibrancy view
    vibrancyEffectView.contentView.addSubview(vibrantLabel)

    // Add the vibrancy view to the blur view
    blurEffectView.contentView.addSubview(vibrancyEffectView)