在String Swift中带下划线的搜索文本

发布于 2025-01-24 09:08:06 字数 1076 浏览 0 评论 0 原文

我正在尝试在字符串内部制作大胆和下划线的搜索文本。我已经尝试了这个解决方案,但这只是给我一个错误。

这是我的配置

 func configure(model: SmartSearchGroupViewModel, searchedKey: String) {
    let boldUnderlined = NSAttributedString(string: searchedKey, attributes: [.font: UIFont.sfProTextBold(size: 15), .underlineStyle: NSUnderlineStyle.single.rawValue])
    let content = NSMutableAttributedString.init(string: model.name)
    content.addAttribute(NSAttributedString.Key.font, value: UIFont.sfProTextRegular(size: 15), range: NSRange.init(location: 0, length: content.length))
    if model.name.substring(from: 0) == searchedKey.substring(from: 0) {
        content.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSRange.init(location: 0, length: boldUnderlined.string.count))
    }
    groupNameLabel.attributedText = content
}

,但我会有一个错误

无法将'int'类型的值转换为预期参数类型'string.index'

I am trying to make bold and underlined searched text inside the string. I have tried this solution but it just gives me an error. enter image description here

This is my configure

 func configure(model: SmartSearchGroupViewModel, searchedKey: String) {
    let boldUnderlined = NSAttributedString(string: searchedKey, attributes: [.font: UIFont.sfProTextBold(size: 15), .underlineStyle: NSUnderlineStyle.single.rawValue])
    let content = NSMutableAttributedString.init(string: model.name)
    content.addAttribute(NSAttributedString.Key.font, value: UIFont.sfProTextRegular(size: 15), range: NSRange.init(location: 0, length: content.length))
    if model.name.substring(from: 0) == searchedKey.substring(from: 0) {
        content.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSRange.init(location: 0, length: boldUnderlined.string.count))
    }
    groupNameLabel.attributedText = content
}

But I get an error

Cannot convert value of type 'Int' to expected argument type 'String.Index'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2025-01-31 09:08:06

可以这样做:

首先初始化具有默认属性的属性字符串:

let content = NSMutableAttributedString.init(string: model.name)
content.addAttribute(NSAttributedString.Key.font, value: UIFont.sfProTextRegular(size: 15), range: NSRange.init(location: 0, length: content.length))

//Here, if you don't want to use the `NSRange`, you can use instead:
let content = NSMutableAttributedString(string: model.name, attributes: [NSAttributedString.Key.font: UIFont.sfProTextRegular(size: 15)])

然后,为了添加搜索的特殊效果,您可以使用:

解决方案1:

let searchRange = (content.string as NSString).range(of: searchedKey)
if searchRange.location != NSNotFound {
    content.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: searchRange)
}

solution2

if let search = content.string.range(of: searchedKey) {
    let searchNSRange = NSRange(search, in: content.string)
    content.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: searchNSRange)
    }

: solution1&amp;解决方案2,是使用 range(of:)应用于 nsstring String ,它将输出 nsrange 或<代码>范围。由于 nsattributedString 使用 nsrange ,解决方案可能是“更快”的。

附带注意,如果您还想搜索非案例敏感性,则可以使用 .range(of:of:searchedkey,options:.caseinsentive)(甚至放置本地参数: .range。 (of:searchedkey,选项:.caseinsentive,locale:.current)

。小心nsfoundation中的 length 的计数可能是相同的。 /代码>直接。

This could be done like this:

First initialize the attributed string with default attributes:

let content = NSMutableAttributedString.init(string: model.name)
content.addAttribute(NSAttributedString.Key.font, value: UIFont.sfProTextRegular(size: 15), range: NSRange.init(location: 0, length: content.length))

//Here, if you don't want to use the `NSRange`, you can use instead:
let content = NSMutableAttributedString(string: model.name, attributes: [NSAttributedString.Key.font: UIFont.sfProTextRegular(size: 15)])

Then, to add special effects for search, you can use:

Solution 1:

let searchRange = (content.string as NSString).range(of: searchedKey)
if searchRange.location != NSNotFound {
    content.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: searchRange)
}

Solution2:

if let search = content.string.range(of: searchedKey) {
    let searchNSRange = NSRange(search, in: content.string)
    content.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: searchNSRange)
    }

The difference between Solution1 & Solution2, is the use of range(of:) applied on a NSString or a String, it will output a NSRange or a Range. Since NSAttributedString uses NSRange, solution one might be "quicker" to write.

Side note, if you also want to search for non-case sensitive, you can use .range(of: searchedKey, options: .caseInsensitive) (and even put the local parameter: .range(of: searchedKey, options: .caseInsensitive, locale: .current).

I noticed on your code NSRange.init(location: 0, length: boldUnderlined.string.count), be careful the count on Swift might no be the same of length in NSFoundation. Ie UTF8 vs UTF16 counting. So use string.utf16.count instead, or length directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文