How to show html in textview with dynamic height? - ios

My problem is that I have done to show html in textview but problem appear in this that textview height not perfectly set and also try with web view same problem appear.
How to render text in textview with dynamic height ?
Please help me out.

Try using a UIWebView instead.
Implement the UIWebViewDelegate, where webViewDidFinishLoad should look like this:
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(CGSize.zero)
//layout any other elements in the view if needed
}
Do not forget to set the webview's delegate.

Related

What happens to WKWebView when scrolled out of bounds

I'm using a paged scroll view displaying different web views as pages. The scroll view has set clipsToBounds = false to allow the display of previous and following pages in the scroll view.
When scrolling through the pages, there is one exact moment, where the WKWebViews content becomes completely invisible. This happens exactly at the moment, where the web view leaves the bounds of the scroll view (see below). On some websites, the visible content out of the bounds is different to the actual content.
These problems only occur when using WKWebView instead of UIWebView (Everything works as intended with UIWebView).
Has someone experienced similar issues with the WKWebView and has a solution for them?
EDIT:
There is a now a sample project on Github, where you can experience the bug and play around with it:
WKWebView test project
To figure it out i think you need to use UICollectionView.
The cell should exist with WKWebView while it will be on screen.
1) You need to place it like Here
And next problem will be to make content pageble.
2) To do it right you need to add UIScrollView over your collection like Here
3) After, make your UIScrollView pageble and write in viewdidload:
collectionView.addGestureRecognizer(scrollView.panGestureRecognizer)
4) After you receive models for showing in UICOllectionView - update your contentSize of UIScrollView like
scrollView.contentSize = CGSize(width: (models.count*(cellWidth+cellIndent) + sectionIndent * 2), height: cellHeight)
5) Implement UIScrollViewDelegate
extension YourVC: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.scrollView {
collectionView.contentOffset = scrollView.contentOffset
} else if scrollView == self.collectionView {
self.scrollView.contentOffset = scrollView.contentOffset
}
}
}
Thats it. Happy coding =)
WKWebView is designed to be more performant than UIWebView. It will stop running some parts of a webpage or stop rendering it completely if it is not on top of the view hierarchy. The problem here is that once the webpage is out of the bounds of the scrollview, WKWebView does not consider itself to be on screen even though it is visible by virtue of setting clipsToBounds = false.
The easiest thing to do would be to use UIWebView. If for some reason you need WebKit, consider adjusting you paging code so your scrollview spans the entire view controller.

WKWebview goes blank after device rotation

I'm making a scrollview with multiple subviews, includes collectionView, webView, and normal UIview. As far as Ive read through, it is not recommended to put webview inside a scrollview. But it is still ok to add webview as subview inside the scrollview.
The requirement is that the webview will dynamically load the content onClick. So, at first, there was a bit of issue trying to dynamically resize the height of the webview as per the content size. The height gets added extra pixels after every rotation. But somehow managed to make it work using webview.sizeToFit() in the viewDidLayoutSubviews().
But now the problem is that, the content of the webview disappears after every 2 rotation of the device. The content should still be there because the webview's frame size didnt change after roation, just the texts inside disappear.
There is not other error as to why the webview is showing blank for after rotated.This is my viewdidlayoutsubview.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(.zero)
webView.sizeToFit()
}
Not sure how to solve.
I will just answer myself here, since the solution last time was still working well for this. Refresh the view layouts on in the override func viewWillTransition. As per apple's doc, "UIKit calls this method before changing the size of a presented view controller's view". So Ive refreshed all the subview's before changing the orientation.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
super.viewWillTransition(to: size, with: coordinator)
//code to refresh the views
}
Just add this line below wkwebview code
i named var = webView so,
webView.autoresizingmask = [.flexiblewidth,.flexibleheight]
hope it works.

UIWebView on scrollView resize the height

Ihave a UIWebView on a scroll view. I want to disable scrolling for my web view. It should scroll when the main scroll view scrolling.But my problem is when the webview content is long I cant see the bottom lines. I am resizing the webview like this.
func webViewDidFinishLoad(webView: UIWebView) {
let url = NSBundle.mainBundle().URLForResource("Script", withExtension: "js")!
let javascript = try! String(contentsOfURL: url)
self.webvw.stringByEvaluatingJavaScriptFromString(javascript)
var frame:CGRect=webView.frame
frame.size.height=1
let fittingSize:CGSize = webView.sizeThatFits(CGSizeZero)
frame.size=fittingSize
self.webvw.frame=frame
self.mainScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.webvw.frame.origin.y+self.webvw.frame.size.height)
self.hud.hide(true, afterDelay: 1.0)
}
But still same. Didn't help me that. How can I solvethis pronlem.
Please help me.
Thanks
WebViews have built in scrolling. If you want to scroll through WebView's content, you would not need to use UIScrollView.
However, if you choose to do so, (I would not recommend), then you must set
you UIWebView's instance's scrollEnabled to false.
And then you will have to track you UIScrollView's content offset, and match UIWebView's scroll's content offset to that value. Doesn't seem like a good idea though. You can just scroll webview itself (without having to use UIScrollView).
And to get UIScrollView's content offset, you must override didScroll method of scrollView delegate.

UICollectionView dynamically resize height to display all content without scrolling

I would like to dynamically resize UICollectionView height and display all cells without scrolling when page loads.
I tried following codes in viewDidLoad but didn't work
self.constraintTableViewHeight.constant = self.myProfileCollectionView.contentSize.height
or
self.constraintTableViewHeight.constant = self.myProfileCollectionView.collectionViewLayout.collectionViewContentSize().height
call
self.view.layoutIfNeeded()
after
self.constraintTableViewHeight.constant = self.myProfileCollectionView.contentSize.height
If it will not help try to move this code to view viewWillAppear method.

Prevent UIWebView to scroll when keyboard appears

I have a UIWebView presenting a page containing input fields ().
When the user touches the field to enter data, the UIWebView is scrolled up and left to make the field stay visible for the user. Well ok.
I don't mind it is scrolled up (because everything below my form is hidden by the keyboard) but I would like to prevent the view from being scrolled left.
Is there a way ( headers in the html page, or iOS code) to prevent this behavior ?
Thanks
not beautiful but effective:
If you don't want your webView to scroll at all, you can implement the UIWebView's UIScrollViewDelegate and set the contentOffset to 0 on scrollViewDidScroll
aWebView.scrollView.delegate = self
func scrollViewDidScroll(scrollView: UIScrollView) {
scrollView.contentOffset = CGPointMake(0, 0)
}
I don't know how to prevent the scrolling entirely, but if you don't mind a little jitter, you can add this handler to the input field:
onfocus="setTimeout('window.scroll(0,0)',100)"
First you will have to capture the display of a Keyboard using UIKeyboardWillShowNotification .
Then restrict the contentSize width of the UIScrollView (of the UIWebview) to screen width which will ensure that the screen doesnot scroll horizontally.

Resources