我有一个加号按钮可以增加字体大小,而有一个减号按钮可以减小字体大小。现在,我尝试使用搜索栏突出显示textview中的单词。到目前为止,它的效果很好,但不幸的是,当前字体大小并未保留,但将其恢复为默认字体大小。我的解决方案是调用addAttributes并指定.font,不幸的是,我不知道要在值和范围中输入什么。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
do
{
if let searchString = searchBar.text, let baseString = textNotiz.text {
let attributed = NSMutableAttributedString(string: baseString)
attributed.addAttribute(.font, value: "??", range: "??" )
if searchString.count > 0 && baseString.count > 0 {
let regex = try! NSRegularExpression(pattern: searchString,options: .caseInsensitive)
for match in regex.matches(in: baseString, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: baseString.count)) as [NSTextCheckingResult] {
attributed.addAttribute(.backgroundColor, value: UIColor.yellow, range: match.range)
}
self.textNotiz.attributedText = attributed
} else {
textNotiz.attributedText = attributed
}
}
}
}