Swift 如何在变量闭包中 DRY 代码?
Swift How to DRY code in variable closures?
我正在使用自动布局(以编程方式)设置我的 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.")
我正在使用自动布局(以编程方式)设置我的 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.")