-2

Is there a way to detect a string height dynamically? The string sometimes long enough to extend to 2 number of lines and sometimes it is only one line string.

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];

I get 200px height which is the webview frame;

shoujo_sm
  • 3,173
  • 4
  • 37
  • 60

1 Answers1

1

for that you have to specify font and font size and size of webview. specify it in below method as and you will get height of text.

-(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 ;
}

you can test for your maximum height and maximum width with desired font and font size. It Works for me in my dynamic strings values.

Max
  • 2,269
  • 4
  • 24
  • 49