NSAttributedString changed font unexpectedly after inserting Image

后端 未结 4 1406
死守一世寂寞
死守一世寂寞 2021-01-02 20:54

I am using Text Kit in iOS 7 to create a rich text editor. Following is the problem.

At first, then font size is 16:

相关标签:
4条回答
  • 2021-01-02 21:00

    I had a similar problem. I tried setting and adding attributes to my NSMutableAttributedString and these were never reflected after setting the UITextView's attributedText property. The text's font size was always smaller.

    I finally got this to work by storing the UITextView's font property in a local variable right before I set it's attributedText property, and then I set it's font property to the local variable right after.

    0 讨论(0)
  • 2021-01-02 21:18

    set the typing attributes property of the textView

    0 讨论(0)
  • 2021-01-02 21:20

    This issue caused me also some headache. The problem is that you replace the range of the NSAttributedString currentContent with a new NSAttributedString. However, this overwrites ALL attributes which were specified on currentContent, including the font. When you start typing after the NSTextAttachment, it will look for the font of newContent but there is no font specified. Thus, it uses the default font. To fix it, you can add this line to your code:

    [newContent 
        addAttribute:NSFontAttributeName 
        value:[UIFont fontWithName:kOFontDefault size:16.0]
        range:NSMakeRange(0, newContent.length)]; 
    
    0 讨论(0)
  • 2021-01-02 21:21

    This is apparently a bug. Here is a workaround I found. Adding the correct font attributes one more time after inserting image will fix it.

    self.contentTextView.textStorage.addAttributes(self.contentTextView.typingAttributes, range: mEditingRange)
    
    0 讨论(0)
提交回复
热议问题