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

 没有变成王子的青蛙 发布于 2023-01-31 14:46

我正在尝试使用HTML字符串填充UITableView.获取字符串,将它们放入单元格等等一切都很好.我想改变我的NSAttributedText的字体和字体大小.我写了下面的代码.

我通过第一个NSLog检查了代码,如果我的UIFont按预期在字典中; 所以它在字典里.还是没问题.但我的字体和字体大小在运行时没有变化.没有控制台错误.只是不工作.

第二个NSLog用于检查attributionString的字体和大小.它说,我的字符串是"Times New Roman",fontSize:12.但正如您将在代码中看到的那样,我正试图将它变成尺寸为15的"Verdana"

任何人都可以帮我这个吗?谢谢.

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?.

Gianluca Tra.. 5

几天前我遇到了同样的问题.我通过简单地改变实现来解决这个问题:

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

我希望它对你也有帮助.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有