Created
July 25, 2012 01:24
-
-
Save deathau/3173830 to your computer and use it in GitHub Desktop.
Code relevant to my stack overflow question: http://stackoverflow.com/questions/11641252/getting-size-of-styled-uiwebview-content-not-quite-working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title> </title> | |
<!--link rel="stylesheet" href="./style.css"--> | |
<style> | |
body{max-width:810px;color:#222;font-family:Arial, sans-serif;font-size:14px;line-height:1.286;background:#f7f4df;margin:0;padding:0} | |
body.page{background-color:transparent;color:#fff;font-family:"DIN Black";font-size:22px;text-transform:uppercase;line-height:22px} | |
</style> | |
</head> | |
<body class="page"> | |
<!--content--> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[indicator startAnimating]; | |
//setup secondary view controller and move to near the bottom of the view | |
self.secondaryViewController = [[SecondaryViewController alloc] initWithNibName:@"SecondaryViewController" bundle:nil]; | |
self.secondaryViewController.view.center = CGPointMake(self.secondaryViewController.view.center.x, 555.0F); | |
self.secondaryViewController.pageSize = 3; | |
self.secondaryViewController.currentPage = 1; | |
//Load data from my JSON API | |
JSONData* api = [[JSONData alloc] init]; | |
[api loadDataWithDelegate:self]; | |
[self.view insertSubview:self.secondaryViewController.view belowSubview:toolbar]; | |
} | |
// delegate method called when the JSON API has finished loading its data | |
-(void) JSONDataLoaded:(WorpressJSONData *)api withResults:(NSDictionary *)results{ | |
TertiaryViewController* tertiary = nil; | |
for(int i = 0; i < [results count]; i++){ | |
NSDictionary* result = [results objectAtIndex:i]; | |
tertiary = [[TertiaryViewController alloc] initWithNibName:@"TertiaryViewController" bundle:nil]; | |
// Set some other fields, unimportant for this issue | |
... | |
//assign the content | |
tertiary.content = [result objectForKey:@"content"]; | |
[self.secondaryController.tertiaryViews addObject:tertiary]; | |
tertiary = nil; | |
} | |
[self.secondaryController setup]; | |
[indicator stopAnimating]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void) setup{ | |
for(TertiaryViewController* tertiary in self.tertiaryViews){ | |
[tertiary setupWithDelegate:self]; | |
} | |
} | |
//Delegate Method called from the tertiary view controller | |
- (void) tertiaryViewLoaded:(TertiaryViewController*)controller{ | |
BOOL allAreFinished = YES; // innocent until proven guilty | |
// There's probably a better way to do this | |
for(TertiaryViewController* tertiary in self.tertiaryViews){ | |
if(!tertiary.isFinishedLoading){ | |
allAreFinished = NO; | |
break; | |
} | |
} | |
if(allAreFinished) [self setupProperly]; | |
} | |
- (void) setupProperly{ | |
TertiaryViewController* prevTertiary = nil; | |
for(TertiaryViewController* tertiary in self.tertiaryViews){ | |
// if already added to any view, remove it | |
if([tertiary.view superview] != nil){ | |
[tertiary.view removeFromSuperview]; | |
} | |
// if it isn't finished loading, realistically we shouldn't even be here. | |
if(!tertiary.isFinishedLoading){ | |
break; | |
} | |
// set the top of this view to the bottom of the previous view | |
if(prevTertiary != nil){ | |
CGRect tFrame = tertiary.view.frame; | |
tFrame.origin.y = prevTertiary.view.frame.origin.y + prevTertiary.view.frame.size.height + 10.0F; | |
deal.view.frame = tFrame; | |
} | |
prevTertiary = tertiary; | |
[self.scrollView addSubview:tertiary.view]; | |
} | |
// adjust the content size of the scroll view to the bottom of the last tertiary view | |
if(prevTertiary != nil){ | |
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, | |
prevTertiary.view.frame.origin.y + prevTertiary.view.frame.size.height + 10.0F); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)setupWithDelegate:(id<TertiaryViewDelegate>)aDelegate{ | |
self.delegate = aDelegate; | |
if(self.webView.superview != nil){ | |
[self.webView removeFromSuperview]; | |
} | |
_isFinishedLoading = NO; | |
if(self.content != nil){ | |
NSString* htmlPath = [[NSBundle mainBundle] bundlePath]; | |
NSString* htmlString = [NSString stringWithContentsOfFile: | |
[htmlPath stringByAppendingString:@"/page.html"] | |
encoding:NSUTF8StringEncoding error:nil]; | |
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<!--content-->" | |
withString:self.content]; | |
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath]; | |
[self.webView setDelegate:self]; | |
[self.webView loadHTMLString:htmlString baseURL:baseURL]; | |
} | |
} | |
-(void) webViewDidFinishLoad:(UIWebView *)theWebView { | |
CGRect frame = self.webView.frame; | |
frame.size.height = 1; | |
self.webView.frame = frame; | |
CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero]; | |
frame.size = fittingSize; | |
self.webView.frame = frame; | |
[self.view addSubview:self.webView]; | |
CGRect vFrame = self.view.frame; | |
vFrame.size.height = aFrame.origin.y + aFrame.size.height + 10.0F; | |
self.view.frame = vFrame; | |
_isFinishedLoading = YES; | |
[self.delegate tertiaryViewLoaded:self]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment