Quantcast
Viewing latest article 1
Browse Latest Browse All 19

iPhone : UIWebView like a rich text label

Image may be NSFW.
Clik here to view.
actionsheet

In this post UIWebView like a rich text label (SDK 4.2):

-A- How to use UIWebView with content by string
-B- How to intercept click action on link
-C- How to navigate subviews of UIWebView
-D- How to disable scroll bounce of UIWebView
-E- How to make transparent UIWebView and HTML page

UIWebView could be used in order to display mixed rich text styles (thing that UILabel can’t do):
bold
, italic and underline styles more over you can use mixed font sizes and styles, use CSS syntax…

For example in order to display string with mixed styles like this :
Warining : you have to accept T&C before to start

you can’t use UILabel instead you have to use UIWebView.

-A- How to use UIWebView with content by string
-E- How to make transparent UIWebView and HTML page

Sometime it is useful to make rich the text of a label for example with an underlined or bold portion of text.
Some customer asks to make “Localisable” text such as :

 <span style=”color: #ff0000;”><strong>Warining</strong></span> : you have to accept <a href=”http://www.google.it”>T&C</a> before to start

Following code allow to realize that :

/* String with HTML code (note use of style=\"background-color:) :
in order to make Localized : NSLocalizedString(@"Please <B>localise</B> this string","") */
NSString *html=@"<html><head></head><body  text=\"#FFFFFF\" link=\"#000000\" style=\"background-color: transparent; font-family: Helvetica; font-size:14px\"><B> Warining </B> : <I> you have to accept <a href=\"http:www.google.it\">T&C</a> before to start </I></body></html>";

/* Make background transparent
don't forget : style=\"background-color: transparent;....
which is present in html string */
[self.webv_terms setOpaque:FALSE];
[self.webv_terms setBackgroundColor:[UIColor clearColor]];

/* set html string */
[self.webv_terms loadHTMLString:html baseURL:nil];

-C- How to navigate subviews of UIWebView
-D- How to disable scroll bounce of UIWebView

Above code shows something like a label with rich text. What isn’t very great is bounce scroll that UIWebView has: label doesn’t have that … so we need to remove.
As we can suppose we have to disable it on UIScrollView that is ( a hidden) part of UIWebView.
One approach is change (using objectAtIndex:0) the top-level subview which is the proper view (I mean is the UIScrollView) …
but it isn’t a very good idea and it is better doesn’t use fixed value for index. I prefer  to search for UIScrollView instead to use a direct addressing to subview with 0 index.

//Disable bounce scroll
for(UIView *tmpView in ((UIWebView *)webv_terms).subviews){
  if([tmpView isKindOfClass:[UIScrollView class] ]){
   ((UIScrollView*)tmpView).scrollEnabled = NO;
   ((UIScrollView*)tmpView).bounces = NO;
  break;
 }
}

 -B- How to intercept click action on link

You have to create a class (let me call it WebViewDelegate) which extends <UIWebViewDelegate> and assign it how to delegate for UIWebView (keep in mind that UIWebView is what we are using such as a rich text label).

[webv_terms setDelegate:webViewDelegate];
(webv_terms is instance of UIWebView)

Consider that we can use link (webv_terms  has HTML content) with our defined schema :<a href=”about://#“>about us</a>

In class WebViewDelegate (webViewDelegate) you have to overwrite shouldStartLoadWithRequest.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
     if ([@"about" compare:[[request URL] scheme]]!=NSOrderedSame) {
       //Do something if user clicked on URL : for example flip the mainview and show about
     }
   return [@"about" compare:[[request URL] scheme]]==NSOrderedSame;
}

This method is called twice :

  1. a first time when html is loaded with : [self.webv_terms loadHTMLString:html baseURL:nil];
  2. and a second time if user (for example)  clicks on the link.

It is for this reason that we check
if (@”about”compare:[[request URL] scheme]]!=NSOrderedSame)
:
I mean in order to discover if user has clicked on link or it is the start loading event. Probably you would differentiate among actions basing on link clicked by the user. In this case you can check “(NSURLRequest *)request” in order to determinate which link is pressed.

I’m sure there are several better ways to do that I’ll be happy to get feedback about that.
Warning :  if you keep pressed link on the UIWebView an action-sheet is opened.Image may be NSFW.
Clik here to view.
actionsheet iPhone : UIWebView like a rich text label snippet iphone by example

The post iPhone : UIWebView like a rich text label appeared first on Busylog.net.


Viewing latest article 1
Browse Latest Browse All 19

Trending Articles