Powered By Blogger

Friday, July 13, 2012

how to compress string according to frame in iphone sdk?

Hi everyone,
Please check the below tip for Compression of string according to your view/controller frame size.
 
        UILabel *lable =[[UILabel alloc]initWithFrame:CGRectMake(X_POS, Y_POS, width, height)];
        lable.tag = tag;
        lable.backgroundColor = [UIColor clearColor];
        lable.lineBreakMode = UILineBreakModeWordWrap;
        lable.numberOfLines = 10.0;


            CGFloat fontHeight = 15.0;
            BOOL huge = YES;
            while (huge)
            {
                CGSize boundingSize = CGSizeMake(lable.frame.size.width, CGFLOAT_MAX);
                CGSize requiredSize = [lable.text sizeWithFont:lable.font
                                             constrainedToSize:boundingSize
                                                 lineBreakMode:UILineBreakModeWordWrap];
                NSLog(@"Width:%f",requiredSize.width) ;
                NSLog(@"Height:%f",requiredSize.height) ;
                NSLog(@"NumberofLines : %f",requiredSize.height/lable.font.lineHeight);
               
                if (lable.frame.size.height<requiredSize.height)
                {
                    fontHeight --;
                    lable.font = [UIFont fontWithName:@"Verdana" size:fontHeight];
                    lable.adjustsFontSizeToFitWidth = YES;
                }
                else
                {
                    huge = NO;
                }
            }

Thanks & Regards,
Nilesh Prajapati

No comments:

Post a Comment