[英]Swift: Refactoring NSMutableAttributedString
I have used NSMutableAttributedString/NSAttributedString
here and there but don't have extensive experience with them.我在这里和那里都使用过
NSMutableAttributedString/NSAttributedString
,但对它们没有丰富的经验。 I have a code block that repeats itself and was wondering how would I go about refactoring
it?我有一个重复的代码块,想知道如何
refactoring
它? I've been working on a few extensions to refactor this but haven't had any luck.我一直在研究一些扩展来重构它,但没有任何运气。
The attributes
goes into a UILabel variable closure. attributes
进入 UILabel 变量闭包。
let attributes = NSMutableAttributedString(string: "ID: \n",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.black,
NSAttributedString.Key.backgroundColor : UIColor.clear,
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!])
attributes.append(NSMutableAttributedString(string: "\(nameID)",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.backgroundColor : UIColor.customBlue(),
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
attributes.append(NSMutableAttributedString(string: "\nDate Created: \n",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.black,
NSAttributedString.Key.backgroundColor : UIColor.clear,
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
attributes.append(NSMutableAttributedString(string: "TEST",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.backgroundColor : UIColor.customBlue(),
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
attributes.append(NSMutableAttributedString(string: "\nDate Last Used: \n",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.black,
NSAttributedString.Key.backgroundColor : UIColor.clear,
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
attributes.append(NSMutableAttributedString(string: "TEST",
attributes: [NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.backgroundColor : UIColor.customBlue(),
NSAttributedString.Key.font : UIFont(name: "Helvetica", size: 15)!]))
Try looking at your code and seeing if there's a pattern.尝试查看您的代码,看看是否有模式。 What I see is creating multiple
NSAttributedString
s with String
values, setting foreground and background, and the same font over and over.我看到的是使用
String
值创建多个NSAttributedString
,设置前景和背景,一遍又一遍地使用相同的字体。 So this is ripe for the refactor picking.所以现在进行重构选择的时机已经成熟。
First, set up your data:首先,设置您的数据:
// one note here is that you actually have a newline char for every entry, so it's probably better practice t simply drop them and apply them in your loop which we'll get to
let values = ["ID:", nameID /*I assume this is already a `String`*/, "Date Created: ", "TEST", "Date Last Used:", "Test"]
Colors seem to follow their own pattern: titles are black on clear and values are white on custom blue.颜色似乎遵循自己的模式:标题在透明上是黑色的,值在自定义蓝色上是白色的。 We can use this in our loop, too.
我们也可以在循环中使用它。 Let's set up our attributes so they're easy to reference:
让我们设置我们的属性,以便它们易于引用:
let titleAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.black,
.backgroundColor: UIColor.clear,
.font: UIFont(name: "Helvetica", size: 15)!
]
let valueAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.white,
.backgroundColor: UIColor.customBlue(),
.font: UIFont(name: "Helvetica", size: 15)!
]
Now, let's loop:现在,让我们循环:
let mutableString = NSMutableAttributedString()
for (index, currentValue) in values.enumerated() {
// if we're on an even number, we have a title. If it's odd, it's a value
let attributes = index % 2 == 0 ? titleAttributes : valueAttributes
// if we aren't on the last index, add a newline to the value
let value = index < values.count - 1 ? "\(currentValue)\n" : currentValue
mutableString.appendAttributedString(string: value, attributes: attributes)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.