如何计算html字符串的高度?

编程入门 行业动态 更新时间:2024-10-28 00:25:55
本文介绍了如何计算html字符串的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在标签(TTStyleLabel)上显示HTML文字。我收到html的形式的文本。如何计算html字符串的高度?

I want to display html text on a label(TTStyleLabel). I am receieving text in form of html. How do I calculate height of html string?

推荐答案

为了获得html文本的高度,你需要把html信息uiwebview。在uiwebview中加载html之后,你可以在它的委托方法中获得它的高度 -

To get height of html text you need to put that html info uiwebview. After loading the html in uiwebview you can get its height in its delegate methods like this -

- (void)viewDidLoad { [super viewDidLoad]; webview.delegate = self; [webview loadHTMLString:@"<div id='foo' style='background: red'>The quick brown fox jumped over the lazy dog.</div>" baseURL:nil]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *output = [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"]; NSLog(@"height: %@", output); }

但是如果你不使用webview在屏幕上显示文本你使用的是TTStyleLabel),你可以隐藏webview并加载其中的html。你需要执行一些小技巧。

But if you are not displaying the text on the screen using the webview (as you are using a TTStyleLabel) you can hide the webview and load the html in it. You need to perform some tricks.

更多推荐

如何计算html字符串的高度?

本文发布于:2023-10-29 20:39:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   高度   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!