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
.
UITextView
links
UITextView
provides data detectors that pick up on URLs and make them tappable.UILabel
links using "Fancy Labels" (http://furbo.org/2008/10/07/fancy-uilabels/)
UIButton
s 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.
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.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.
Я нашел решение своей проблемы на http://www.iphonedevsdk.com/forum/iphone-sdk-development/1388-getting-actual-content-size-uiwebview .html
Я не уверен, что это лучшее решение, но оно работает. Если у кого-то есть идеи получше, буду рад их услышать.
В итоге я запустил этот javascript в моем webView в webViewDidFinishLoad:
. У меня есть один элемент div, обертывающий мое содержимое, называемое body, поэтому мой код выглядит так:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *string = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
CGFloat height = [string floatValue] + 8;
CGRect frame = [_webView frame];
frame.size.height = height;
[_webView setFrame:frame];
if ([[self delegate] respondsToSelector:@selector(webViewController:webViewDidResize:)])
{
[[self delegate] webViewController:self webViewDidResize:frame.size];
}
}
Мне пришлось немного увеличить высоту, так как исходное значение не учитывало размеры полей и т. д.
После того, как я получил высоту, я перезваниваю своему делегату, чтобы веб-просмотр изменил размер, чтобы можно было сделать макет.