Calculate UIWebView height depending on its content(根据其内容计算 UIWebView 高度)
问题描述
我有一个名为Announcement的类,它有一个Title(NSString),一个Publishing Date(NSString)和一个 Body(NSString 与 HTML 内容).
I have a class named Announcement, it has a Title (NSString), a Publishing Date (NSString) and a Body (NSString with HTML content).
我想以与电子邮件应用程序类似的方式显示它.当您阅读电子邮件时,您会看到前三个行"(发件人、收件人、标题[日期]),然后是电子邮件的内容.
I would like to display it in a similar way like in the Email app. When you read an email you have the first three "rows" (From, To, Title[date]) and then the content of the email.
我只想创建Title[date]"行,然后是正文.我想它是一个 UITableView,在第一个单元格上有我的 Title,在第二个单元格上有一个 UIWebView.
I would want to create only the "Title[date]" row and then the body. I imagine that it to be a UITableView which has on the first cell my Title and on the second cell an UIWebView.
但似乎单元格的高度与 UIWebView 的高度完全相同,所以我的问题是:
But it seems like the cell has exactly the height of the UIWebView, so my question is this:
如何根据我的 Body 计算我的 UIWebView 的高度?因为基本上,当你垂直滚动时,你会滚动整个 UITableView,而当你水平滚动时,你只会滚动 UIWebView 内容.
How can calculate the height of my UIWebView depending on my Body? Because basically when you scroll vertically you scroll the whole UITableView, and when you scroll horizontally you scroll only the UIWebView content.
谢谢
推荐答案
在 webview 委托方法中,试试下面的代码:
In webview delegate method, just try with below code:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[webviewTopicDesc sizeToFit];
[webviewTopicDesc setFrame:CGRectMake(webviewTopicDesc.frame.origin.x, webviewTopicDesc.frame.origin.y, 300.0, webviewTopicDesc.frame.size.height)];
}
在此之前,将 webview 高度设为 5.0.
Before that, make the webview height as 5.0.
希望对您有所帮助.
这里可以根据字符串获取动态高度.
Here, you can get dynamic height based on string.
-(CGSize)getDynemicHeight:(int)pintFixWidth :(NSString *)pstrText
{
CGSize maximumSize=CGSizeMake(pintFixWidth, g_Max_Width);
UIFont *myFont = [UIFont fontWithName:g_Default_Font_Name size:g_Default_Font_Size];// font used for label
myFont=[UIFont boldSystemFontOfSize:g_Default_Font_Size];
CGSize sizeS = [pstrText sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
sizeS.height=sizeS.height+39;
return sizeS;
}
这里,你必须用字体样式来固定宽度和字体大小.
Here, you have to fix the width and font-size with font-style.
可能对你有帮助.
这篇关于根据其内容计算 UIWebView 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:根据其内容计算 UIWebView 高度
基础教程推荐
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
