- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个网格UICollectionView
,在每个单元格中显示一个文本标签。虽然该图显示了每个单元格中的不同属性,但我无法弄清楚如何存储和访问 indexPath.item
处的特定 NSAttributedString.Key.foregroundColor
值。
对于文本,我有一个字符串值数组,我通过 cellForItemAt indexPath 中的 indexPath.item 调用该数组。但我不知道如何创建等效的属性值数组。
型号: let myText = ["Pos.1", "主要动词", "Pos.2".... 等等
Collection View 数据源:
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 访问。
我经验不足,所以我可能会遗漏一些非常基本的东西。
最佳答案
你必须创建一个数组来存储你的模型中的颜色,就像你的文本一样
型号:
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)
...
关于ios - 如何存储和访问 'array' 的 NSAttributedString.Key.foregroundColor 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55918449/
我对 Core Text 的行距一无所知。我正在使用 NSAttributedString 并在其上指定以下属性:- kCTFont属性名称- kCTParagraphStyleAttributeNa
我想显示 3 行 NSAttributedString。有没有办法根据宽度和行数确定所需的高度? 而且我不想创建 UILabel 来进行大小计算,因为我希望计算在后台线程中完成。 最佳答案 我想知道为
我想用另一个 NSAttributedString 替换 NSAttributedString 的子字符串(例如 @"replace")。 我正在寻找与 NSString 的 stringByRepl
我正在尝试在另一个文本中创建多行单独文本以实现以下文本样式。 我已经尝试使用下面的代码来实现目标,但是代码的第三部分产生了一个问题(中等字体) private func createLimit
我有一个 NSAttributedString 并希望在每个 “***” 处拆分它。所以实际上,我想在该字符串出现的任何地方分割一个 NSAttributedString 。结果应该类似于 NSAtt
尝试在 UITextView 中使用 NSAttributedString 显示上标/下标文本在 iOS13 中似乎已损坏 - 除非有人知道否则? 奇怪的是,如果我使用 UIFont systemFo
尝试在 UITextView 中使用 NSAttributedString 显示上标/下标文本在 iOS13 中似乎已损坏 - 除非有人知道否则? 奇怪的是,如果我使用 UIFont systemFo
我有一个富文本编辑器,用户可以在其中编辑字体、颜色等。模型将此信息作为 NSAttributedString 的实例保存在内存中。当我想存储此信息并写入磁盘时,我通过以下函数将属性字符串转换为 htm
这个问题在这里已经有了答案: Cannot convert value of type NSAttributedString.DocumentAttributeKey to .DocumentRea
我想将一个 html 字符串转换为 NSAttributedString,然后处理该字符串(更改颜色、字体大小、字体系列、背景-、前景色...),然后将字符串转换回来从 NSAttributedStr
为什么 NSAttributedString 不继承 NSString ? NSAttributedString 的父类为 NSObject 为什么? 最佳答案 为什么你希望它是 NSString 的
我们正在调用 NSAttributesString 的 initWithRTFD 将 NSData 转换为 attributedString。从 NSTextView 中,我们读取字符串并将其转换为
我想为标签设置样式。没有什么疯狂的,只是一些颜色,改变特定单词的字体大小,等等。我可以用 NSAttributedString 做到这一点,但是在 iOS 7 新引入的 TextKit 中这样做可能/
我正在尝试使用 RTFDFromRange 方法将 NSAttributedString 转换为 NSData。得到这个: No visible @interface for 'NSAttribute
有什么方法可以连接两个 NSAttributedString 吗?或者 NSAttributedString 和 NSString ?我尝试过这样的事情: NSAttributedString *at
有没有人知道那里有任何代码可以让我阅读包含 NSAttributedString 中的图像的 RTF? 是的,我知道有 RTFD 格式和 initWithRTF 方法...但是 initWithRTF
我正在使用 drawGlyphsForGlyphRange渲染 NSAttributedString (在 NSView drawRect 中) 有什么方法可以默认 NSAttributedStrin
这个问题在这里已经有了答案: How to use NSUnderlineStyle.PatternDot (1 个回答) 4 个月前关闭。 我有以下代码尝试为标签的字符串加下划线。 let attr
我已经花了3个小时来解决这个问题,但是有人知道为什么吗?请帮我! 下面的代码将图像添加为属性字符串的附件, UIImage *img = [UIImage imageNamed:imgName]; N
我有一个本地化的字符串: "%@ some text" = "%@ some text"; 格式说明符%@可以出现在本地化字符串中的任何位置。 问题是这个字符串应该是一个NSAttributedStr
我是一名优秀的程序员,十分优秀!