【问题标题】:Setting particular font-size at the cursor point in UITextView with attributed text using Swift使用 Swift 在 UITextView 中的光标处设置特定的字体大小,并带有属性文本
【发布时间】:2019-12-02 23:36:30
【问题描述】:

我正在尝试使用 UITextView 构建 RichTextEditor。 当我在文本末尾应用不同的字体大小时,它无法为输入的新文本更新字体大小。

我目前有以下代码,我试图让它工作:

富文本出口是
@IBOutlet weak var richTextView: UITextView!

尝试更改字体的代码是

func setAttributesForSelectedTextRange(_ attributes: [NSAttributedString.Key: Any]) {
    if let text = richTextView.attributedText.mutableCopy() as? NSMutableAttributedString, let selectedRange = richTextView.selectedTextRange {
        let cursorPosition = richTextView.offset(from: richTextView.beginningOfDocument, to: selectedRange.start)
        text.addAttributes(attributes, range: richTextView.selectedRange)
        if selectedRange.end == richTextView.endOfDocument {
            text.append(NSAttributedString(string: "", attributes: attributes))
        }

        richTextView.attributedText = text
    }
}

我尝试在末尾添加一个带有新属性的空字符串,但没有成功。 上例中如何实现设置属性变化?

【问题讨论】:

    标签: swift uitextview nsattributedstring rich-text-editor nsmutableattributedstring


    【解决方案1】:

    它无法为新输入的文本更新字体大小。

    设置用户将要输入的文本的属性的方法是设置UITextView的typingAttributes

    https://developer.apple.com/documentation/uikit/uitextview/1618629-typingattributes

    【讨论】:

    • 谢谢。这就像一个魅力。我不知道那个参数的存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多