如何从iphone中的NSString中删除HTML标签?

时间:2014-05-20 10:45:58

标签: ios iphone objective-c

我正在为我的应用程序需求工作日历功能。我今天点击日期或明天日期或其他日期需要在UITextview中显示吉祥的详细信息。我一直在尝试在文本视图中格式化字符串但是我不能解决它。我是xcode的新手。我想删除stringResonse中的HTML标签并显示在UITextview中。

我在我的代码中这样写:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

if(connection==urlConnection)

{

NSString *strResponse=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"String Response is : %@",strResponse);

NSMutableString *mutString=[NSMutableString string];

NSString *s=nil;

NSString *s1=nil;

//NSArray *arr1=[strResponse componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<br>"]];

NSArray *arr2=[strResponse componentsSeparatedByString:@"\n"];

NSLog(@"array %@",arr2);

for(s in arr2)

{

s = [s stringByReplacingOccurrencesOfString: @"<br>" withString: @"\n"];

s1=[s stringByReplacingOccurrencesOfString:@"<font color>" withString:@" "];

[mutString appendString:@""];

[mutString appendString:s1];

}

text1.text=[text1.text stringByAppendingString:mutString];

}

}

- (void)viewDidLoad

{

[super viewDidLoad];
    // Do any additional setup after loading the view.

strZone=[[NSString alloc]init];

dict=[[NSDictionary alloc]init];

text1=[[UITextView alloc]initWithFrame:CGRectMake(10, 280, 300, 120)];

text1.font=[UIFont fontWithName:@"Helvetica" size:12];

text1.font=[UIFont boldSystemFontOfSize:12];

text1.backgroundColor=[UIColor whiteColor];

text1.editable=NO;

[self.view addSubview:text1];

}

这是我的字符串响应。

 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>

但如果用户点击日历中的日期

,我想要显示如下内容

S.Panchami 01.38

Arudra 02.01

V.08.54-10.39

D.05.02-06.52

Shankara Jayanthi

5 个答案:

答案 0 :(得分:31)

iOS 7的简单解决方案:

NSString *html = @"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 *attr = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
                                                            options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                      NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
                                                 documentAttributes:nil
                                                              error:nil];
NSLog(@"html: %@", html);
NSLog(@"attr: %@", attr);
NSLog(@"string: %@", [attr string]);
NSString *finalString = [attr string];

答案 1 :(得分:8)

如果您使用ios 7,则可以应用此代码

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];

对于低于ios 7的ios使用此代码,

(NSString *) stringByStrippingHTML {
  NSRange r;
  NSString *s = [[self copy] autorelease];
  while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    s = [s stringByReplacingCharactersInRange:r withString:@""];
  return s;
}

答案 2 :(得分:2)

这应删除< >

之间的所有内容
NSString * yourString = @"<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>";

NSRange r;
NSMutableString * cleanString = [NSMutableString stringWithString:yourString];
while ((r = [cleanString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    cleanString = [cleanString stringByReplacingCharactersInRange:r withString:@""];

答案 3 :(得分:1)

正则表达式的一个选项是使用NSScanner

- (NSString *)removeHTML:(NSString *)str
{
    NSMutableString *pureStr = [NSMutableString stringWithCapacity:[str length]];
    NSScanner *scanner = [NSScanner scannerWithString:str];
    scanner.charactersToBeSkipped = NULL;
    NSString *tmp = nil;
    while (![scanner isAtEnd])
    {
        [scanner scanUpToString:@"<" intoString:&tmp];
        if (tmp != nil)
            [html appendString:tmp];
        [scanner scanUpToString:@">" intoString:NULL];
        if (![scanner isAtEnd])
            [scanner setScanLocation:[scanner scanLocation] + 1];
        tmp = nil;
    }
    return pureStr;
}

答案 4 :(得分:0)

我建议您使用UIWebView。如果您使用UITextView,首先必须从中删除HTML标记,然后必须插入换行符\n以进行格式化。使用简单的HTML格式(例如<html><head/><body><your string response><body/></html>)包装字符串响应会更容易,并将其显示在UIWebView中。