2011-01-14 4 views
1

Je pense que ce serait une chose facile puisque les gens voudraient le faire souvent, mais j'ai cherché et essayé différentes approches, mais rien ne semble fonctionner.UITextView autoSize

Tout ce que je veux faire est de créer un UITextView qui a deux lignes de texte.

Si le texte est trop long et qu'il est divisé en 3 lignes, je souhaite que la police s'autosise automatiquement jusqu'à ce qu'elle soit en 2 lignes. Conceptuellement, je prévoyais de faire une fonction récursive qui continue à réduire le texte jusqu'à ce qu'il se trouve sur deux lignes (en fonction de la hauteur du champ de texte), mais je ne pouvais pas le retirer.

Toutes les suggestions sont grandement appréciées.

+0

doit-il être modifiable ou un UILabel fonctionnera-t-il? –

+0

Le texte n'est pas éditable, je cherche essentiellement un UILabel avec 2 lignes –

Répondre

2

Au cas où quelqu'un d'autre vient à travers cette question, j'ai trouvé la réponse à cette ici:

http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/

//Create a string with the text we want to display. 
self.ourText = @"This is your variable-length string. Assign it any way you want!"; 

/* This is where we define the ideal font that the Label wants to use. 
    Use the font you want to use and the largest font size you want to use. */ 
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:28]; 

int i; 
/* Time to calculate the needed font size. 
    This for loop starts at the largest font size, and decreases by two point sizes (i=i-2) 
    Until it either hits a size that will fit or hits the minimum size we want to allow (i > 10) */ 
for(i = 28; i > 10; i=i-2) 
{ 
    // Set the new font size. 
    font = [font fontWithSize:i]; 
    // You can log the size you're trying: NSLog(@"Trying size: %u", i); 

    /* This step is important: We make a constraint box 
     using only the fixed WIDTH of the UILabel. The height will 
     be checked later. */ 
    CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT); 

    // This step checks how tall the label would be with the desired font. 
    CGSize labelSize = [self.ourText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    /* Here is where you use the height requirement! 
     Set the value in the if statement to the height of your UILabel 
     If the label fits into your required height, it will break the loop 
     and use that font size. */ 
    if(labelSize.height <= 180.0f) 
     break; 
} 
// You can see what size the function is using by outputting: NSLog(@"Best size is: %u", i); 

// Set the UILabel's font to the newly adjusted font. 
msg.font = font; 

// Put the text into the UILabel outlet variable. 
msg.text = self.ourText; 
3

Utilisez un UILabel et définissez les propriétés suivantes:

 
adjustsFontSizeToFitWidth = YES; 
minimumFontSize = [UIFont systemFontofSize: 10]; // or whatever suits your app 
numberOfLines = 2; 
+0

Cela ne semble pas fonctionner. Je ne pense pas que vous pouvez auto-dimensionner s'il y en a plus d'un comme du texte. –

0

i pense que c'est un peu tard pour cette question, mais puisque cela vient comme le meilleur résultat dans google pour cette requête un plus approprié (copier & coller) solution suivante peut-être

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{ 

if (textView.contentSize.height > textView.frame.size.height) { 
    int fontIncrement = 1; 
    while (textView.contentSize.height > textView.frame.size.height) { 
     textView.font = [UIFont fontWithName:@"Copperplate" size:25.0 - fontIncrement]; 
     fontIncrement++; 

    } 

} 
else { 
    int fontIncrement = 1; 
    while (textView.font.pointSize < 25.0) { 
     textView.font = [UIFont fontWithName:@"Copperplate" size:8.0 + fontIncrement]; 
     fontIncrement++; 

    } 
} 
return YES; 

}

dans le code ci-dessus 25 est tha variable maxFontSize que vous souhaitez définir pour le textview.

p.s. (BOOL) textViewShouldEndEditing: est l'une des méthodes déléguées de uitextview appelées juste avant que l'édition ne soit terminée et le clavier est résigné, donc vous devriez également inclure le protol délégué dans votre contrôleur de vue.