gpt4 book ai didi

swift - 如何跟踪 NSMutableAttributedString 中的位置?

转载 作者:行者123 更新时间:2023-11-30 10:01:45 25 4
gpt4 key购买 nike

我正在迭代大量的输入行,并且对于某些标准,我将拉出一些行以构建要显示给用户的结果。我希望符合条件的最后一行是不同的颜色。目前我正在这样做

 endOfString = result.string.characters.count
lengthOfLastString = tempResult.string.characters.count

然后当我准备好显示我构建的字符串时

let changeRange = NSRange(location: endOfString+1, length: lengthOfLastString)
result.addAttributes(singleAttribute, range: changeRange)

但这不起作用,因为我最终超出了字符串上有效索引的范围。我一般认为 Swift 字符串比较复杂,那么这样做的方法是什么?

按要求提供示例数据:

But treade the stranger pathes of banishment

Rich. King them lay by their Helmets & their Speares, And both returne backe to their Chaires againe: Withdraw with vs, and let the Trumpets sound,

While we returne these Dukes what we Keyword decree.

我开始关心包含“King”一词的行,并且我想捕获所有行,包括包含“Keyword”的行。最后一行“关键字”是我想要制作不同颜色的内容。

最佳答案

在构建输出字符串时,请在每次追加行之前记录 endIndex。然后,在添加最后一行后, savedIndex..<endIndex是您要寻找的范围。

关于swift - 如何跟踪 NSMutableAttributedString 中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38132967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com