我想找到来自webservice的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.