Ios 如何存储和访问';数组';NSAttributedString.Key.foregroundColor值的

Ios 如何存储和访问';数组';NSAttributedString.Key.foregroundColor值的,ios,uitableview,uicollectionview,nsattributedstring,nsattributedstringkey,Ios,Uitableview,Uicollectionview,Nsattributedstring,Nsattributedstringkey,我有一个网格UICollectionView在每个单元格中显示一个文本标签。虽然图中显示了每个单元格中的不同属性,但我无法确定如何在indexath.item中存储和访问特定的NSAttributedString.Key.foregroundColor值 对于文本,我有一个字符串值数组,我通过cellForItemAt indexPath中的indexPath.item调用它。但我不知道如何创建属性值的等效数组 型号: 让myText=[“位置1”,“主动词”,“位置2”…等 集合视图数据源:

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

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

型号: 让myText=[“位置1”,“主动词”,“位置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或NSAttributedString.Key的数组,但它从未编译过。如何才能在indexPath.item中获得正确的值?或者这是完全错误的方法

let cellColor = [
     NSAttributedString.Key.foregroundColor: UIColor.blue
     NSAttributedString.Key.foregroundColor: UIColor.red
      ...
最终,我希望将数据保存在plist或json文件或核心数据中,但(我相信)仍然需要将数据加载到数组中(我相信)才能通过indexath.item进行访问


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

您必须创建一个数组来存储模型中的颜色,就像文本中的颜色一样

型号:

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)
...
请注意,您发布的不是数组,而是数组。 此外,如果只更改文本颜色,而不必使用
NSAttributedString
,则可以更改标签
textColor
属性

编辑:

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

struct TextSettings {
    let text: String
    let color: UIColor
}
并在设置单元格时使用它

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

将myText数组类型从[String]改为[NSAttributedString]是否要将颜色存储在coredata或plist中?作为第一步,我“只是”想创建一个要使用的数组,然后在代码中对数据建模后,我会使用将加载数组的plist或coredata。现在-尝试LorenzOliveto回答(对我来说有意义)。我最好只有一个数组,而不是多个。使用自定义结构来处理颜色和文本,并使该数组包含此自定义结构实例,效果更好。使用一个数组也是一种很好的做法,特别是如果您要修改它。假设您要删除索引2处的项,则需要删除另一个数组上的项y也是。如果你想根据文本排序,同样,你必须模仿颜色数组的排序。这是可能的,但你增加了额外的工作。简单地说,太棒了-谢谢你。我确实尝试了你拥有的数组,但在字典中使用它超出了我的经验。非常感谢。
...
let settings = myModel.myTextSettings[indexPath.item]
let myAttributes: [NSAttributedString.Key : Any] = [.foregroundColor: settings.color]
let myAttributedText = NSAttributedString(string: settings.text, attributes: myAttributes)
...