【问题标题】:How to add HTML Tags in NSMutableArray in objective c如何在目标 c 的 NSMutableArray 中添加 HTML 标签
【发布时间】:2017-02-06 07:21:34
【问题描述】:

我是 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 内容。我在这样的数组中获取值"&lt;p&gt;&amp;quot;I don&amp;#39;t want to live in a lie anymore. When I was 10, my mother in a hotel in Athens, Greece.&lt;/p&gt;"

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;

} 

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

【问题讨论】:

  • 无法理解您的问题。
  • @InderKumarRathore 从 Web 服务我得到了一些 HTML 格式的文本,例如

    用于在问题中使用代码。但我面临的问题是文本没有以正确的 HTML 格式显示。
  • @InderKumarRathore 我在这样的数组中获得价值“

    ”我不想再活在谎言中了。当我 10 岁的时候,我妈妈在雅典的一家酒店, 希腊。

    ".
  • 为什么不使用带有选项 [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] 的属性字符串来解析 HTML 内容?
  • @Swift_Guru 我可以将它添加到 NSMutableArray 中吗??

标签: html ios objective-c uitableview


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

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

【讨论】:

  • 如何在 NSMutableArray 中添加这个 atteStr?
  • 不需要添加数组。只需将此 attrStr 设置在您想要的任何位置,即 lbl.attributedtext = attrStr
  • 完成了,谢谢。
  • 它在表格视图中只显示一个文本。我没有字符串我有数组,我需要将它显示到表格视图中。
  • 给我看看你的数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
相关资源
最近更新 更多