【问题标题】:AttributedString on a CollectionView cell is not displaying string valueCollectionView 单元格上的 AttributedString 未显示字符串值
【发布时间】:2018-11-13 19:44:02
【问题描述】:

我有一个标签设置为属性字符串的 UICollectinViewCell。

在 cellForItem 中,我将一个简单的字符串转换为 NSAttributedString,这样我就可以设置 UILabel 的属性文本属性。

字符串"Attributed String Text" 仅在我滚动 CollectionView 时出现。如果我不滚动,"Label" 就会出现,因为这是在 IB 中设置的文本。如果我将 UILabel 保持为纯文本,则正确的文本会显示得很好。

// cellForItemAt
let string = "Attributed String Text"
let mutableAttributedString = NSAttributedString(string: string)
cell.subName.attributedText = mutableAttributedString

如何在不滚动 CollectionView 的情况下显示属性文本?

【问题讨论】:

  • 你在哪里设置委托和数据源?
  • 我在viewDidLoad中设置了delegate和dataSource。
  • 你能发布完整的上下文
  • 改用cell.subName.text = "Something"。如果你没有看到任何东西,那与 NSAttributedString 无关,而是UICollectionView。但是我们需要更多上下文,cellForItemAt 的完整代码,重用问题?
  • @Larme 谢谢,我会试试的。我也会从项目中发布更多代码。

标签: swift string uicollectionview uilabel nsattributedstring


【解决方案1】:

我想通了。我相信这可能是一个 Xcode 错误。我的代码没有任何问题。普通(纯)文本 UILabel 显示正确的文本就好了。当我将它切换到属性 UILabel 时,文本停止显示,没有其他代码更改。

经过一段时间的挖掘,我找到了这个帖子:Label text in UICollectionViewCell not updating,用户 Jacksonsox 在那里遇到了类似的问题。

在 Interface Builder 中,我删除了与未更新的 UILabel 的连接,删除了 UILabel,重新添加了一个新标签,重新连接它,将其设置为 Attributed text,文本现在显示得很好.同样,没有进行任何代码更改。只有 IB 发生了变化。

我希望这对遇到类似问题的其他人有所帮助。

【讨论】:

    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    相关资源
    最近更新 更多