swift - Swift 在 addAttribute 中设置当前字体
问题描述
我有一个加号按钮来增加字体大小和一个减号按钮来减小字体大小。现在我尝试使用搜索栏在我的文本视图中突出显示单词。到目前为止它工作得很好,但不幸的是没有保留当前的字体大小,而是将其恢复为默认字体大小。我的解决方案是调用 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
}
}
}
}
解决方案
推荐阅读
- cakephp-3.0 - Cakephp 3 允许在 BelongsToMany 中重复
- mysql - 如何读取真正大的 JSON 文件并使用 node.js 将该文件的数据插入 MYSQL 数据库?
- mono - 由于所需的资产似乎正在使用中而导致单体开发失败……单独使用?
- reactjs - 从 create-react-app 切换到 webpack。现在如果使用地址栏访问路由将不会加载
- time - 如何测量单元格的时间并将输出写入 Jupyter 中的文件?
- python - asyncio 任务(apache 光束管道)在 python 中同步运行
- docker - 如何将 nginx X-Forwarded-Port 配置为原始请求端口
- javascript - 为什么`1vh` CSS大小解析为屏幕上不同的像素大小
- rtf - 如何在 rtf 模板中使用 SQL 的 INIT_CAP 功能?
- angular - 当您使用 Cloud Run 作为后端时,在哪里部署 Angular 前端?