我想在标签 (TTStyleLabel) 上显示 html 文本。我正在接收 html 形式的文本。如何计算 html 字符串的高度?
最佳答案
要获取 html 文本的高度,您需要将该 html 信息放入 uiwebview。在 uiwebview 中加载 html 后,您可以像这样在其委托(delegate)方法中获取其高度 -
- (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。您需要执行一些技巧。
关于html - 如何计算html字符串的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6247936/