如何根据字数多少获取字符串对应高度

如何根据字数多少获取字符串对应高度,第1张

这种方法一般用于tableview,制作类似于微博浏览,空间展示动态的功能。表的高度根据以及字符串字数确定。

通过文字展示的最大宽度以及文字字体大小确定文字展示的高度

[objc] view plain copy

- (CGRect)getHeightOfText:(NSString )text width:(CGFloat)width font:(UIFont )font{

/

width:设定的字符串展示的宽度

font :字符的字体大小

/

CGRect rect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil];

/

字符串对应高度

float aHeifht = rectsizeheight;

字符串对应宽度

float aWidth = rectsizewidth;

/

return rect;

}

根据需要展示的高度或者文字高度自适应表行高

[objc] view plain copy

- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{

if (文字内容){

NSString text = [NSString stringWithFormat:@"\t%@",需要展示的文字];

CGRect rect = [self getHeightOfText:text width:300 font:[UIFont systemFontOfSize:14]];

return rectsizeheight+10;

}else//展示

{

return [[ objectForKey:@"height"] integerValue]+10;//获取服务器中高度,为了美观都比原始高多10

}

}

设置cell内容

[objc] view plain copy

- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath{

NewsDetailCell cell = [tableView dequeueReusableCellWithIdentifier:@"data"];

NSDictionary content = [_dataArray objectAtIndex:indexPathrow];

if ([[content objectForKey:@"data_type"] intValue] == 1) {

//文本

NSString text = [NSString stringWithFormat:@"\t%@",[content objectForKey:@"content"]];

celltxtLabeltext = text;

CGRect rect = [CommonTool getHeightOfText:text width:300 font:[UIFont systemFontOfSize:14]];

celltxtLabelframe = CGRectMake(10, 5, rectsizewidth, rectsizeheight);

}else{

//

NSString urlString = [NSString stringWithFormat:@"%@%@",SERVER_ADDRESS,[[content objectForKey:@"image"] objectForKey:@"source"]];

[cellnewsImageView setImageWithURL:[NSURL URLWithString:urlString]];

cellnewsImageViewframe = CGRectMake(10, 5, 300, [[[content objectForKey:@"image"] objectForKey:@"height"] integerValue]);

}

return cell;

}

在headerview采用的是autolayout布局的方式下,怎么去动态设置headerview的高度?

可使用如下方法:

systemLayoutSizeFittingSize ,它是UIView的方法,使用它的前提是需要展示内容的控件必须约束完美,接受一个CGSize,目前使用系统提供的Fitting Size即可

1 webView加载H5链接,设置它为tableView的 headerView,下方评论信息用Cell加载展示。

2 在webView的回调方法webViewDidFinishLoad中获取网页内容高度,设置为webView的高度,重新将webView赋给tableView的headerView。

Tip: 将一个View赋值给UITableView的tableHeaderView时,不需要手动设置高度,HeaderView会自动使用View的高度。

像上面这样,类似的方法很多,无论是JS获取,还是contentSize获取,最后结果都难以获取到准确高度,并非方法不行,而是:

设置cell动态高度有好几种方法:

1使用SDAutoLayout: >

调整UITableViewCell的高度,需要进行以下的步骤:

1、设置UITableView的代理:

tableViewdelegate=self;

tableViewdataSource=self;

2、实现代理方法:

-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath: (NSIndexPath )indexPath{

// 将所有cell的高度设置为44

return 44;

}

3、通过上面的代理方法就可以设置每一个cell的高度,如果每个cell的高度不同,就需要分开设置:

-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath: (NSIndexPath )indexPath{

if (indexPathsection == 0) {

if (indexPathrow == 0) {

return 64;

} else if (indexPathrow == 1) {

return 30;

}

} else if (indexPathsection == 1) {

if (indexPathrow == 0) {

return 88;

} else (indexPathrow == 1) {

return 44;

} else (indexPathrow == 2) {

return 44;

}

}

//方法需要一个返回值

return 0;

}

以上就是关于如何根据字数多少获取字符串对应高度全部的内容,包括:如何根据字数多少获取字符串对应高度、UITableView tableHeaderView 高度计算、webdeviceagent无法获得webview内容ios等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9440748.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存