NSAttributedString插入图像后意外更改了字体 [英] NSAttributedString changed font unexpectedly after inserting Image

查看:218
本文介绍了NSAttributedString插入图像后意外更改了字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆