我在 iOS 7 中使用 Text Kit 来创建富文本编辑器。以下是问题。
首先,字体大小为 16:
然后我使用代码插入图像:
// Image Attachment
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
[imageAttachment setImage:image];
imageAttachment.bounds = CGRectMake(0, 0, width, height);
[self insertTextAttachment:imageAttachment];
和
- (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];
}
然而,插入图片后,文字字体意外改变:
我尝试使用以下代码来解决问题(在 insertTextAttachment: 方法中添加):
[currentContent setAttributes:@{NSFontAttributeName: [UIFont fontWithName:kOFontDefault size:16.0]} range:NSMakeRange(mEditingRange.location + newContent.length, 1)];
问题已部分修复,新行中的新文本具有正确的字体,但图像旁边的文本仍然是错误:
任何人都可以帮忙吗?