use*_*174 -2 iphone objective-c ios
我正在为我的应用程序需求工作日历功能.我今天点击日期或明天日期或其他日期需要显示吉祥的详细信息.UITextview我一直在尝试在文本视图中格式化字符串,但我无法解决它.我是xcode的新手HTML.我想删除我的stringResonse 中的标签并显示在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];
}
这是我的字符串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>
但是如果用户点击日历中的日期,我想要显示如下
S.Panchami 01.38
Arudra 02.01
V.08.54-10.39
D.05.02-06.52
节
Shankara Jayanthi
Lar*_*rme 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];
如果您使用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;
}
| 归档时间: | 
 | 
| 查看次数: | 11604 次 | 
| 最近记录: |