I've a grid UICollectionView
showing a single text label in each cell. While the figure shows different attributes in each cell, I cannot work out how to store and access specific NSAttributedString.Key.foregroundColor
values at the indexPath.item
.
For the text, I've an array of string values, which I call via the indexPath.item in cellForItemAt indexPath. But I can't figure out how to create an equivalent array of attribute values.
Model: let myText = ["Pos.1", "Main Verb", "Pos.2".... etc
Collection View datasource:
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
}
I've tried creating arrays of NSAttributedString or NSAttribtuedString.Key, but it never compiles. How do I do this so that I can get the right value at indexPath.item? or is this entirely the wrong approach?
let cellColor = [
NSAttributedString.Key.foregroundColor: UIColor.blue
NSAttributedString.Key.foregroundColor: UIColor.red
...
Ultimately, I'd like to have the data in a plist or json file or core data, but (I believe) would still need to load the data into an array (I believe) to access via indexPath.item.
I'm not very experienced, so I might be missing something quite basic.