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

标签 html objective-c ios4 nsstring css

我想在标签 (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/

相关文章:

javascript - 动态图像加载

ios - 如何从 knownTimeZoneNames 获取每个时区的 GMT 值

iphone - iOS 框架无法创建对象

iphone - 页面控制问题

html - 单击菜单项时如何影响其他元素

javascript - 根据 url 中的 #anchor 执行/触发脚本?

javascript - 如何添加jQuery函数悬停(显示)然后点击显示另一个菜单

iphone - didUpdateToLocation 未调用

ios - 在字典中访问字典中的字符串

objective-c - didUpdateToLocation 停止调用