gpt4 book ai didi

ios - 在 ios 7 中加载到 webview 之前如何计算 html 字符串的高度?

转载 作者:可可西里 更新时间:2023-11-01 03:31:13 24 4
gpt4 key购买 nike

我想找到来自网络服务的 html 字符串的高度。请向我建议最佳解决方案。

提前致谢

最佳答案

对此的一个解决方法是将 HTML 加载到 NSAttributedString 中并获取它的高度。

NSAttributedString *text = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil]; // make sure to check for error in a real app

// Then get the bounding rect based on a known width
CGRect textFrame = [text boundingRectWithSize:CGSizeMake(someKnownWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
context:nil];

CGFloat height = textFrame.size.height;

我还没有尝试过使用各种 HTML 所以 YMMV。

关于ios - 在 ios 7 中加载到 webview 之前如何计算 html 字符串的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com