示例代码如下:
textview = [[UITextView alloc] init];
[textview setFrame:CGRectMake(5, 20, 310, 540)];
textview.font = [UIFont systemFontOfSize:15];
textview.editable = NO;
textview.scrollEnabled = YES;
textview.backgroundColor = [UIColor yellowColor];
NSString *text = @"根据设置的字符串字体大小,计算字符串的高度";
CGSize labelSize1;
labelSize1 = [text sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:CGSizeMake(320, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByCharWrapping];
CGRect rect = textview.frame;
rect.size.height = labelSize1.height;
textview.frame = rect ;
textview.text = text;
[self.view addSubview:textview];