天天看點

iphone中得到字元串所占空間的大小

NSString *string1 = @"Hi to all!I've this question for you, and i hope you can help me";

CGRect textSize = CGRectMake(0.0, 0.0, 320.0, FLT_MAX);

UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0];

UILabel *label = [[UILabel alloc] init];

label.lineBreakMode = UILineBreakModeWordWrap;

// Zero means infinite in this case

label.numberOfLines = 0;

label.font = font;

label.text = string1;

label.frame = [label textRectForBounds:textSize limitedToNumberOfLines:0];

NSLog(@"Frame at width %f height %f", label.frame.size.width, label.frame.size.height);