2011-09-16 12 views
5

Sappiamo tutti che possiamo calcolare l'altezza di un'etichetta o qualsiasi controllo in base al testo. Così:Calcolo del testo in base all'altezza

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

Supponiamo ora di ottenere altezza = 270. Ora voglio solo quel testo che si trova in 200 altezza. Come My label height è 200 e voglio che il testo di altezza fino a 200 arrivi in ​​etichetta e il resto del testo dovrebbe apparire in un'altra etichetta. Quindi voglio chiedere se è possibile ottenere il testo in base all'altezza.

Grazie in anticipo!

+0

vuoi dire, si vorrebbe ottenere la dimensione del carattere del testo in base all'altezza del marchio? –

+0

nessuna dimensione del carattere del testo è fissa, ho bisogno solo di quel testo che può essere corretto in una certa altezza. – Gypsa

+0

Penso che il metodo migliore sarebbe controllare questo in un attimo ... –

risposta

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

Penso che questo possa fare il lavoro. Non è completamente testato, ma funziona.

1

Come da necessità è possibile modificare il testo dell'etichetta in base al proprio interesse. Ecco il mio codice di esempio.

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

}

Problemi correlati