ios - Swift 如何在变量闭包中干燥代码?

标签 ios swift dry

我正在使用自动布局(以编程方式)设置我的 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
}()

最佳答案

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

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.")

关于ios - Swift 如何在变量闭包中干燥代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55566245/

相关文章:

ruby-on-rails - 如何在 Rails 中调用类似模型时使此代码(帮助程序)干燥?

ios - UIRefreshControl 和 UITableView 的 backgroundVIew

ios - 如何在 Interface Builder 中设置自动布局约束以使默认常量始终为 0?

ios - 通过标签区分一个 UIButton

iOS:应用程序在一段时间后无法运行(Xcode 8 的并行安装)

swift - 我一遍又一遍地使用相同的代码块。我如何巩固它?

ios - 请求完成后无法更新按钮标题

ios - 当图像保存在核心数据的 Collection View 中时,应用程序因内存错误而崩溃

ios - 如何在 AFNetworking 中设置内容类型?

c++ - 为什么auto不能作为函数声明的返回类型