Labels

‘Google Play again alcohol analytics Android Android Control Android Developer Android ebook Android N Android Studio Android Wear AndroidDevStory androidn AndroidWear anime Apps average bag beach benidorm beta testing betatesting GooglePlayforFamilies birthday BLT buildingforbillions camden camera cat ears charm christmas Christmas Countdown Clock chrome circle lenses Clock coat coffee collar Connectivity cosplay Countdown craft CultureAlley curl cute Develop Developer Preview developerstory developerstory developerconsole Distribute DIY drunk Education engagement exhibition families fashion Featured Featured’ felt fox freshers friends funny Games geek ghibli Google I/O Google Play Google Play Games Google Play games services GooglePlay gothic lolita gyaru hair halloween hand made hangover happy harry belafonte havea HelloEnglish herbie hancock hiragana Hitman Reborn holiday home hood hoodie hot chocolate house Hutch i'm back instagram iOS Control japanese japanese lolita javea kawaii kawaii it yourself kitty and the bulldog KIY knitting la mort learning life lightning lolita london look love love lunch machine madoka kaname maid maid cafe make-up make-up tutorial mariscos recio MaterialDesign me medieval mobile vision APIs monetization museum music Naruto Clock Naruto Flash Game NBU NDK needles neko nerd new New Year Countdown Clock oh One Piece Flash Clock Onee-chan outfit owl pan party peter peter pan photography pink player pool punk lolita purikura Reborn record red riding hood sea Security sewing shoes sorry spain store listing experiments story Storytoys student sunny sunshine sweet lolita table football tail tan to today totoro trying tutorial Udacity uni vam VandA victoria and albert vinyl wear well wifi wig winter wool xabia yarn zombie

How to use Web View to work with static HTML content

From developer.apple.com: "A web view object displays web-based content. It is an instance of the UIWebView class that enables you to integrate what is essentially a miniature web browser into your app’s user interface. The UIWebView class makes full use of the same web technologies used to implement Safari in iOS, including full support for HTML, CSS, and JavaScript content..." 

A web view is not only useful for loading websites on the internet within your application but also can load local html files as well.


In this tutorial, we want to introduce you how to use a web view to display a static html string and some tasks with this string like change font type, font size, font color...


Here is a result of this tutorial:



May be we use this to display content of a page of book like the way of Kindle :)

1. Create a new project



2. Design Main.storyboard like this


In that, we have a web view to display html content, one toolbar contain a button to show the UIView when touch on it and a UIView(we call it fontView) contain some controls are buttons, tableview to work with html string on web view

3. Display html string on web view
- Create a html string sample:


1
NSString * htmlString = @"<br><b>this is text in bold</b><br><i>this is text in italic</i> this is normal text<br>This is the third line of string";

- Then create a css string and append those string into NSMutableString, finally call loadHTMLString to load this string into web view


1
2
3
4
5
6
7
8
9
NSString *css = [NSString stringWithFormat:
@"<html><head><style>body { background-color: transparent; text-align: %@; font-size: %fpx; color: %@; font-family: %@; -webkit-text-size-adjust: none;} a { color: #172983; } </style></head><meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0; maximum-scale=1.0;\"><body>",
@"justify",
fontsize, @"black",[fontArray objectAtIndex:1]];
NSMutableString *desc = [NSMutableString stringWithFormat:@"%@%@<br><br>%@ ",
css,
htmlString,
@"</body></html>"];
[self.webviewContent loadHTMLString:desc baseURL:nil];


4. From fontView add some codes to process the html string on web view. We can change font type, font size and font color of that string

- For font size:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (IBAction)upFontSizeTap:(id)sender{
if (fontsize < MAX_FONTSIZE) {
fontsize += 1;
NSString *str = [NSString stringWithFormat:@"document.body.style.fontSize = '%f'",fontsize];
[self.webviewContent stringByEvaluatingJavaScriptFromString: str];
}
}
- (IBAction)downFontSizeTap:(id)sender{
if (fontsize > MIN_FONTSIZE) {
fontsize -= 1;
NSString *str = [NSString stringWithFormat:@"document.body.style.fontSize = '%f'",fontsize];
[self.webviewContent stringByEvaluatingJavaScriptFromString: str];
}
}

- For font type: we choose font type show on table view


1
2
3
4
5
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = [NSString stringWithFormat:@"document.body.style.fontFamily = '%@'",[fontArray objectAtIndex:indexPath.row]];
[self.webviewContent stringByEvaluatingJavaScriptFromString: str];
}

You can also get all system fonts by this code:


1
2
3
4
5
6
for(NSString* family in [UIFont familyNames]) {
NSLog(@"%@", family);
for(NSString* name in [UIFont fontNamesForFamilyName: family]) {
NSLog(@" %@", name);
}
}

- For font color:


1
2
3
4
5
6
7
8
9
10
- (IBAction)chooseBG1Tap:(id)sender{
self.photoImageView.image = [UIImage imageNamed:@"whitebg.png"];
NSString *str = @"document.body.style.color = 'black'";
[self.webviewContent stringByEvaluatingJavaScriptFromString: str];
}
- (IBAction)chooseBG2Tap:(id)sender{
self.photoImageView.image = [UIImage imageNamed:@"blackbg.png"];
NSString *str = @"document.body.style.color = 'white'";
[self.webviewContent stringByEvaluatingJavaScriptFromString: str];
}

5. Run Demo Application



Touch on button on toolbar to show font view and touch on buttons of font view to change style of string on web view




You can download all source codes of this tutorial from here





0 Response to "How to use Web View to work with static HTML content "

Post a Comment