SWIFT - convert string to UIKit element

maybe this is an awkward question, but it is possible to convert String to UIKit element. By this I mean:

I have the following UIViewController

import UIKit

class BlurVC: UIViewController {


    let bg: UIImageView = {
       let image = UIImageView()
        image.image = #imageLiteral(resourceName: "IMG_9293")
        image.contentMode = .scaleAspectFill
        image.backgroundColor = .red
        return image
    }()

    let blurView: UIVisualEffectView = {
        let view = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()



        bg.frame = view.bounds
        view.addSubview(bg)
        blurView.frame = bg.bounds
        bg.addSubview(blurView)

        setBlurButton()
    }

    func setBlurButton() {

        let blurEffects = ["extraLight", "light", "dark"]

        for index in 0..<blurEffects.count{

            let button = UIButton()
            button.setTitle(blurEffects[index], for: .normal)
            button.addTarget(self, action: #selector(setBlurEffect(_ :)), for: .touchUpInside)
            button.frame = CGRect(x: 10, y: 10  + index * 30, width: 100, height: 30)
            view.addSubview(button)
        }
    }

    func setBlurEffect(_ sender: UIButton){
        print(sender.titleLabel?.text)
        if let blurEffect = sender.titleLabel?.text {
            blurView.effect = UIBlurEffect(style: ".\(blurEffect)")
        }
    }
}

      

I am creating 3 buttons dynamically with 3 parameters UIBlurEffectStyle title.

I want the following to set the UIBlurEffectStyle value based on the title label.

Of course xCode warns me that it cannot convert String to UIBlurEffectStyle.

Can I pass this string to UIBlurEffectStyle? If so, how to do it?

Thank.

+3


source to share


2 answers


UIBlurEffectStyle

based on Int

, so there is no automatic conversion from String

.

But you can do it manually. Here's an extension with init that accepts cases UIBlurEffectStyle

as String

s:



extension UIBlurEffectStyle {

    init?(with string: String) {
        switch string {
        case "extraLight":
            self = .extraLight
        case "light":
            self = .light
        case "dark":
            self = .dark
        case "regular":
            self = .regular
        case "prominent":
            self = .prominent
        default:
            return nil
        }
    }
}

      

Is not an erroneous initializer. Since it is line-printed, you want to test if it works before using it.

+2


source


You can add an extension:

extension UIBlurEffectStyle {
    static func styleFromString(_ string: String) -> UIBlurEffectStyle {
        switch string {
        case "extraLight":
            return .extraLight
        case "light":
            return .light
        case "dark":
            return .dark
        default:
            return .regular /* NOTE: .regular is not available in < iOS 10 */
        }
    }
}

      



For use in your case:

blurView.effect  = UIBlurEffect(style: UIBlurEffectStyle.styleFromString("blurEffect"))

      

+2


source







All Articles