swift - UITextView 样式在我更改字距时重置
问题描述
当我更改字距时,样式会重置。例如,当我更改文本大小或颜色时,它被保存,但是当我更改字距时,样式 UITextView 被重置
@IBAction func sizeTextEdit(_ sender: Any) {
self.textOne?.font = UIFont.systemFont(ofSize: CGFloat(sizeText.value * 1))
}
@IBAction func kernTextEdit(_ sender: Any) {
let textString = textOne.text
let attrs: [NSAttributedString.Key : Any] = [.kern: kernText.value]
textOne?.attributedText = NSAttributedString(string: textString!, attributes: attrs)
}
正如你在第一个截图中看到的,我增加了字体,然后我增加了字母之间的距离,字体大小被重置
解决方案
该font
属性仅更改该属性的字体text
。TheattributedText
是一个不同的属性,所以你也需要font
为它定义:
let attrs: [NSAttributedString.Key : Any] = [.kern: kernText.value,
.font: UIFont.systemFont(ofSize: CGFloat(sizeText.value * 1))]
textOne?.attributedText = NSAttributedString(string: textString!, attributes: attrs)
附带说明一下,如果您想做这样的事情,最好坚持一个属性,在这种情况下attributedText
,否则您必须使它们保持同步。
推荐阅读
- mysql - 如何解决 Mysql DB Error changed account admin access denied 问题?
- browser - 带有电子JS的自动化机器人和?
- java - 如何将令牌从 Nestjs 请求标头传递到 Spring Boot 标头
- javascript - 使标题具有响应性、粘性并位于中心 + 将社交媒体图标向右对齐 + 导航栏在减小其宽度时形成 2 行
- vue.js - 如何在没有 nuxt 的情况下从 vue 项目中生成 SSG
- flutter - 从可选数据表中的数据源中删除对象
- c# - 将 ProductService.Host 升级到 4.3 和 .net 5 后出现错误
- database - MongoDB,在一个集合中创建数百万个文档的索引时出错
- oracle - 如何按时间给出客户的状态
- python - Python错误'NoneType'对象没有属性'post'jira