我更新了我的问题“如何在 iPhone SDK 中调整 UILabel 的大小以适应文本?”,描述了我的问题和一个建议的解决方案,但没有得到答案。也许通过提出新问题会有更好的结果...
我在以下代码后设置了断点:
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
是21,theSize.width
是231。我做错了什么?那个高度不能是像素也不能是行数。
请注意,[UIFont systemFontOfSize:17.0f]
用于指定默认字体。
更新:我更改了代码为:
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
是 273。看起来更正常了。