html - 如何在 Objective C 中的 NSMutableArray 中添加 HTML 标签

标签 html ios objective-c uitableview

我是 iOS 的新手,我面临着在 UITableView 的 NSMutableArray 中添加 HTML 内容的问题。

我正在使用这样的代码

 namearray=[[NSMutableArray alloc]init];
    namearray=[responsedict valueForKey:@"News"];

  NSMutableArray *trimmedStrings = [NSMutableArray array];
        for (NSString *string in namearray) {
            NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"<p></p>&amp;#39;"];
            NSString *trimmedString = [[string componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
            [trimmedStrings addObject:trimmedString];
        }
        namearray = trimmedStrings;

但此代码删除标签不添加 HTML 内容。我在这样的数组中获取值 "<p>&quot;I don&#39;t want to live in a lie anymore. When I was 10, my mother in a hotel in Athens, Greece.</p>"

我的单元格行 AtIndexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *STI=@"STI";
    NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewsTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.accessoryType=UITableViewCellAccessoryNone;
    }

    NSString *strImgURLAsString = [NewsImageArray objectAtIndex:indexPath.row];
    [strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *imgURL = [NSURL URLWithString:strImgURLAsString];
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imgURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (!connectionError) {
            img = [[UIImage alloc] initWithData:data];
            if (img==nil) {
                img=[UIImage imageNamed:@"userimage.png"];
            }
            cell.newsimage.image=img;

            // pass the img to your imageview
        }else{
            NSLog(@"%@",connectionError);
        }
    }];

    cell.Headlbl.text=[NSString stringWithFormat:@"%@",[headarray objectAtIndex:indexPath.row]];

    cell.bodylbl.attributedText=attrStrBody;


    cell.bodylbl.textAlignment=NSTextAlignmentCenter;
    cell.bodylbl.textColor=[UIColor whiteColor];
    [cell.bodylbl setFont:[UIFont fontWithName:@"Arial" size:16]];
 //   cell.randrid.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
    return cell;

} 

如何解决这个问题。 提前致谢!

最佳答案

NSString *aux = [webServiceArr objectAtIndex:indexPath.row];
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[aux dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:font} documentAttributes:nil error:nil];`

在字体属性中设置想要设置的字体

关于html - 如何在 Objective C 中的 NSMutableArray 中添加 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42062293/

相关文章:

ios - 执行字符串中定义的代码

objective-c - 更改帧大小时 NSImageView 的动画未保持在原位

objective-c - OS X - 应用程序无法打开,因为当 bundle 包含新版本的第三方可执行文件时无法确认开发人员的身份

ios - 是否可以使用 iOS Developer Enterprise Program 分发使用 beta SDK 构建的内部应用程序?

ios - 为什么设置类变量 NSArray 需要我使用访问器方法?

HTML 到横向 MS Word

html - android html.fromhtml 从网络加载图像

javascript - 如何在支持触摸的浏览器中模拟带有触摸的悬停?

javascript - 如何在无需用户点击的情况下进行复制

ios - 禁用 iOS 系统声音的 AVSystemController 未找到架构 i386 错误