"NSAttributedString initWithData: options: documentAttribute: error:"未按预期工作



我正在尝试用html字符串填充一个uitableview。获取字符串,将它们放入牢房等。一切都可以。我想更改我的nsattributedText的字体和字体尺寸。我写了下面的代码。

如果我的uifont在词典中,我通过第一个nslog检查了代码;因此在词典中。仍然没问题。但是我的字体和字体尺寸在运行时没有变化。没有控制台错误。只是不工作。

第二个NSLOG用于检查属性的字体和大小。它说,我的字符串是带有字体:12 的" Times New Roman"。但是,正如您将在代码中看到的那样,我正在尝试将其" Verdana",尺寸为15

有人可以帮我吗?谢谢。

ps:我将字符串放在某些对象中。 obj.name 是我想写入表单元格的字符串。
HTML字符串具有一些不需要的标签,因此功能 [self clearthehtml:obj.name] 函数正在清除它们。

    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
    UIFont *font = [UIFont fontWithName:@"Verdana" size:15];
    [attributes setObject:font forKey:NSFontAttributeName];
    NSString *s = [self clearTheHtml:obj.name];
    NSLog(@"%@", [attributes objectForKey:NSFontAttributeName]); //to see if my font is not in the dictionary. 
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[s dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:&attributes error:nil];
    NSLog(@"%@", attributedString);
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
    cell.textLabel.attributedText = attributedString;

编辑:

这是NSString *S:

2014-01-25 07:38:04.527 KadirB[7483:70b] 
''Narakas 2013 Brachial Plexus Surgery Symposıum'' 23-25 Mayıs 2013 tarihleri arasında Montreux, İsviçre de yapıldı. Brakial Plexus sorunlarına yönelik son gelişmeler toplantıda tartışıldı.

几天前我遇到了同样的问题。我通过简单地更改实施方式解决了:

...
NSMutableAttributedString *attributedString = ...;
[attributedString addAttributes:@{NSFontAttributeName: font} range:NSMakeRange(0, attributedString.length)];

我希望它也对您有帮助。

最新更新