Swift: 重构 NSMutableAttributedString

Swift: Refactoring NSMutableAttributedString

我在这里和那里使用过 NSMutableAttributedString/NSAttributedString,但没有丰富的经验。我有一个自我重复的代码块,想知道我将如何处理 refactoring 它? 我一直在进行一些扩展来重构它,但一直没有成功。

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)!]))

尝试查看您的代码并查看是否存在模式。我看到的是用 String 值创建多个 NSAttributedStrings,设置前景和背景,一遍又一遍地使用相同的字体。所以重构选择的时机已经成熟。

首先,设置您的数据:

// 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"]

颜色似乎遵循自己的模式:标题为黑色透明,值为白色定制蓝色。我们也可以在循环中使用它。让我们设置我们的属性,以便于参考:

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)!
]

现在,让我们循环:

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)
}