2011-09-16 33 views
5

Todos sabemos que podemos calcular la altura de una etiqueta o cualquier control de acuerdo con el texto. De esta manera:Obtener texto basado en la altura

NSString *[email protected]"fwfgwefgwefhwefhwoefhwoeifhoiwefhwoeifhwieofhweohfiweofowefhowefhoweifhweofhweofhweoihfweiofhiowefhweiofhwioefhweiofhiweofhweiofhweiofhweiofhweiofweoifiweofhweoifhiowefhoiwefhowewoefoiwehfoiwe";  
    labelsize=[text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)]; 
    NSLog(@"labelsize.height%f",labelsize.height); 

Ahora supongo que obtengo la altura = 270. Ahora solo quiero ese texto que se encuentra en 200 de altura. Al igual que Mi altura de etiqueta es 200 y quiero que hasta 200 texto de altura entre en la etiqueta y el resto del texto se muestre en otra etiqueta. Entonces quiero preguntar si es posible obtener el texto basado en la altura.

¡Gracias de antemano!

+0

Qué quiere decir, que le gustaría para obtener el tamaño de fuente del texto de acuerdo a la altura de la etiqueta? –

+0

no se ha reparado el tamaño de fuente de texto, solo necesito ese texto que se puede arreglar en una altura determinada. – Gypsa

+0

Creo que el mejor método sería comprobar esto en un momento ... –

Respuesta

3
CGFloat maxHeight = 500; 
NSString *text = @"fwfgwefgwefhwefhwoefhwoeifhoiwefhwoeifhwieofhweohfiweofowefhowefhoweifhweofhweofhweoihfweiofhiowefhweiofhwioefhweiofhiweofhweiofhweiofhweiofhweiofweoifiweofhweoifhiowefhoiwefhowewoefoiwehfoiwe"; 
NSMutableString *tmpText = [[NSMutableString alloc] initWithString:text]; 
NSRange range = NSMakeRange([tmpText length] - 1, 1); 
while ([text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)].height > maxHeight) { 
    [tmpText deleteCharactersInRange:range]; 
    range.location--; 
} 
NSLog(@"result: %@", tmpText); 
[tmpText release]; 

Creo que esto puede hacer el trabajo. No está completamente probado, pero funciona.

1

Según su necesidad, puede cambiar el texto de la etiqueta dependiendo de su interés. Aquí está mi código de muestra.

NSMutableString *tmpLabel2=[[NSMutableString alloc]init]; 
NSString *[email protected]"Hello friend what r u doin..? what is going on in your company.. Tell me something yar i want to meet with u whenever u free just call me i will be der ok rest is perfect. talk u later…";  
NSMutableString *tmpLabel1 = [[NSMutableString alloc] initWithString:text]; 
NSRange range = NSMakeRange([tmpLabel1 length] - 1, 1); 

CGSize labelsize=[text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0) lineBreakMode:UILineBreakModeWordWrap]; 
while ([tmpLabel1 sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)].height > 200) { 

    unichar Char=[tmpLabel1 characterAtIndex:[tmpLabel1 length]-1]; 
    NSString*strTemp=[NSString stringWithFormat:@"%C",Char]; 
    [tmpLabel2 insertString:strTemp atIndex:0]; 
    [tmpLabel1 deleteCharactersInRange:range]; 
    range.location--; 
} 

label.frame=CGRectMake(50, 50, labelsize.width, 200); 
label.text=tmpLabel1; 
label.font=[UIFont fontWithName:@"Arial" size:14]; 
label.numberOfLines=0; 
label.clipsToBounds=YES; 
label.adjustsFontSizeToFitWidth=YES; 
label.lineBreakMode=UILineBreakModeCharacterWrap; 
label.backgroundColor=[UIColor grayColor]; 

NSLog(@"first Label is: %@", tmpLabel1); 
NSLog(@"Second Label is: %@", tmpLabel2); 

}

Cuestiones relacionadas