无法将"Range <String.Index>"类型的值(又名"Range <String.CharacterView.Index>")转换为预期的参数类型"NSRange"(又名"_NSRange")

Bha*_*dy 8 nsattributedstring ios swift

我试图用属性String替换子字符串.以下是我的代码.

let searchText = self.searchBar.text!
let name = item.firstName ?? ""
let idNo = "Employee Id. \(item.employeeId ?? "NA")"

if let range = name.range(of: searchText, options: String.CompareOptions.caseInsensitive, range: nil, locale: nil)
{
    let attributedSubString = NSAttributedString.init(string: name.substring(with: range), attributes: [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 17)])
    let normalNameString = NSMutableAttributedString.init(string: name)
    normalNameString.mutableString.replacingCharacters(in: range, with: attributedSubString)
    cell.name.attributedText = normalNameString
}
else
{
    cell.name.text = name
}
Run Code Online (Sandbox Code Playgroud)

我收到编译时错误:

"无法将'Range'类型的值(又称'Range')转换为预期的参数类型'NSRange'(又名'_NSRange')".

我应该在这里改变什么?

Rei*_*ian 13

您可以使用NSStringNSRange,也需要更改此行normalNameString.mutableString.replacingCharacters(in: range, with: attributedSubString) 并使用此,normalNameString.replaceCharacters(in: nsRange, with: attributedSubString)因为mutableString是NSMutableString,并且replacingCharacters期望String而不是NSAttributtedString

完整代码

    let nsRange = NSString(string: name).range(of: searchText, options: String.CompareOptions.caseInsensitive)

    if nsRange.location != NSNotFound
    {
        let attributedSubString = NSAttributedString.init(string: NSString(string: name).substring(with: nsRange), attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 17)])
        let normalNameString = NSMutableAttributedString.init(string: name)
        normalNameString.replaceCharacters(in: nsRange, with: attributedSubString)
        cell.name.attributedText = normalNameString
    }
    else
    {
        cell.name.text = name
    }
Run Code Online (Sandbox Code Playgroud)


vik*_*ati 5

请这样做,

第一的,

extension String {

    func nsRange(from range: Range<String.Index>) -> NSRange {
        let from = range.lowerBound.samePosition(in: utf16)
        let to = range.upperBound.samePosition(in: utf16)
        return NSRange(location: utf16.distance(from: utf16.startIndex, to: from),
                       length: utf16.distance(from: from, to: to))
    }
}
Run Code Online (Sandbox Code Playgroud)

之后你有一个标签和这样的字符串,

private func setAttributedLabel() {

    let lableText = UILabel()

    let strTitle = "Basic string Double tap" // your main string
    let title:NSMutableAttributedString = NSMutableAttributedString(string: strTitle)

    // give attribute to basic string
    title.addAttributes([NSForegroundColorAttributeName:UIColor.blue ,NSFontAttributeName:UIFont.boldSystemFont(ofSize: 15)], range: NSRange.init(location: 0, length: title.length))
    lableText.attributedText = title

    // give attribute to your range string
    let rangeOfString = lableText.text?.range(of: "Double tap") // this string is subString of strTitle
    title.addAttributes([NSForegroundColorAttributeName:UIColor.gray,NSFontAttributeName:UIFont.systemFont(ofSize: 15)], range: strTitle.nsRange(from: rangeOfString!))
    lableText.attributedText = title

}
Run Code Online (Sandbox Code Playgroud)

我希望它会有所帮助。