首页 > 解决方案 > 当自定义属性应用于属性字符串范围时,键入属性在该范围内不具有自定义属性,为什么会这样?

问题描述

我正在为选定的文本应用自定义属性,效果很好,但是当我在应用的属性文本中修改文本时,新输入的文本没有自定义属性。如果这是一种标准行为,那么我应该怎么做才能实现我想要的行为。

我在做什么:

  1. 在 UiTextView 中输入文本,例如:“This is a sample text”
  2. 选择所有文本
  3. 应用自定义属性
  4. 记录属性文本

日志输出

这是一个示例文本{ 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"; }

  1. 现在在文本之间插入文本,在这里,例如“修改”,修改后的完整文本:“This a modified sample text”
  2. 记录属性文本

现在我得到日志输出(日志被格式化以清晰)

这是一个 { 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
    }

但是现在的问题是,每当从键盘选择预测或自动更正时,下一个键入单词的键入属性就没有自定义属性。任何帮助将不胜感激。

标签: iosswiftuitextviewnsattributedstringnsmutableattributedstring

解决方案


推荐阅读