Swift如何在变量闭包中干掉代码?

问题描述 投票:2回答:1

我正在使用Auto Layout(以编程方式)设置我的ViewController,我已经得到了我想要的一切,但现在我想让我的代码更有效率,我注意到我有一些重复的代码,我正在试图如何在变量闭包中重复代码并将其放在其他位置,以便代码更清晰。

我该如何清理代码?可变封闭仍然是新手。

我复制和粘贴的代码是一个全局变量。

let descriptionTextViewOne: UITextView = {
    let textView = UITextView()

    let text = "Tap anywhere to start\nyour day right!"
    let shadow = NSShadow()
    shadow.shadowColor = UIColor.white
    shadow.shadowOffset = CGSize(width: 1, height: 1)
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.init(name: "Marker felt", size: 25)!,
        .foregroundColor: UIColor.init(red: 91.0/255.0, green: 91.0/255.0, blue: 91.0/255.0, alpha: 1.0),
        .shadow: shadow
    ]
    let attributedText = NSAttributedString(string: text, attributes: attributes)
    textView.attributedText = attributedText
    textView.textAlignment = .center
    textView.isEditable = false
    textView.isScrollEnabled = false
    textView.isSelectable = false
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.backgroundColor = .clear
    return textView
}()

let descriptionTextViewTwo: UITextView = {
    let textView = UITextView()

    let text = "A happy video a day\nmakes the heartache\ngo away."
    let shadow = NSShadow()
    shadow.shadowColor = UIColor.white
    shadow.shadowOffset = CGSize(width: 1, height: 1)
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.init(name: "Marker felt", size: 25)!,
        .foregroundColor: UIColor.init(red: 91.0/255.0, green: 91.0/255.0, blue: 91.0/255.0, alpha: 1.0),
        .shadow: shadow
    ]
    let attributedText = NSAttributedString(string: text, attributes: attributes)
    textView.attributedText = attributedText
    textView.textAlignment = .center
    textView.isEditable = false
    textView.isScrollEnabled = false
    textView.isSelectable = false
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.backgroundColor = .clear
    return textView
}()
ios swift dry
1个回答
2
投票

您可以创建一个函数并重用它

func descriptionTextView(with text: String) -> UITextView {
    let textView = UITextView()
    let shadow = NSShadow()
    shadow.shadowColor = UIColor.white
    shadow.shadowOffset = CGSize(width: 1, height: 1)
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.init(name: "Marker felt", size: 25)!,
        .foregroundColor: UIColor.init(red: 91.0/255.0, green: 91.0/255.0, blue: 91.0/255.0, alpha: 1.0),
        .shadow: shadow
    ]
    let attributedText = NSAttributedString(string: text, attributes: attributes)
    textView.attributedText = attributedText
    textView.textAlignment = .center
    textView.isEditable = false
    textView.isScrollEnabled = false
    textView.isSelectable = false
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.backgroundColor = .clear
    return textView
}

lazy var descriptionTextViewOne: UITextView = descriptionTextView(with: "Tap anywhere to start\nyour day right!")

lazy var descriptionTextViewTwo: UITextView = descriptionTextView(with: "A happy video a day\nmakes the heartache\ngo away.")
© www.soinside.com 2019 - 2024. All rights reserved.