插入Image后,NSAttributedString意外更改了字体

Han*_*nXu 6 iphone ios ios7 textkit

我在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];
Run Code Online (Sandbox Code Playgroud)

- (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];
}
Run Code Online (Sandbox Code Playgroud)

但是,插入图像后,文本字体意外更改: 在此输入图像描述

我尝试使用以下代码来解决问题(在insertTextAttachment:方法中添加):

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

问题部分修复,新行中的新文本使用正确的字体,但图像旁边的文本仍然是错误:

在此输入图像描述

有人可以帮忙吗?

use*_*977 10

我遇到了类似的问题.我尝试设置并向我添加属性NSMutableAttributedString,这些在设置UITextView's attributedText属性后从未反映过.文本的字体大小始终较小.

我终于通过UITextView's font在设置它的attributedText属性之前将属性存储在局部变量中来实现这一点,然后我将它的font属性设置为后面的局部变量.

  • 我不能通过互联网亲你,这是一种耻辱. (3认同)