[英]Cannot convert value of type 'NSRange' (aka '_NSRange') to expected type 'Range<Index>'(aka 'Range<String.CharacterView.Index>')
[英]Cannot convert value of type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>') to expected argument type 'NSRange' (aka '_NSRange')
我试图用属性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
}
我收到编译时错误:
“无法将'Range'类型的值(又称'Range')转换为预期的参数类型'NSRange'(又名'_NSRange')”。
我应该在这里改变什么?
您可以使用NSString
和NSRange
,还需要更改此行normalNameString.mutableString.replacingCharacters(in: range, with: attributedSubString)
并使用此行代替 normalNameString.replaceCharacters(in: nsRange, with: attributedSubString)
因为mutableString是NSMutableString,和replacingCharacters
预计字符串,而不是NSAttributtedString
完整代码(针对Swift5进行了更新)
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
}
请这样做,
第一,
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))
}
}
之后你有一个标签和字符串,
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
}
我希望它会有所帮助。
您必须使用Range<String.index>
lowerBound
和UpperBound
位置值创建NSRange
实例,如下所示。
let searchText = "Kiran"
let name = "Kiran Jasvanee"
let idNo = "Employee Id. \("24")"
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)
let startPos = name.distance(from: searchText.characters.startIndex, to: range.lowerBound)
let nsrange = NSMakeRange(startPos, searchText.characters.count)
normalNameString.replaceCharacters(in: nsrange, with: attributedSubString)
labelName.attributedText = normalNameString
}
else
{
labelName.text = name
}
add, self.searchBar.text!
而不是搜索searchtext constant
的"Kiran"
searchtext constant
和
添加item.firstName ?? ""
根据您的要求, item.firstName ?? ""
而不是"Kiran Jasvanee"
name constant
。
在Swift Range
中不是NSRange
类型。 在这里,您将通过Swift Range
类型,其中包含NSRange
,这就是您遇到错误的原因。
一种解决方法是您可以将您的name
为NSString
以获取NSRange
类型的范围,如下所示:
let range = (name as NSString).range(of: searchText, options: String.CompareOptions.caseInsensitive)
let subString = NSString(string: name).substring(with: range)
let attributedSubString = NSAttributedString.init(string:subString, attributes: [NSFontAttributeName : UIFont.boldSystemFont(ofSize: 17)])
let normalNameString = NSMutableAttributedString.init(string: name)
normalNameString.replaceCharacters(in: range, with: attributedSubString)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.