如何计算UILabel行中可以输入多少个字符? [英] How To Count how many characters can come in a UILabel line?

查看:94
本文介绍了如何计算UILabel行中可以输入多少个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个具有动态高度的 UITableview 单元格.在每个单元格中,我都显示一条变化的文本消息.现在,我希望我的表格单元格根据消息的文本,而且我在单元格上显示的标签应显示该文本.我根据行中可容纳的字符数来使表格单元格灵活.但这是问题所在.不同的字母占用不同的空间.因此,我无法计算一行中可以有多少个字母.有什么建议吗?

I want to make a UITableview with dynamic height for cell. In each cell i am displaying a message of varying text. Now i want that my table cell be according to the text of the message and also my label present on cell should display the text. I am making my table cell flexible on the basis of the number of characters that can accommodate in a line. But here is the problem. Different alphabets take different space. So, i am unable to calculate how many alphabets can come in a single line. Any Suggestions to this???

推荐答案

尝试一下:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{CGSize size;
        if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
            // code here for iOS 5.0,6.0 and so on
            CGSize fontSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17]];
            size = fontSize;
        }
        else {
            // code here for iOS 7.0
            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                  [UIFont fontWithName:@"Helvetica Neue" size:19], NSFontAttributeName,
                                                  nil];
            CGRect fontSizeFor7 = [text boundingRectWithSize:CGSizeMake(571, 500)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:attributesDictionary
                                                     context:nil];
            size = fontSizeFor7.size;

        }return size.height +30 ;

    }

这篇关于如何计算UILabel行中可以输入多少个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆