0

I am new in iOS and I am facing problem regarding to add HTML content in NSMutableArray in UITableView.

I am using code like this

 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;

But this code remove tag not adding HTML content.I am getting value in array like this "<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>"

My cell for row 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;

} 

How to solve this issue. Thanks in Advance!

6
  • 1
    Couldn't understand your problem. Commented Feb 6, 2017 at 7:27
  • @InderKumarRathore From Web Service I am getting some text in the HTML format like <p></p> for that In using code as in the question. But the problem I am facing is the text is not showing in correct HTML format. Commented Feb 6, 2017 at 7:32
  • @InderKumarRathore I am getting value in array like this "<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>". Commented Feb 6, 2017 at 7:35
  • Why dont you use attributed string with option [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] to parse HTML content? Commented Feb 6, 2017 at 7:44
  • @Swift_Guru Can I add it in NSMutableArray?? Commented Feb 6, 2017 at 8:33

1 Answer 1

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

set font which you want to set in font attribute

Sign up to request clarification or add additional context in comments.

15 Comments

How to add this atteStr in NSMutableArray?
No need to add in array. just set this attrStr wherever you want i.e.lbl.attributedtext = attrStr
Done Bro Thank you.
It is displaying Only one text in table view . I don't have string I have Array and I need to show it into table view.
Show me your array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.