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:
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:
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:
Can anyone help?