我有一个网格UICollectionView
在每个单元格中显示一个文本标签。虽然该图显示了每个单元格中的不同属性,但我无法弄清楚如何NSAttributedString.Key.foregroundColor
在indexPath.item
.
对于文本,我有一个字符串值数组,我通过 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 访问。
我不是很有经验,所以我可能会遗漏一些非常基本的东西。