HOW TO-获得文本的高度

星期一, 六月 6th, 2011

方法1:

1
2
3
NSString *str = [NSString stringWithString:@"People \nforget that @BillGates had a sexy 1/4-inch thick slate back in 1993 from NEC. Whatever happens this week will NOT be about hardware!"];
   
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(310.0f, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];

方法2:

1
2
3
4
5
6
7
8
9
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize: 17.0f];
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.text = str;
    CGRect labelFrame = label.frame;
    labelFrame.size.width = 310;
    label.frame = labelFrame;
    [label sizeToFit];