Comment renvoyez-vous un texte multiligne CGSize à partir de la nouvelle méthode iOS 7 sizeWithAtsortingbutes?
Je voudrais que cela produise les mêmes résultats que sizeWithFont: constrainedToSize.
NSSsortingng *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu urna quis lacus imperdiet scelerisque a nec neque. Mauris eget feugiat augue, vitae porttitor mi. Curabitur vitae sollicitudin augue. Donec id sapien eros. Proin consequat tellus in vehicula sagittis. Morbi sed felis a nibh hendrerit hendrerit. Lorem ipsum dolor sit." CGSize textSize = [text sizeWithAtsortingbutes:@{ NSFontAtsortingbuteName : [UIFont fontWithName:@"HelveticaNeue-Light" size:16.0] }];
Cette méthode produit uniquement la hauteur pour une seule ligne de texte.
Eh bien, vous pouvez essayer ceci:
NSDictionary *atsortingbutes = @{NSFontAtsortingbuteName: [UIFont fontWithName:@"HelveticaNeue" size:14]}; // NSSsortingng class method: boundingRectWithSize:options:atsortingbutes:context is // available only on ios7.0 sdk. CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSSsortingngDrawingUsesLineFragmentOrigin atsortingbutes:atsortingbutes context:nil];
C’est comme ça que je l’ai fait:
// Get a font to draw it in UIFont *font = [UIFont boldSystemFontOfSize: 28]; CGRect textRect; NSDictionary *atsortingbutes = @{NSFontAtsortingbuteName: font}; // How big is this ssortingng when drawn in this font? textRect.size = [text sizeWithAtsortingbutes:atsortingbutes]; // Draw the ssortingng [text drawInRect:textRect withAtsortingbutes:atsortingbutes];
Voici ma méthode pour traiter les deux situations, passe dans une catégorie NSSsortingng
.
- (CGSize) sizeWithFontOrAtsortingbutes:(UIFont *) font { if (IS_IOS7) { NSDictionary *fontWithAtsortingbutes = @{NSFontAtsortingbuteName:font}; return [self sizeWithAtsortingbutes:fontWithAtsortingbutes]; } else { return [self sizeWithFont:font]; } }
Pour Xamarin.iOS:
UIFont descriptionLabelFont = UIFont.SystemFontOfSize (11); NSSsortingng textToMeasure = (NSSsortingng)DescriptionLabel.Text; CGRect labelRect = textToMeasure.GetBoundingRect ( new CGSize(this.Frame.Width, nfloat.MaxValue), NSSsortingngDrawingOptions.UsesLineFragmentOrigin, new UISsortingngAtsortingbutes () { Font = descriptionLabelFont }, new NSSsortingngDrawingContext () );
Swift 2.3:
let atsortingbutes = [NSFontAtsortingbuteName:UIFont(name: "HelveticaNeue", size: 14)] let rect = NSSsortingng(ssortingng: textToMeasure).boundingRectWithSize( CGSizeMake(width, CGFLOAT_MAX), options: NSSsortingngDrawingOptions.UsesLineFragmentOrigin, atsortingbutes: atsortingbutes, context: nil)
Si vous avez le texte, la police, numberOfLines et la largeur de votre jeu d’étiquettes, cette méthode renvoie la taille de votre étiquette:
myLabel.numberOfLines = 0; CGSize size = [myLabel sizeThatFits:CGSizeMake(myLabel.frame.size.width, CGFLOAT_MAX)];`
En alternative, si vous UITextView
, vous pouvez toujours utiliser la méthode NSLayoutManager
:
CGSize textSize = [textView.layoutManager usedRectForTextContainer:textView.textContainer].size;
Vous pouvez également trouver la hauteur de ligne pour une police donnée par:
UIFont *font; CGFloat lineHeight = font.lineHeight;
[En tant que nouvel utilisateur, je ne peux pas poster un commentaire sur la réponse de @ testing, mais pour rendre sa réponse (pour xamarin.ios) plus utile]
Nous pouvons renvoyer un CGRect et n’utiliser que le paramètre de hauteur pour l’élément graphique que nous ciblons UIButton, etc.
public CGRect GetRectForSsortingng(Ssortingng strMeasure, int fontSize, nfloat guiItemWidth) { UIFont descriptionLabelFont = UIFont.SystemFontOfSize (fontSize); NSSsortingng textToMeasure = (NSSsortingng)strMeasure; CGRect labelRect = textToMeasure.GetBoundingRect ( new CGSize(guiItemWidth, nfloat.MaxValue), NSSsortingngDrawingOptions.UsesLineFragmentOrigin, new UISsortingngAtsortingbutes () { Font = descriptionLabelFont }, new NSSsortingngDrawingContext () ); return labelRect; }
header_Revision.Frame = new CGRect (5 , verticalAdjust , View.Frame.Width-10 , GetRectForSsortingng( header_Revision.Title(UIControlState.Normal) , 18 , View.Frame.Width-10 ).Height );
CGSize ssortingngsize = [lbl.text sizeWithAtsortingbutes: @{NSFontAtsortingbuteName:[UIFont fontWithName:FontProximaNovaRegular size:12.0]}]; CGSize adjustedSize = CGSizeMake(ceilf(ssortingngsize.width), ceilf(ssortingngsize.height));
utiliser la méthode ceilf
pour gérer correctement