【问题标题】:Swift: Refactoring NSMutableAttributedStringSwift:重构 NSMutableAttributedString
【发布时间】:2019-12-24 07:48:21
【问题描述】:

我在这里和那里使用过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)!]))

【问题讨论】:

  • 至少可以避免重复属性数组的代码。只需移出属性数组并为其分配一个常量,然后在参数中重用它

标签: ios swift refactoring nsattributedstring nsmutableattributedstring


【解决方案1】:

尝试查看您的代码并查看是否存在模式。我看到的是使用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)
}

【讨论】:

  • 谢谢!我对您的答案进行了一些调整,它完美无缺,非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多