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:
- For font type: we choose font type show on table view
You can also get all system fonts by this code:
- For font color:
5. Run Demo Application
You can download all source codes of this tutorial from here. - 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
thank you .
ReplyDeleteThanks for sharing such a informative post
ReplyDeleteAndroid Development company in Lucknow
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.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis 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.
ReplyDeleterpa 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
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
ReplyDeleteData Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore
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.
ReplyDeletejava 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
Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
ReplyDeleteData Science training in marathahalli
Data Science training in btm
Data Science training in rajaji nagar
Data Science training in chennai
Data Science training in electronic city
Data Science training in USA
Data science training in pune
Data science training in kalyan nagar
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.
ReplyDeletepython online training
python training in OMR
python training in tambaram
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.
ReplyDeleteDevops training in velachery
Devops training in annanagar
Devops training in tambaram
DevOps online Training
This is good site and nice point of view.I learnt lots of useful information.
ReplyDeleteBlueprism training in velachery
Blueprism training in marathahalli
Hello. This post couldn’t be written any better! Reading this post reminds me of my previous roommate. He always kept chatting about this. I will forward this page to him. Fairly certain he will have a good read. Thank you for sharing.
ReplyDeleteAWS Training in Bangalore | Amazon Web Services Training in Bangalore
Amazon Web Services Training in Pune | Best AWS Training in Pune
AWS Online Training | Online AWS Certification Course - Gangboard
Selenium Training in Chennai | Best Selenium Training in Chennai
Selenium Training in Bangalore | Best Selenium Training in Bangalore
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.
ReplyDeletepython training in velachery
python training institute in chennai
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.
ReplyDeleteData 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
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.
ReplyDeleteDevops Training courses
Devops Training in Bangalore
Devops Training in pune
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.
ReplyDeletepython training in rajajinagar | Python training in btm | Python training in usa
Really you have done great job,There are may person searching about that now they will find enough resources by your post
ReplyDeletejava training in chennai | java training in bangalore
java online training | java training in pune
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.
ReplyDeleteAWS 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
When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.
ReplyDeleteAmazon Web Services Interview Questions And Answers | Best AWS interview questions 2018
Advanced Amazon Web Services Training in Chennai | Best AWS Training Institute in Chennai
Advanced AWS Training Institute in Pune | Best AWS Training Center in Pune
Best AWS Training Institute in Marathahalli | Advanced AWS Training in Marathahalli, Bangalore
Thanks for sharing such a informative post...
ReplyDeleteAngularJS 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.
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.
ReplyDeleteIf you want to make custom website & application you can contact us on our Android App Development Company and Mobile App Development Company anytime.
Fut hair transplant price Thanks for the post. It was very interesting and meaningful.
ReplyDeleteI am very thankfull to you for sharing this fantastic article
ReplyDeleteinternship request letter | Internship completion letter | internship companies | internship resume objective | internship application letter | Internship with training | internship email | internship experience | What internship means | Internship acknowledgement
This comment has been removed by the author.
ReplyDeleteI would be fantastic if you could point me in the direction of a good platform.
ReplyDeleteB.A. Time Table 2022 - BA 1st 2nd 3rd Year Exam Date Sheet
B.A. 1st Year Time Table 2022
B.A. 2nd Year Time Table 2022
B.A. 3rd Year Time Table 2022
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.
ReplyDeleteweb development internship
Fantastic article.
ReplyDeleteSemi Automatic Paper Cutting Machine
Programmable Paper Cutting Machine
Hydraulic Paper Cutting Machine
Amazing post
ReplyDeleteGreat Article bcom colleges Bangalore is home to some of the best B.Com colleges that offer high-quality education and excellent career prospects. These colleges provide a comprehensive curriculum, experienced faculty, and modern infrastructure, creating a conducive environment for academic growth. With a range of specializations like finance, accounting, taxation, and management, students are equipped with the skills required to excel in various sectors of business and commerce. Renowned institutions such as Christ University, St. Joseph’s College, and Mount Carmel College are known for their academic excellence, industry exposure, and strong placement records, making them top choices for aspiring B.Com students in Bangalore.
ReplyDelete