Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically sizing a UIWebView based on it's content font size

I have some text that needs to be displayed in my app. The text contains links that need to be interactive. I have tried a few solutions for making tappable links in UITextViews and UILabels.

  1. UITextView links

    • UITextView provides data detectors that pick up on URLs and make them tappable.
    • Unusable to me because the link will only open externally in Safari, there is no way to take control and do what I want with the link.
  2. UILabel links using "Fancy Labels" (http://furbo.org/2008/10/07/fancy-uilabels/)

    • Almost the perfect solution, providing detection of links and a way for the user to see and tap them inline and giving me complete control over what to do with a link when it's pressed.
    • Currently unusable because the implementation uses UIButtons to overlay the links and they don't wrap with the text on long links.

Finally I settled on using a webview. I pass the text into the webview and construct a basic HTML wrapper around it and everything is hunky-dory. This is the same approach that Twitter for iPhone (aka Tweetie) uses.

The only problem I have now is that if the text is too long, the webview clips the overflow and scrolls to view the clipped text. Secondly if the text is too short, then there is a big area of wasted space below the webview.

Unlike Tweetie, I have more content beneath the webview, and I want to avoid the scrolling overflow or the wasted space. The webview is a subview of a scroll view anyway so the content of the entire page can grow without its subviews needing to scroll.

Is there any way I can dynamically resize the webview so that it's just the right height based on it's content?

I've tried many things so far, nothing seems to be a solid solution:

  • sizeToFit does nothing (or more likely, just sets the size to what the current frame is anyway).
  • sizeThatFits: returns its current size anyway.
  • NSString's sizeWithFont:constrainedToSize: isn't feasible because UIWebView and UIFont have different representations of what size 18 is.
    • Just to elaborate on this point. In my testing, I've found that a webView with a scaleFactor of 1.0 and scalesPageToFit set to NO and a font size for the entire page set to 13.5pt (in CSS) renders text that is the same size as a UILabel with a UIFont of size 18. If anyone can explain that me I'd be grateful.
  • Also, in the webView I'm using the CSS property word-wrap: break-word; in order to prevent the webView from scrolling horizontally with long words or URLs. Because of this, the wrapping behaviour between the webView and UILineBreakModeWordWrap differ completely. Even using UILineBreakModeCharacterWrap doesn't produce the same results.

Either there's no way to do this, or I'm being dumb and missing something simple. If anyone could provide some insight, I'd be very grateful. Thanks.

like image 825
Jasarien Avatar asked Aug 27 '10 09:08

Jasarien


1 Answers

Actually, you can use -sizeThatFits: with a little trick. See my response to a similar question.

like image 159
Ortwin Gentz Avatar answered Sep 28 '22 18:09

Ortwin Gentz