簡體   English   中英

使用 Swift 在帶有屬性文本的 UITextView 中的光標點設置特定字體大小

[英]Setting particular font-size at the cursor point in UITextView with attributed text using Swift

我正在嘗試使用 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
    }
}

我嘗試使用新屬性在末尾添加一個空字符串,但這沒有奏效。 在上面的例子中如何實現設置屬性更改?

它無法更新鍵入的新文本的字體大小。

設置用戶將要鍵入的文本的屬性的方法是設置 UITextView 的typingAttributes

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM