好吧,而不是尝试在文本中解释我的问题,观看我录制的这个小视频 http://www.youtube.com/watch?v=fwb55jnZI6w
这是详细视图控制器的代码(webview所在的页面)
detailViewController.h
#import#import "MWFeedItem.h" @interface DetailViewController : UIViewController { MWFeedItem *item; NSString *summaryString; IBOutlet UILabel *titleLabel; IBOutlet UIWebView *contentLabel; //IBOutlet UILabel *dateLabel; IBOutlet UIScrollView *textScroller; } @property (nonatomic, retain) MWFeedItem *item; @property (nonatomic, retain) NSString *summaryString; @property (nonatomic, retain) IBOutlet UILabel *titleLabel; @property (nonatomic, retain) IBOutlet UIWebView *contentLabel; @end
detailViewController.m
#import "DetailViewController.h" #import "NSString+XMLEntities.h" typedef enum { SectionHeader, SectionDetail } Sections; typedef enum { SectionHeaderTitle, SectionHeaderDate, SectionHeaderURL } HeaderRows; typedef enum { SectionDetailSummary } DetailRows; @implementation DetailViewController @synthesize item, summaryString, titleLabel, contentLabel; - (BOOL)webView:(UIWebView *)contentLabel shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; { NSURL *requestURL = [ [ request URL ] retain ]; // Check to see what protocol/scheme the requested URL is. return [ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ]; return NO; // Auto release [ requestURL release ]; // If request url is something other than http or https it will open // in UIWebView. You could also check for the other following // protocols: tel, mailto and sms } - (void)viewDidLoad { [super viewDidLoad]; /*if (item.date) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterMediumStyle]; self.dateString = [formatter stringFromDate:item.date]; [formatter release]; }*/ if (item.summary) { self.summaryString = [[[item.summary stringByStrippingTags] stringByRemovingNewLinesAndWhitespace] stringByDecodingXMLEntities]; } titleLabel.text = item.title ? item.title : @"[No Title]"; [titleLabel setBackgroundColor:[UIColor clearColor]]; //dateLabel.text = dateString ? dateString : @"[No Date]"; // Summary NSString *HTMLData = summaryString; [contentLabel loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://www.feed43.com/1515171705611023.xml"]]]; //Calculate the expected size based on the font and linebreak mode of your label //CGSize maximumLabelSize = CGSizeMake(280,9999); //CGSize expectedLabelSize = [summaryString sizeWithFont:contentLabel.font //constrainedToSize:maximumLabelSize //lineBreakMode:contentLabel.lineBreakMode]; //adjust the label the the new height. //CGRect newFrame = contentLabel.frame; //newFrame.size.height = expectedLabelSize.height; //contentLabel.frame = newFrame; [textScroller setCanCancelContentTouches:NO]; [textScroller setContentSize:CGSizeMake(320, 500)]; textScroller.indicatorStyle = UIScrollViewIndicatorStyleBlack; textScroller.scrollEnabled = YES; textScroller.clipsToBounds = YES; //return titleLabel; //return dateLabel; //return contentLabel; } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations //return (interfaceOrientation == UIInterfaceOrientationPortrait); return NO; } - (void)dealloc { [super dealloc]; } @end
注意:即使它说"contentLabel",它也是一个UIWebView.它是从旧版本遗留下来的
谢谢!
实现-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
然后你可以做这样的事情:
NSURL* url = [request URL]; if (UIWebViewNavigationTypeLinkClicked == navigationType) { [[UIApplication sharedApplication] openURL:url]; return NO; }