ios - 当自定义属性应用于属性字符串范围时,键入属性在该范围内不具有自定义属性,为什么会这样?
问题描述
我正在为选定的文本应用自定义属性,效果很好,但是当我在应用的属性文本中修改文本时,新输入的文本没有自定义属性。如果这是一种标准行为,那么我应该怎么做才能实现我想要的行为。
我在做什么:
- 在 UiTextView 中输入文本,例如:“This is a sample text”
- 选择所有文本
- 应用自定义属性
- 记录属性文本
日志输出
这是一个示例文本{ MyCustomAttributeKey = CustomAttributeValue; NSFont = "字体系列:\".SFUIText\";字体粗细:正常;字体样式:正常;字体大小:14.00pt";NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\ n 112L,\n
140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n
308L,\n 336L\n), DefaultTabInterval 0, 块 (\n), 列表 (\n n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; }
- 现在在文本之间插入文本,在这里,例如“修改”,修改后的完整文本:“This a modified sample text”
- 记录属性文本
现在我得到日志输出(日志被格式化以清晰)
这是一个 { MyCustomAttributeKey = CustomAttributeValue; NSFont = "字体系列:\".SFUIText\";字体粗细:正常;字体样式:正常;字体大小:14.00pt";NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\ n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, 块 (\n), 列表 (\n n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; }
修改 { NSFont = " font-family: \".SFUIText\"; font-weight: normal; font-style: normal; font-size: 14.00pt"; NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\ n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, 块 (\n), 列表 (\n n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; }
示例文本{ MyCustomAttributeKey = CustomAttributeValue; NSFont = "字体系列:\".SFUIText\";字体粗细:正常;字体样式:正常;字体大小:14.00pt";NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n 28L,\n 56L,\n 84L,\ n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, 块 (\n), 列表 (\n n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0"; }
我的预期输出是将自定义属性应用于新输入的文本,但事实并非如此。如何做到这一点?
提前致谢。
我的 ViewController 和代码:
// ViewController.swift
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
let customAttributeStringKey: NSAttributedStringKey = NSAttributedStringKey.init(rawValue: "MyCustomAttributeKey")
@IBOutlet weak var textV: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func applyCustomAttribute(_ sender: Any) {
let attributedString : NSMutableAttributedString = textV.attributedText.mutableCopy() as! NSMutableAttributedString
attributedString.addAttribute(customAttributeStringKey, value: "CustomAttributeValue", range:textV.selectedRange)
textV.attributedText = attributedString
}
@IBAction func logAttribute(_ sender: Any) {
print(textV.attributedText.description)
}
}
PS:使用 NSAttributedStringKey.underlineStyle (而不是自定义属性)进行实验,
attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range:textV.selectedRange)
在这种情况下,修改后的文本也会获得 underlineStyle 属性,那么 customAttribute 有什么问题呢?
编辑:进展
我尝试在 textview 侦听器方法中设置键入属性,如下所示,这部分工作......但是有一个但是
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(isApplyAttributesClicked){ //this flag is true when we have applied attributes by clicking Apply attribute button
textV.typingAttributes = [customAttributeStringKey.rawValue:"CustomAttributeValue"]
}
return true
}
但是现在的问题是,每当从键盘选择预测或自动更正时,下一个键入单词的键入属性就没有自定义属性。任何帮助将不胜感激。
解决方案
推荐阅读
- c# - 如何通过html敏捷包获取特定的表格数据
- firebase - 通过电子表格将谷歌表单同步到 Firebase
- r - 在 R 数据框中提取具有共同值的行
- webpack - Webpack url 图像不会显示在 dev-server localhost 中
- android - 获取结果 - Kotlin startActivityForResult 不提供任何结果
- vb.net - 如何在搜索文本框中选择具有特定值/行的数据?
- azure - Azure DevOps 管道触发变量
- java - 从 Spring Security 中的另一个控制器手动调用 failureHandler?
- python - 验证损失方式大于 1
- java - 如何在迭代时向 Json 对象添加新属性