【问题标题】:How to store and access an 'array' of NSAttributedString.Key.foregroundColor values如何存储和访问 NSAttributedString.Key.foregroundColor 值的“数组”
【发布时间】:2019-09-18 23:14:58
【问题描述】:

我有一个网格UICollectionView,在每个单元格中显示一个文本标签。虽然该图显示了每个单元格中的不同属性,但我无法弄清楚如何在 indexPath.item 处存储和访问特定的 NSAttributedString.Key.foregroundColor 值。

对于文本,我有一个字符串值数组,我通过 cellForItemAt indexPath 中的 indexPath.item 调用它。但我不知道如何创建一个等效的属性值数组。

型号: 让 myText = ["Pos.1", "Main Verb", "Pos.2".... 等

集合视图数据源:

func colletionView(_ collectionView.UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CVCell", for: indexPath as! CVCell

    let text = myModel.myText[indexPath.item]
    let myAttribute = [NSAttributedString.Key.foregroundColor: UIColor.blue]
    let myAttributedText = NSAttributedString(string: text, attributes: myAttributes as [NSAttributedString.Key : Any])

    cell.label.attributedText = myAttributedText
    return cell
}

我尝试过创建 NSAttributedString 或 NSAttribtuedString.Key 的数组,但它永远不会编译。我该怎么做才能在 indexPath.item 中获得正确的值?还是这完全是错误的方法?

let cellColor = [
     NSAttributedString.Key.foregroundColor: UIColor.blue
     NSAttributedString.Key.foregroundColor: UIColor.red
      ...

最终,我希望将数据保存在 plist 或 json 文件或核心数据中,但(我相信)仍需要将数据加载到数组中(我相信)才能通过 indexPath.item 访问。

我不是很有经验,所以我可能遗漏了一些非常基本的东西。

【问题讨论】:

  • Chante myText 数组类型从 [String] 到 [NSAttributedString]
  • 您要将颜色存储在coredata还是plist中?
  • 作为第一步,我“只是”想创建一个要使用的数组,然后当我在代码中对数据进行建模时,我将使用 plist 或 coredata 来加载大批。现在 - 尝试 LorenzOliveto 回答(对我来说很有意义)。

标签: ios uitableview uicollectionview nsattributedstring nsattributedstringkey


【解决方案1】:

你必须创建一个数组来存储模型中的颜色,就像你为文本所拥有的那样

型号:

let myText = ["Pos.1", "Main Verb", "Pos.2".... etc
let myColors = [UIColor.blue, UIColor.red, UIColor.green.... etc

然后像这样访问它

...
let text = myModel.myText[indexPath.item]
let color = myModel.myColors[indexPath.item]
let myAttributes: [NSAttributedString.Key : Any] = [.foregroundColor: color]
let myAttributedText = NSAttributedString(string: text, attributes: myAttributes)
...

请注意,您发布的不是数组,而是Dictionary。 此外,如果您只是更改文本颜色而不必使用NSAttributedString,您可以更改标签textColor 属性

编辑:

根据@Larme 的建议,您还可以创建一个结构来保存模型中的数据,这样您就只能拥有一个数组:

struct TextSettings {
    let text: String
    let color: UIColor
}
let myTextSettings = [TextSettings(text: "Pos.1", color: UIColor.blue),
                      TextSettings(text: "Main Verb", color: UIColor.red),
                      TextSettings(text: "Pos.2", color: UIColor.green), ...]

并在设置单元格时使用它

...
let settings = myModel.myTextSettings[indexPath.item]
let myAttributes: [NSAttributedString.Key : Any] = [.foregroundColor: settings.color]
let myAttributedText = NSAttributedString(string: settings.text, attributes: myAttributes)
...

【讨论】:

  • 最好只有一个数组而不是多个数组。更好地使用自定义结构来处理颜色和文本,并使该数组包含此自定义结构实例。
  • 拥有一个数组也是一种很好的做法,尤其是如果您想修改它。想象一下,您想删除索引 2 处的项目,您也需要删除另一个数组上的项目。如果你想根据文本进行排序,同样,你必须模仿颜色数组的排序。这是可能的,但你增加了额外的工作。
  • 太棒了 - 谢谢。我确实尝试了您拥有的数组,但是在字典中使用它超出了我的经验。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 2021-03-16
  • 2013-04-30
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 2020-12-03
相关资源
最近更新 更多