I wanna wrap some text around a UIButton or clickable image in some kind of scrollview.
My ide was to add an UIButton to an UIWebView, but thats not possible?
Is there a way to wrap text around an UIButton in an UIScrollView or UITextView?
Or can I add an image to the UIWebView and handle the clicking somehow?
In the current SDK there is no easy way to wrap text around an image. You could make multiple UILabels and manually cut the strings up into pieces, but that is difficult in itself.
If you make the image a normal anchor, you can handle the opening of the link in the UIWebView delegate callbacks. Return NO and do whatever you want the image to do. You can use a custom scheme to make it easy to distinguish.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] scheme] isEqualToString:#"myscheme"] ) {
// do something
return NO;
}
return YES; // normal link
}
In a UIWebView, you have nearly full access to css and javascript and can do almost anything you could do in Safari. You are not limited to using an anchor tag, that is just the easiest way.
Related
Is there a way to get a callback to objective-c when a certain event has been detected in a UIWebView? Can Javascript send a callback to Objective-C?
Update - don't use UIWebView anymore. Use WKWebView, or better yet (if it fits your needs and you're building for iOS 9), a Safari View Controller.
But if you must use UIWebView, in your UIWebView delegate, provide an implementation for webView:shouldStartLoadWithRequest:navigationType:
In your HTML or Javascript files, add functions that send URLs to a custom scheme (for readability purposes, the custom scheme isn't required). All the URLs sent will be passed to your Objective-C method implementation, and then you can do what you'd like.
Just to illustrate the solution by "bpapa" with actual code:
WARNING: untested code
Implement this method in the UIWebView's delegate...
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] scheme] isEqualToString:#"callback"] ) {
// Do something interesting...
return NO;
}
return YES;
}
...then put a link in the webwieb like this:
Click me
And it should activate your callback-code. Obviously, you could trigger it with a javascript instead of a plain link.
I've designed my main view using HTML, CSS and some images. It looks very nice. I'm using some images that are clickable in HTML code.
On click of some image, I want to load some view accordingly.
Is it possible? If yes, please suggest me how to do it.
If tutorial or some sample code is available please share it.
Implement the following UIWebView Delegate Method and call your Objective methods
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(#"LINK CLICKED......");
//return NO;
}
return YES;
}
You can look into PhoneGap for some sample code.
There are two parts. First you define a custom scheme for you application that is only used to communicate between the web page and the delegate. Could just be 'foo:' as it will be private to the page and host. You then handle this custom scheme in the delegate shouldStartLoadWithRequest method and return NO.
With a clickable link, you can just set the href to something that starts with your custom scheme. If you need to send messages at arbitrary times, you can use javascript to set the window.location to a url with your custom scheme.
I have an app in which I render local HTML files in a UIWebView. The files, however, are sometimes large, and getting to where you want takes a long time with the default scroll speed. Is there any way to boost up the vertical scroll speed of a UIWebView?
In iOS 5 we can access the scrollView property of the UIWebView.
If you are targeting iOS 5+, you can simply call:
webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
Find a subview of UIWebView which is a UIScrollView, then set decelerationRate to UIScrollViewDecelerationRateNormal. This makes the UIWebView as fast as an ordinary UIScrollView.
In iOS 4/5, we can simply use the last subview of UIWebView.
UIScrollView *scroll = [webView.subviews lastObject];
if ([scroll isKindOfClass:[UIScrollView class]]) {
scroll.decelerationRate = UIScrollViewDecelerationRateNormal;
}
The default decelerationRate of UIWebView's UIScrollView is 0.989324, while UIScrollViewDecelerationRateFast is 0.99, and UIScrollViewDecelerationRateNormal is 0.998.
This method doesn't use any private API.
Search for a subview of UIWebView that responds to -setScrollDecelerationFactor: (it's UIScroller - a private class that's the only subview of UIScrollView). You'll find that it takes the same deceleration factors defined for the public UIScrollView class:
- (void)webViewDidFinishLoad:(UIWebView *)aView {
id decelerator = [aView viewWithSelector:#selector(setScrollDecelerationFactor:)];
[decelerator setScrollDecelerationFactor:UIScrollViewDecelerationRateNormal];
}
Note that the method I'm using viewWithSelector: is a method I defined in a category of UIView. Presumably, if UIWebView changes in future, my search will return nil and this method will become a no-op.
Have considered adding # tags into your html on significant boundaries?
You could actually use native UI to implement bookmarks or a ToC for easier navigation, or simply embed links to the appropriate targets right in your html.
If you 'speed up scrolling' your app is at risk of rejection for being non-standard, since it may confuse users who are used to webviews scrolling with a standard 'feel'.
Old question but these setting or trick really helped me even in 2018.
Follow these simple coding tricks to improve Android WebView Performance:
WebView mWebView = new WebView(this);
WebSettings settings = mWebView.getSettings();
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(false);
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setScrollbarFadingEnabled(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
I am designing an app which will present large amounts of text that is interspersed with notes and references as clickable images. On a PC I'd use a control that shows HTML, but in the iPhone I am not able to intercept the touches of images and links too well using the UIWeb control.
Should I use a UIScroll and build the text as lables and UIImages perhaps?
Looking for the best way forward in my design phase.
I don't know what your requirements are obviously, but it is possible to capture the click on an link in a UIWebView and take some alternative action. In one of my Apps, I have a UIWebView with one particular link which I want to route differently, while I let all other links open as web pages in the UIWebView as normal. Here's the code snippet from the app which accomplishes this. It is within a UIViewController which loads the UIWebView:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [ request URL ];
if( [[url path] isEqualToString:#"/the_special_link.html"] ) {
// Take some alternative action and then stop the page from loading...
// (code to take some special action goes here)
return NO;
}
else {
return YES;
}
}
This is a delegate method call, so when I set up the UIWebView, which I do programmatically in the Controller loadView method, I set the WebView's delegate to that same Controller:
myWebView.delegate = self;
I am using a webview and on that web view i have a image. I want to know how i detect touch event of image of webview. I have o open a new view when image is touch.
Depending on the content of the webView, you might have success turning the image into a link, then in your UIWebViewDelegate, implement the webView:shouldStartLoadWithRequest:navigationType: method and check for the image link's URL using the (NSURLRequest *)request parameter.
If you don't have control of the webView's HTML content, but the image does have an ID, you could make it a link using UIWebView's stringByEvaluatingJavaScriptFromString: method to run some javascript and "linkify" the image.
UIWebView inherits from UIView so you could try overriding:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Not sure how much control of the contents of the UIWebView you will have though and if you will be able to figure out the location of the image.