如何计算UILabel线中可以有多少个字符?

5 objective-c uitableview ios

我想UITableview为细胞制作动态高度.在每个单元格中,我显示的是不同文本的消息.现在我希望我的表格单元格符合消息的文本,并且单元格上的标签也应该显示文本.我根据可以容纳在一行中的字符数使我的表格单元格变得灵活.但这是问题所在.不同的字母表占用不同的空间.所以,我无法计算出一行中有多少字母可以出现.对此有何建议?

Kum*_* KL 2

尝试这个 :

-(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 ;

    }
Run Code Online (Sandbox Code Playgroud)