Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IOS:动态检测字符串长度_Ios_Nsstring - Fatal编程技术网

IOS:动态检测字符串长度

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>&

有没有办法动态检测字符串高度?该字符串有时足够长,可以扩展到2行,有时仅为一行字符串

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 ;
}
您可以使用所需的字体和字号测试最大高度和最大宽度。它适用于我的动态字符串值