美文网首页
判断label有几行

判断label有几行

作者: 朵朵刺刺 | 来源:发表于2017-03-07 17:18 被阅读314次

先计算总文字的高度,然后再除以每行文字的高度。

总文字的高度:CGFloat  textH = [label.text  boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;

每行文字的高度:CGFloat lineHeight = label.font.lineHeight;

行数:NSInteger  lineCount = textH / lineHeight;

//动态改变cell高度

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Incomplete implementation, return the number of sections

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

#warning Incomplete implementation, return the number of rows

return 100;

}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

return cell;

}

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

{

if(self.index == indexPath){

return 120;

}

return 60;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

self.index = indexPath;

[tableView deselectRowAtIndexPath:indexPath animated:TRUE];

// 重点是这2句代码实现的功能

[tableView beginUpdates];

[tableView endUpdates];

}

相关文章

网友评论

      本文标题:判断label有几行

      本文链接:https://www.haomeiwen.com/subject/znnmgttx.html