Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedString changed font unexpectedly after inserting Image

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:

enter image description here

Then I insert an image using the code:

//  Image Attachment
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
[imageAttachment setImage:image];
imageAttachment.bounds = CGRectMake(0, 0, width, height);

[self insertTextAttachment:imageAttachment];

and

- (void)insertTextAttachment:(NSTextAttachment*)textAttachment {
    NSMutableAttributedString *newContent = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] mutableCopy];

    NSMutableAttributedString *currentContent = [self.contentTextView.attributedText mutableCopy];
    [currentContent insertAttributedString:[[NSAttributedString alloc] initWithString:@"\n"] atIndex:mEditingRange.location];
    [currentContent insertAttributedString:[[NSAttributedString alloc] initWithString:@"\n"] atIndex:mEditingRange.location];

    mEditingRange.location++;
    [currentContent replaceCharactersInRange:mEditingRange withAttributedString:newContent];
    [currentContent setAttributes:@{NSFontAttributeName: [UIFont fontWithName:kOFontDefault size:16.0]} range:NSMakeRange(mEditingRange.location + newContent.length, 1)];
    [self.contentTextView setAttributedText:currentContent];
}

However, after inserting the image, the text font changed unexpectedly: enter image description here

I tried to use following code to fix the problem (added in the insertTextAttachment: method):

[currentContent setAttributes:@{NSFontAttributeName: [UIFont fontWithName:kOFontDefault size:16.0]} range:NSMakeRange(mEditingRange.location + newContent.length, 1)];

Problem is partly fixed, new text in a new line is with correct font, but text right next to the image is still bug:

enter image description here

Can anyone help?

like image 960
HanXu Avatar asked Feb 12 '14 23:02

HanXu


2 Answers

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.

like image 133
user3344977 Avatar answered Oct 29 '22 12:10

user3344977


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)]; 
like image 37
Progamer Avatar answered Oct 29 '22 14:10

Progamer