Ios 在swift attributedString中设置字体将删除粗体强调

Ios 在swift attributedString中设置字体将删除粗体强调,ios,swift,fonts,nsattributedstring,Ios,Swift,Fonts,Nsattributedstring,我在获取AttributeText以同时管理字体和粗体时遇到问题。 我的场景是一个具有可滚动文本视图的视图控制器,该视图接受属性文本 属性是通过使用下面的函数转换一个简单的html字符串(ul和b)来创建的 没关系。当我稍后将文本设置为attributedData,然后设置字体时,问题出现在我的视图控制器中 先执行文本,然后执行字体:删除显示的粗体。 执行字体,然后执行文本:停止字体工作 我怀疑粗体在字符串中不是表示为“粗体”,而是表示为使用粗体字体。 当我设置文本,然后设置字体时,它将覆盖属性

我在获取AttributeText以同时管理字体和粗体时遇到问题。 我的场景是一个具有可滚动文本视图的视图控制器,该视图接受属性文本

属性是通过使用下面的函数转换一个简单的html字符串(ul和b)来创建的

没关系。当我稍后将文本设置为attributedData,然后设置字体时,问题出现在我的视图控制器中

先执行文本,然后执行字体:删除显示的粗体。 执行字体,然后执行文本:停止字体工作

我怀疑粗体在字符串中不是表示为“粗体”,而是表示为使用粗体字体。
当我设置文本,然后设置字体时,它将覆盖属性中的任何字体(包括粗体)。 当我设置字体,然后是文本时,属性中的字体是最后一个,它是用默认字体设置的,但正确地切换到字体的粗体版本

有没有办法更改attributedString上的字体并保持粗体/斜体? 不是我的首选位置,但是可以在NSAttributedString函数的选项中设置字体吗

func html2AttributedString(fromString: String) -> NSAttributedString {
    var returnString: NSAttributedString

    do {
        let attributedData = fromString.dataUsingEncoding(NSUnicodeStringEncoding)
        let tempAttributedString = try NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
        returnString = tempAttributedString
    } catch {
        let tempString = "Unknown String"
        let attributedData = tempString.dataUsingEncoding(NSUnicodeStringEncoding)
        let tempAttributedString = try! NSAttributedString(data: attributedData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
        returnString = tempAttributedString
    }

    return returnString
}
在视图控制器中设置文本

    contentTextView.attributedText = descriptionTextAttributed
    contentTextView.textColor = UIColor.blackColor()
    if let font = UIFont(name: "StoneSans", size: 20.0) {
        contentTextView.font = font
    }
更新:感谢Wain的评论。 我可以看到更改字体的三个级别:html、转换时和转换完成时

Wain已经提出了在AttributeText转换后在AttributeText中更改它的方法,但是我也受到CPU的限制,对于用户来说,这种方法的速度会非常慢

我已经尝试过用编程的方式为html做这件事,但它不起作用。如果有人能帮忙,我的插件可能需要修复:

    //let tempString = "<style> {font-family: sans-serif;}</style>" + fromString
    //let tempString = "<body style='font-family=sans-serif'>" + fromString
如果有人能修复我前面两种改变字体方法的代码,我将不胜感激


蒂姆。

没错,粗体和斜体信息是字体信息的一部分。使用
NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType
使用默认字体(或HTML中指定的任何字体)

因此,您需要编辑字体,而不仅仅是替换字体,例如:

NSMutableAttributedString *attributedString = [self mutableCopy];

[attributedString beginEditing];
[attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(UIFont *value, NSRange range, BOOL *stop) {

    NSString *fontName = [value.fontName lowercaseString];
    UIFont *destinationFont = [UIFont XXXX];

    if ([fontName rangeOfString:@"bold"].location != NSNotFound) {
        destinationFont = [UIFont BOLD_XXXX];
    } else if ([fontName rangeOfString:@"italic"].location != NSNotFound) {
        destinationFont = [UIFont ITALIC_XXXX];
    }

    [attributedString removeAttribute:NSFontAttributeName range:range];
    [attributedString addAttribute:NSFontAttributeName value:destinationFont range:range];
}];
[attributedString endEditing];

谢谢我看看现在还有没有其他好主意。我之所以对此犹豫不决,是因为我在许多幻灯片上从html进行翻译,并且我试图最小化属性更改,因为这会明显减慢我的应用程序的速度。你不能在后台这样做,因为它使用web架构。唯一的其他选择是在HTML中添加格式,我想谢谢——我已经添加到了原始问题中。我对编辑html不抱太大希望,因为我正在切换到自定义字体,希望html找不到它。转换过程中的文档属性看起来不错,但我无法获得正确的格式。这是一个高价值的post dude!:O
NSMutableAttributedString *attributedString = [self mutableCopy];

[attributedString beginEditing];
[attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(UIFont *value, NSRange range, BOOL *stop) {

    NSString *fontName = [value.fontName lowercaseString];
    UIFont *destinationFont = [UIFont XXXX];

    if ([fontName rangeOfString:@"bold"].location != NSNotFound) {
        destinationFont = [UIFont BOLD_XXXX];
    } else if ([fontName rangeOfString:@"italic"].location != NSNotFound) {
        destinationFont = [UIFont ITALIC_XXXX];
    }

    [attributedString removeAttribute:NSFontAttributeName range:range];
    [attributedString addAttribute:NSFontAttributeName value:destinationFont range:range];
}];
[attributedString endEditing];