我在TableView
中实施了一个搜索栏。现在我想强调一下结果。例如,如果我输入了两个字母,那么这两个字母应该在从搜索栏下拉的结果TableView
中突出显示。任何人都可以帮我这样做吗?我知道我应该使用自定义单元格,但我没有实现它。
答案 0 :(得分:12)
以下是在tableview
中归档文本的第二种方法let initialtext = "Hello World" let attrString: NSMutableAttributedString = NSMutableAttributedString(string: initialtext) let range: NSRange = (initialtext as NSString).rangeOfString(("World" , options:NSStringCompareOptions.CaseInsensitiveSearch]) attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: range) cell!.textLabel?.attributedText = attrString
答案 1 :(得分:9)
@Vijay对这篇文章有一个很好的正确答案。
我通过创建一个接受searchString
和你要修改的字符串的函数来为我自己的目的(在我的搜索结果中加粗文本)略微修改了这个 -
resultString
- 并返回可应用于attributedString
的{{1}}。
我也对UILabel
属性进行了此检查,因此无论我在搜索栏中输入什么内容,我的字符串都会匹配字符而非案例(此要求可能会有所不同,具体取决于您的使用情况) - 案例)。
lowercaseString
注意:我有一个自定义粗体字体可供使用,但你总是可以使用func boldSearchResult(searchString: String, resultString: String) -> NSMutableAttributedString {
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: resultString)
let pattern = searchString.lowercaseString
let range: NSRange = NSMakeRange(0, resultString.characters.count)
let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions())
regex.enumerateMatchesInString(resultString.lowercaseString, options: NSMatchingOptions(), range: range) { (textCheckingResult, matchingFlags, stop) -> Void in
let subRange = textCheckingResult?.range
attributedString.addAttribute(NSFontAttributeName, value: UIFont.customBoldedFontNameWithSize(15.0), range: subRange!)
}
return attributedString
}
或类似的东西来获得类似的效果。
然后,只需通过执行(某些变体)将结果添加到标签中:
UIFont.boldSystemFontOfSize(fontSize: CGFloat)
答案 2 :(得分:4)
您可以通过从结果字符串中查找搜索字符串范围并在该范围内添加属性字符串来实现此目的。找到下面的示例代码,
<强>目标C 强>
NSString *searchTerm = /* SEARCH_TERM */;
NSString *resultText = /* YOUR_RESULT_TEXT */;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:resultText];
NSString *pattern = [NSString stringWithFormat:@"(%@)", searchTerm];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:kNilOptions error:nil];
NSRange range = NSMakeRange(0, resultText.length);
[regex enumerateMatchesInString:resultText options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange subStringRange = [result rangeAtIndex:1];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:subStringRange];
}];
Swift(TESTED)
let searchTerm = "" /* SEARCH_TERM */
let resultText = "" /* YOUR_RESULT_TEXT */
let attributedString:NSMutableAttributedString = NSMutableAttributedString(string: resultText)
let pattern = "(\(searchTerm))"
let range:NSRange = NSMakeRange(0, resultText.characters.count)
let regex = try! NSRegularExpression( pattern: pattern, options: NSRegularExpressionOptions())
regex.enumerateMatchesInString(
resultText,
options: NSMatchingOptions(),
range: range,
usingBlock: {
(textCheckingResult, matchingFlags, stop) -> Void in
let subRange = textCheckingResult?.range
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: subRange!)
}
)
答案 3 :(得分:0)
试图使它尽可能的薄。 该函数返回带有出现的搜索文本(以粗体显示)的属性字符串。 Swift 4.2 +
private func boldedString(with baseString: String, searchString: String, fontSize: CGFloat) -> NSAttributedString? {
guard let regex = try? NSRegularExpression(pattern: searchString, options: .caseInsensitive) else {
return nil
}
let attributedString = NSMutableAttributedString(string: baseString)
let boldFont = UIFont.systemFont(ofSize: fontSize, weight: .bold)
regex
.matches(in: baseString, options: .withTransparentBounds,
range: NSRange(location: 0, length: baseString.utf16.count))
.forEach {
attributedString.addAttributes([.font: boldFont], range: $0.range)
}
return attributedString
}
在单元格中,您需要添加以下代码以进行配置:
func configure(with text: String, searchText: String?) {
if let boldedAddress = boldedString(with: text,
searchString: searchText,
fontSize: titleLabel.font.pointSize) {
titleLabel.attributedText = boldedAddress
} else {
titleLabel.text = locationInfo.location.address
}
}
答案 4 :(得分:0)
Vijayvir的答案的Swift 5版本:
let initialtext = "Hello, World!"
let attrString: NSMutableAttributedString = NSMutableAttributedString(string: initialtext)
let range = (initialtext as NSString).range(of: "World", options: .caseInsensitive)
attrString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: range)
cell.textLabel?.attributedText = attrString
答案 5 :(得分:0)
此解决方案适用于转义字符,例如( {} []()'“ )用SWIFT 5编写的此解决方案。
func generateAttributedString(with searchTerm: String, targetString: NSAttributedString) -> NSAttributedString? {
let attributedString = NSMutableAttributedString(attributedString: targetString)
do {
let regex = try NSRegularExpression(pattern: NSRegularExpression.escapedPattern(for: searchTerm).trimmingCharacters(in: .whitespacesAndNewlines).folding(options: .regularExpression, locale: .current), options: .caseInsensitive)
let range = NSRange(location: 0, length: targetString.string.utf16.count)
attributedString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.clear, range: range)
for match in regex.matches(in: targetString.string.folding(options: .regularExpression, locale: .current), options: .withTransparentBounds, range: range) {
attributedString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.yellow, range: match.range)
}
return attributedString
} catch {
NSLog("Error creating regular expresion: \(error)")
return nil
}
}