Comment puis-je obtenir -[NSString sizeWithFont:forWidth:lineBreakMode:] de travailler?

J'ai mis à jour ma question "Dimensionnement d'un UILabel (dans le SDK de l'iPhone) pour s'adapter?" avec une description de mon problème avec la solution proposée, mais n'a pas obtenu une réponse. Peut-être que je vais avoir de meilleurs résultats en posant une nouvelle question...

J'ai mis un point d'arrêt après le code suivant:

NSString *theText = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
CGSize theSize = [theText sizeWithFont:[UIFont systemFontOfSize:17.0f] forWidth:260.0 lineBreakMode:UILineBreakModeWordWrap];

theSize.height est 21 et theSize.width est 231. Ce que je fais mal? Cette hauteur ne peut pas être en pixels, ni en nombre de lignes.

Noter que [UIFont systemFontOfSize:17.0f] est destiné à spécifier la police par défaut.

Mise à jour: j'ai modifié le code pour:

CGSize constraintSize;
constraintSize.width = 260.0f;
constraintSize.height = MAXFLOAT;
NSString *theText = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
CGSize theSize = [theText sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

theSize.height est 273. Qui semble mieux comme ça.

  • J'ai eu exactement le même résultat: 21. Quelqu'un sait où ce numéro vient de, ou est-il simplement en se basant sur la taille de la police. Idiot de la méthode.
  • Si vous utilisez un UITextView, ont un coup d'oeil à ma réponse ici: stackoverflow.com/questions/2330939/...