Powered By Blogger
Showing posts with label NSString. Show all posts
Showing posts with label NSString. Show all posts

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

Thursday, April 26, 2012

NSData to HexString and HexString to NSData

Hi everyone,
Here, I've put two different methods for the conversion of "HEXString" to "NSData" and "NSData" to "HEXString".  you may get help from these two. Please ask if you have any query.


- (NSData *)dataFromHexString:(NSString *)string
{  
    NSMutableData *stringData = [[[NSMutableData alloc] init] autorelease];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < [string length] / 2; i++) {
        byte_chars[0] = [string characterAtIndex:i*2];
        byte_chars[1] = [string characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [stringData appendBytes:&whole_byte length:1];
    }
    return stringData;
}

- (NSString*)hexStringFromData:(NSData *)data
{
    unichar* hexChars = (unichar*)malloc(sizeof(unichar) * (data.length*2));
    unsigned char* bytes = (unsigned char*)data.bytes;
    for (NSUInteger i = 0; i < data.length; i++) {
        unichar c = bytes[i] / 16;
        if (c < 10) c += '0';
        else c += 'a' - 10;
        hexChars[i*2] = c;
        c = bytes[i] % 16;
        if (c < 10) c += '0';
        else c += 'a' - 10;
        hexChars[i*2+1] = c;
    }
    NSString* retVal = [[NSString alloc] initWithCharactersNoCopy:hexChars
                                                           length:data.length*2
                                                     freeWhenDone:YES];
    return [retVal autorelease];
}


If any one has query then share it over here.

Thanks and Regards,
Nilesh Prajapati