Jas*_*ien 7 iphone font-size uiwebview uikit ios
我有一些文本需要在我的应用程序中显示.该文本包含需要交互的链接.我曾尝试制造可点击链接了几个解决方案UITextViews
和UILabels
.
UITextView
链接
UITextView
提供数据检测器,可以获取URL并使其可以自适应.UILabel
使用"Fancy Labels"的链接(http://furbo.org/2008/10/07/fancy-uilabels/)
UIButton
s来覆盖链接,并且它们不包含长链接上的文本.最后我决定使用webview.我将文本传递给webview,并围绕它构建一个基本的HTML包装器,所有东西都是hunky-dory.这与Twitter for iPhone(又名Tweetie)使用的方法相同.
我现在唯一的问题是,如果文本太长,webview会剪切溢出并滚动以查看剪切的文本.其次,如果文本太短,那么webview下面就会有一大片浪费的空间.
与Tweetie不同,我在webview下面有更多内容,我想避免滚动溢出或浪费空间.无论如何,webview都是滚动视图的子视图,因此整个页面的内容可以增长,而不需要滚动子视图.
有没有什么方法可以动态调整webview的大小,以便根据它的内容来确定它的高度?
到目前为止,我尝试了很多东西,似乎没有什么是可靠的解决方案:
sizeToFit
什么都不做(或者更可能的是,只是将大小设置为当前帧的大小).sizeThatFits:
无论如何返回当前大小.NSString
的sizeWithFont:constrainedToSize:
原因是不可行的UIWebView
,并UIFont
有什么大小18是不同的表示.
scalesPageToFit
设置NO
和设置为13.5pt整个页面的字体大小(CSS)呈现文本,其大小为同UILabel
一个UIFont
尺寸18.如果有人能解释我,我会感激不尽.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找到了问题的解决方案
我不相信这是最好的解决方案,但它确实有效。如果有人有更好的想法,我会很高兴听到。
我最终在我的 webView 中运行了该 javascript 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];
}
}
Run Code Online (Sandbox Code Playgroud)
我必须稍微填充高度,因为原始值没有考虑边距大小等。
获得高度后,我会回调我的委托,告知 Web 视图确实调整了大小,以便可以完成布局。
归档时间: |
|
查看次数: |
13314 次 |
最近记录: |