Thursday, January 1, 2015

29 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





29 comments:

  1. Nice update!It is easy to make a dorky web page. It's also easy to make a very nice, clean, professional-looking web page even if you don't have much design experience there are several Web Development Company India who can do your project in your budget.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    iot-training-in-chennai


    ReplyDelete
  5. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    java training in annanagar | java training in chennai

    java training in marathahalli | java training in btm layout

    java training in rajaji nagar | java training in jayanagar

    java training in chennai

    ReplyDelete
  6. The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept.
    python online training
    python training in OMR
    python training in tambaram

    ReplyDelete
  7. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram
    DevOps online Training

    ReplyDelete
  8. This is good site and nice point of view.I learnt lots of useful information.
    Blueprism training in velachery

    Blueprism training in marathahalli

    ReplyDelete
  9. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    python training in velachery
    python training institute in chennai

    ReplyDelete
  10. After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.

    Data Science course in kalyan nagar | Data Science course in OMR
    Data Science course in chennai | Data science course in velachery
    Data science course in jaya nagar | Data science training in tambaram

    ReplyDelete
  11. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Devops Training courses

    Devops Training in Bangalore

    Devops Training in pune

    ReplyDelete
  12. This is a terrific article, and that I would really like additional info if you have got any. I’m fascinated with this subject and your post has been one among the simplest I actually have read.
    python training in rajajinagar | Python training in btm | Python training in usa

    ReplyDelete
  13. Really you have done great job,There are may person searching about that now they will find enough resources by your post

    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  14. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  15. Thanks for sharing such a informative post...
    AngularJS Training in Bangalore | AngularJS Course Fees | AngularJS 6 - i Digital Academy - AngularJS Training in Bangalore - Learn AngularJS 6 from Expert Real-time Trainers
    at i Digital Academy with Live Projects and Placement Assistance. Book a Free Demo Today.

    ReplyDelete
  16. What a great online source of information about this topic. You have done great work. keep continue to sharing such kinds of post. Keep it up.

    If you want to make custom website & application you can contact us on our Android App Development Company and Mobile App Development Company anytime.

    ReplyDelete
  17. Fut hair transplant price Thanks for the post. It was very interesting and meaningful.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. The post is written pretty well, and it has a lot of information that I can utilise. I'm pleased to discover your excellent style of producing this content. Now you make the concept simple enough for me to understand and use.
    web development internship

    ReplyDelete