Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

ios - how to remove HTML Tags from NSString in iphone?

I am working Calendar functionality for my app requirement.IF i click today date or tomorrow date or some other date need to display auspicious details in UITextview.I have been trying to format a string in my text view but I cant work it out. Im very new to xcode.I want to remove HTML Tags in my stringResonse and display in UITextview.

I am writing like this in my code:

-(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:@"
"];

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

for(s in arr2)

{

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

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];

}

This is my string Response.

 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>

But i want to display like this if user clicks date in calendar

S.Panchami 01.38

Arudra 02.01

V.08.54-10.39

D.05.02-06.52

Festival

Shankara Jayanthi

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A simple solution with 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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...