gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:07:23 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com