IOS:动态检测字符串长度
有没有办法动态检测字符串高度?该字符串有时足够长,可以扩展到2行,有时仅为一行字符串IOS:动态检测字符串长度,ios,nsstring,Ios,Nsstring,有没有办法动态检测字符串高度?该字符串有时足够长,可以扩展到2行,有时仅为一行字符串 CGRect frame = CGRectMake(8.0, kContentY, 200, 200); UIWebView *tempwv = [[UIWebView alloc] initWithFrame:frame]; NSString *myHTML = @"<html><body><h1>Hello, world!</h1></body>&
CGRect frame = CGRectMake(8.0, kContentY, 200, 200);
UIWebView *tempwv = [[UIWebView alloc] initWithFrame:frame];
NSString *myHTML = @"<html><body><h1>Hello, world!</h1></body></html>";
[tempwv loadHTMLString:myHTML baseURL:nil];
NSLog(@"==========> %f", tempwv.scrollView.size.height);
[contView addSubview:tempwv];
CGRect frame=CGRectMake(8.0,kContentY,200200);
UIWebView*tempwv=[[UIWebView alloc]initWithFrame:frame];
NSString*myHTML=@“你好,世界!”;
[tempwv loadHTMLString:myHTML baseURL:nil];
NSLog(@“==========>%f”,tempwv.scrollView.size.height);
[contView addSubview:tempwv];
我得到了200px的高度,这是webview框架 为此,您必须指定字体和字体大小以及webview的大小。在下面的方法中将其指定为,您将获得文本的高度
-(CGFloat)getHeightoftext:(NSString *)str
{
CGSize size ;
size = [str sizeWithFont:[UIFont fontWithName:@"font-name" size:12.0] constrainedToSize:CGSizeMake(maximumWidth, maximumHeight) lineBreakMode:NSLineBreakByWordWrapping];
return size.height ;
}
您可以使用所需的字体和字号测试最大高度和最大宽度。它适用于我的动态字符串值