根据其内容计算UIWebView高度 [英] Calculate UIWebView height depending on its content
问题描述
我有一个名为公告
的课程,它有标题
( NSString
),发布日期
( NSString
)和 Body
(带有HTML内容的NSString
)。
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
,它在第一个单元格上有我的标题
,在第二个单元格上有的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:
如何计算我的 UIWebView
的高度,具体取决于我的 Body
?因为基本上当你垂直滚动时你滚动整个 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;
}
在这里,您必须使用font-style修复宽度和字体大小。
Here, you have to fix the width and font-size with font-style.
可能对你有所帮助。
这篇关于根据其内容计算UIWebView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!