2017-04-23 1 views
0

J'essaie d'obtenir une sous-chaîne d'un NSString qui pourrait entrer dans le cadre d'un UILabel. J'utilise ce code pour obtenir le CGRect -boundingRectWithSize renvoie toujours le même CGRect

CGSize sizeConstraint = CGSizeMake(355, 293.09375); 

NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
paraStyle.lineBreakMode = NSLineBreakByWordWrapping; 

NSDictionary *attributes = @{ NSFontAttributeName : bodyFont, NSParagraphStyleAttributeName : paraStyle}; 

NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:body attributes:attributes]; 
CGRect boundingRect = [attributedText boundingRectWithSize:sizeConstraint options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) context:nil]; 

Le problème que je une face am est que boundingRectWithSize retourner le même CGRrect après un certain nombre de caractères dans la chaîne. Voici le CGRect que je reçois quand la chaîne est de 677 caractères -

(CGRect) boundingRect = (origin = (x = 0, y = 0), size = (width = 352.515625, height = 281.25)) 

Et le CGRect je reçois lorsque la chaîne est 1354 caractères -

(CGRect) boundingRect = (origin = (x = 0, y = 0), size = (width = 352.515625, height = 281.25)) 

Aucune idée pourquoi

+0

Eh bien, je mérite la downvote – genaks

Répondre

0

viens juste de réaliser mon erreur -

CGSize sizeConstraint = CGSizeMake(355, 293.09375); 

devrait être

CGSize sizeConstraint = CGSizeMake(labelWidth, CGFLOAT_MAX); 

puis je devrais travailler sur la logique pour couper la chaîne. Stupide!