我更新了我的问题“Sizing a UILabel (in the iPhone SDK) to fit?”,并提供了一个建议解决方案的问题描述,但没有得到答案。也许我会问一个新问题有更好的结果...如何获取 - [NSString sizeWithFont:forWidth:lineBreakMode:]工作?
我把下面的代码后断点:
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.任何人都知道该数字来自哪里,还是仅仅基于该字体大小。愚蠢的方法。 – ZaBlanc 2011-10-07 20:24:12
如果您使用的是UITextView,请在这里查看我的答案:http://stackoverflow.com/questions/2330939/sizewithfont-doesnt-give-correct-height-for-uitextview-if-there-is-a -long-strin/15408198#15408198 – domsom 2013-03-14 11:40:17