NSAttributedString插入图像后意外更改了字体 [英] NSAttributedString changed font unexpectedly after inserting Image
问题描述
我正在iOS 7中使用文本工具包来创建RTF编辑器.以下是问题所在.
首先,字体大小为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)];
问题已部分修复,新行中的新文本使用正确的字体,但是图像旁边的文本仍然存在错误:
任何人都可以帮忙吗?
我遇到了类似的问题.我尝试设置属性并将其添加到我的NSMutableAttributedString
中,并且在设置UITextView's
attributedText
属性后这些属性从未反映出来.文本的字体大小总是较小.
我终于通过将UITextView's
font
属性存储在本地变量中,然后再将其设置为attributedText
属性,然后将其font
属性设置为本地变量,从而使此功能得以实现./p>
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?
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.
这篇关于NSAttributedString插入图像后意外更改了字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!