【发布时间】:2012-02-04 00:50:39
【问题描述】:
我更新了我的问题“Sizing a UILabel (in the iPhone SDK) to fit?”,并用建议的解决方案描述了我的问题,但没有得到答案。或许我可以通过提出一个新问题来获得更好的结果...
我在下面的代码之后设置了一个断点:
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。看起来更像。
【问题讨论】:
-
我得到了完全相同的结果:21。任何人都知道这个数字来自哪里,或者它只是基于那个字体大小。愚蠢的方法。
-
如果您使用的是 UITextView,请在此处查看我的答案:stackoverflow.com/questions/2330939/…
标签: cocoa-touch