objective-ccocoasizenstextfieldnsfont

Change NSTextField's height according to Font/Size


Ok, here's my situation :

The issue :

What I want :


I know it may sound complicated, but I also know it CAN be done.

Any ideas? Any reference to point me to?


Solution

  • You can take the size of the string and then change the height accordingly

    NSString *text = // your string
    CGSize constraint = CGSizeMake(210, 20000.0f);
    CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Helvetica-Light" size:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
    UITextField *cellTextLabel = [[UITextField alloc] initWithFrame:CGRectMake(70, 9, 230, size.height)];
    cellTextLabel.lineBreakMode = UILineBreakModeWordWrap;
    cellTextLabel.numberOfLines = 50;
    cellTextLabel.backgroundColor = [UIColor clearColor];
    cellTextLabel.text = text;
    cellTextLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
    [self.view addSubview:cellTextLabel];
    [cellTextLabel release];