Ios 删除html标记并显示在UIC中

Ios 删除html标记并显示在UIC中,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我在api响应中得到了类似于Html标记的/br>p>。我想在UILabel 我所做的是: NSString *STR_api = [NSString StingWithFormat@:"%@",[API_res valueforkey:@"description"]]; STR_api = [STR_api StringByreplacingaccurancesofString @"&nbsp" with string@""]; 我想要的是,在web门户中,它以粗体显示,段落是否可以

我在api响应中得到了类似于Html标记的/br>p>。我想在
UILabel

我所做的是:

NSString *STR_api = [NSString StingWithFormat@:"%@",[API_res valueforkey:@"description"]];
STR_api = [STR_api StringByreplacingaccurancesofString @"&nbsp" with string@""];
我想要的是,在web门户中,它以粗体显示,段落是否可以通过格式化上述响应在UILabel中显示


提前感谢

使用下面的
UILabel
格式控件,该控件基于类似HTML的iOS标记提供富文本格式。

使用下面的
UILabel
格式控件,该控件基于类似HTML的iOS标记提供富文本格式。

我尝试了来自

我找到了解决办法,效果很好

NSString *strHTML = @"S.Panchami 01.38<br>Arudra 02.01<br>V.08.54-10.39<br>D.05.02-06.52<br> <font color=red><u>Festival</u></font><br><font color=blue>Shankara Jayanthi<br></font>";
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[strHTML dataUsingEncoding:NSUTF8StringEncoding]
                                                            options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                      NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
                                                 documentAttributes:nil
                                                              error:nil];
NSLog(@"html: %@",strHTML);
NSLog(@"attr: %@", attrStr);
NSLog(@"string: %@", [attrStr string]);
NSString *finalString = [attrStr string];
NSLog(@"The finalString is - %@",finalString);
最后一串

The finalString is - S.Panchami 01.38
Arudra 02.01
V.08.54-10.39
D.05.02-06.52
Festival
Shankara Jayanthi
现在从字符串中删除

选项1

NSRange range;
while ((range = [finalString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
finalString = [finalString stringByReplacingCharactersInRange:range withString:@""];

我尝试了来自

我找到了解决办法,效果很好

NSString *strHTML = @"S.Panchami 01.38<br>Arudra 02.01<br>V.08.54-10.39<br>D.05.02-06.52<br> <font color=red><u>Festival</u></font><br><font color=blue>Shankara Jayanthi<br></font>";
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[strHTML dataUsingEncoding:NSUTF8StringEncoding]
                                                            options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                      NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
                                                 documentAttributes:nil
                                                              error:nil];
NSLog(@"html: %@",strHTML);
NSLog(@"attr: %@", attrStr);
NSLog(@"string: %@", [attrStr string]);
NSString *finalString = [attrStr string];
NSLog(@"The finalString is - %@",finalString);
最后一串

The finalString is - S.Panchami 01.38
Arudra 02.01
V.08.54-10.39
D.05.02-06.52
Festival
Shankara Jayanthi
现在从字符串中删除

选项1

NSRange range;
while ((range = [finalString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
finalString = [finalString stringByReplacingCharactersInRange:range withString:@""];
请尝试以下代码:

NSString *strDescription = [NSString stringWithFormat:@"%@",[API_res valueforkey:@"description"]];

self.lblDescription.attributedText = [self getData:strDescription];
将HtmlString转换为字符串

-(NSAttributedString *)getData:(NSString *)str
{
    NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
    NSAttributedString *decodedString;
    decodedString = [[NSAttributedString alloc] initWithData:stringData
                                                     options:options
                                          documentAttributes:NULL
                                                       error:NULL];
    return decodedString;
}
请尝试以下代码:

NSString *strDescription = [NSString stringWithFormat:@"%@",[API_res valueforkey:@"description"]];

self.lblDescription.attributedText = [self getData:strDescription];
将HtmlString转换为字符串

-(NSAttributedString *)getData:(NSString *)str
{
    NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
    NSAttributedString *decodedString;
    decodedString = [[NSAttributedString alloc] initWithData:stringData
                                                     options:options
                                          documentAttributes:NULL
                                                       error:NULL];
    return decodedString;
}

您可以使用
NSAttributedString
解析HTML字符串,并将其显示在
UILabel
中。可能存在的重复项请将您的HTML字符串显示给我好吗?必须具有资产分配经验,且不超过3年,且熟练掌握\n\n@user3182143我的答案是否能帮助您解决Sanjay?您可以使用
NSAttributedString
解析HTML字符串并将其显示在
UILabel
中。可能重复的请向我展示您的HTML字符串?必须拥有不到3年的资产配置经验和出色的熟练程度\n\n@user3182143我的答案是否能帮助您解决Sanjay?但仍然-显示在我的标签中其他标签消失了:)但仍然-显示在我的标签中其他标签消失了:)