如何从iPhone中的NSString中删除HTML标签? [英] how to remove HTML Tags from NSString in iphone?
问题描述
我正在为我的应用程序需求工作日历功能。我今天点击日期或明天日期或其他日期需要在 UITextview
中显示吉利详情。我一直在尝试在我的文本视图中格式化字符串,但我无法解决它。我是xcode的新手。我想删除 HTML
我的stringResonse中的标签,并显示在 UITextview
。
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:@"\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];
}
这是我的字符串响应。
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
S.Panchami 01.38
Arudra 02.01
Arudra 02.01
V.08.54-10.39
V.08.54-10.39
D.05.02-06.52
D.05.02-06.52
节日
Shankara Jayanthi
Shankara Jayanthi
推荐答案
iOS 7的简单解决方案:
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];
这篇关于如何从iPhone中的NSString中删除HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!