根据其内容计算 UIWebView 高度

Calculate UIWebView height depending on its content(根据其内容计算 UIWebView 高度)

本文介绍了根据其内容计算 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 高度

基础教程推荐