IOS - 获取高度NSString

时间:2013-07-26 18:25:25

标签: ios nsstring

我如何才能获得NSString的绝对高度,因为我正在努力做到以下几点:

NSString *text = "Hello word, i'm a programer";
CGSize maximumSize = CGSizeMake(300, 9999);

UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:12];

CGSize myStringSize = [text sizeWithFont:myFont
                       constrainedToSize:maximumSize
                           lineBreakMode:NSLineBreakByWordWrapping];

NSLog(@"myStringSize %f",myStringSize.height);

我在this site找到它,但我认为不行。

1 个答案:

答案 0 :(得分:5)

使用此:

CGSize maximumSize = CGSizeMake(300, 9999);
NSString *myString = @"This is a long string which wraps";
UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont 
                       constrainedToSize:maximumSize 
                           lineBreakMode:self.myLabel.lineBreakMode];

您的代码不起作用,因为您没有使用NSString!