2013-09-26 4 views
4

Dans iOS 6, je me sers:Dynamiquement redimensionner l'étiquette dans iOS 7

CGSize labelSize = [self.text sizeWithFont:self.font constrainedToSize:size lineBreakMode:self.lineBreakMode]; 
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , labelSize.width, self.frame.size.height); 

Pour redimensionner dynamiquement un UILabel. Cela ne fonctionne pas dans iOS 7 donc j'ai essayé:

NSString *text = self.text; 
CGFloat width = size.width; 
UIFont *font = self.font; 
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text 
                   attributes:@{ NSFontAttributeName: font }]; 

CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} 
           options:NSStringDrawingUsesDeviceMetrics 
           context:nil]; 
CGSize size = rect.size; 

self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , size.width, self.frame.size.height); 

C'est dans une catégorie à UILabel, mais cela ne fonctionne pas aussi ... Toutes les idées que je devrais utiliser?

+0

Quelle partie ne fonctionne pas? La taille d'image calculée est-elle incorrecte ou le changement de taille d'étiquette ne se produit-il pas? – tarmes

Répondre

6

Essayez quelque chose comme ça (travailler sans auto-mise en page):

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
                  [UIFont fontWithName:@"FontName" size:15], NSFontAttributeName, 
                  nil]; 

CGRect frame = [label.text boundingRectWithSize:CGSizeMake(263, 2000.0) 
                options:NSStringDrawingUsesLineFragmentOrigin 
                attributes:attributesDictionary 
                context:nil]; 

CGSize size = frame.size; 
3

Sans plus de détails sur pourquoi il ne fonctionne pas, je pense serait que vous devez utiliser l'option NSStringDrawingUsesLineFragmentOrigin pour qu'il pour devenir une solution de remplacement pour l'ancien sizeWithFont:, comme ceci:

NSString *text = ...; 
CGFloat width = ...; 
UIFont *font = ...; 
NSAttributedString *attributedText = 
    [[NSAttributedString alloc] 
     initWithString:text 
     attributes:@ 
     { 
      NSFontAttributeName: font 
     }]; 
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} 
              options:NSStringDrawingUsesLineFragmentOrigin 
              context:nil]; 
CGSize size = rect.size; 

S'il vous plaît noter la documentation mentionne:

Dans iOS 7 et versions ultérieures, cette méthode renvoie des tailles fractionnaires (dans le composant taille du CGRect renvoyé); pour utiliser une taille retournée à la taille vues, vous devez utiliser augmenter sa valeur à l'entier supérieur le plus proche en utilisant la fonction ceil.

Donc, pour tirer la hauteur ou la largeur calculée à utiliser pour les vues de calibrage, j'utiliser:

CGFloat height = ceilf(size.height); 
CGFloat width = ceilf(size.width); 
+0

J'ai essayé ça ... Ça ne marche pas ... – user426132

+0

@ user426132 aidez-moi à comprendre ce qui ne fonctionne pas. Quelle partie ne se comporte pas comme 'sizeWithFont:' habitué? –

1

Cela devrait fonctionner dans iOS6 et iOS 7, mais briserai vos contraintes d'étiquettes (dont vous avez besoin pour les mettre tous de retour si besoin programatically):

-(void)resizeHeightForLabel: (UILabel*)label { 
    label.numberOfLines = 0; 
    UIView *superview = label.superview; 
    [label removeFromSuperview]; 
    [label removeConstraints:label.constraints]; 
    CGRect labelFrame = label.frame; 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
     CGRect expectedFrame = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, 9999) 
                 options:NSStringDrawingUsesLineFragmentOrigin 
                attributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                   label.font, NSFontAttributeName, 
                   nil] 
                 context:nil]; 
     labelFrame.size = expectedFrame.size; 
     labelFrame.size.height = ceil(labelFrame.size.height); //iOS7 is not rounding up to the nearest whole number 
    } else { 
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" 
     labelFrame.size = [label.text sizeWithFont:label.font 
           constrainedToSize:CGSizeMake(label.frame.size.width, 9999) 
            lineBreakMode:label.lineBreakMode]; 
#pragma GCC diagnostic warning "-Wdeprecated-declarations" 
    } 
    label.frame = labelFrame; 
    [superview addSubview:label]; 
} 

Ajouter cette méthode à votre viewController et l'utiliser comme ceci:

[self resizeHeightForLabel:myLabel]; 
//set new constraints here if needed 
+0

J'ai besoin de redimensionner la largeur pas la hauteur ... Je vais l'essayer .. thx – user426132

+0

Essayez-le, je pense qu'il n'est pas si difficile de modifier le code pour redimensionner la largeur. –

+0

essayé ... n'a pas fonctionné – user426132

3
- (void)resizeLabelByContent:(UILabel *)label 
{ 

    CGSize maxSize = CGSizeMake(label.width, 999); 

    NSString *contentStr = label.text; 

    UIFont *contentFont = label.font; 

    CGRect contentFrame; 

    NSString *version = [[UIDevice currentDevice] systemVersion]; 

    if ([version floatValue] < 7.0) { 

     CGSize contentStringSize = [contentStr sizeWithFont:contentFont constrainedToSize:maxSize lineBreakMode:label.lineBreakMode]; 

     contentFrame = CGRectMake(label.left, label.top, label.width, contentStringSize.height); 

    } else { 

     NSDictionary *contentDic = [NSDictionary dictionaryWithObjectsAndKeys:contentFont, NSFontAttributeName, nil]; 

     CGSize contentStrSize = [contentStr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:contentDic context:nil].size; 

     contentFrame = CGRectMake(label.left, label.top, label.width, contentStrSize.height); 
    } 

    label.frame = contentFrame; 
}