Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa touch 如何使-[NSString sizeWithFont:forWidth:lineBreakMode:]正常工作?_Cocoa Touch - Fatal编程技术网

Cocoa touch 如何使-[NSString sizeWithFont:forWidth:lineBreakMode:]正常工作?

Cocoa touch 如何使-[NSString sizeWithFont:forWidth:lineBreakMode:]正常工作?,cocoa-touch,Cocoa Touch,我更新了我的问题“”,用建议的解决方案描述了我的问题,但没有得到答案。也许我问一个新问题会有更好的结果 我在以下代码之后设置了一个断点: 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 a

我更新了我的问题“”,用建议的解决方案描述了我的问题,但没有得到答案。也许我问一个新问题会有更好的结果

我在以下代码之后设置了一个断点:

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];
尺寸高度
为21,
尺寸宽度
为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];

尺寸高度为273。看起来更像是这样。

高度以像素为单位。我希望它是截断的,也就是说,如果你在一个UILabel上用一行设置这个文本,你会看到它的度量


尝试使用
sizeWithFont:constrainedToSize:
,只需为大小的高度分量指定
MAXFLOAT

任何对此感到困惑的人都知道,该方法的命名不恰当,与您的预期不一致(这违反了良好的API设计,但任何经验丰富的开发人员都知道:对不起,伙计们,苹果的API设计得不好)

我认为他们应该对它进行重命名(如果必须保留的话),并使它变得有用,这样你就不会使用通常的做法来传递一个
CGSize
,而你知道它的大小太大了

[someString sizeWithFont:yourFont 
       constrainedToSize:CGSizeMake(maxWidthYouSpecify, self.frame.size.height)  
           lineBreakMode:UILineBreakModeWordWrap];
或者,这也同样有效:

[someString sizeWithFont:yourFont 
       constrainedToSize:CGSizeMake(maxWidthYouSpecify, CGFLOAT_MAX)
           lineBreakMode:UILineBreakModeWordWrap];
可以说,这就是函数应该如何工作。(FWIW,
CGFLOAT\u MAX
在中定义)

旁白,但相当重要:所有核心图形都以点而不是像素处理

一个点不一定对应于屏幕上的一个像素


这是一个重要的区别,在处理不同的分辨率(iPhone3GS vs 4 vs iPad等)时,你需要了解这一点。有关“点与像素”的信息,请参见苹果和Command+F.

我得到了完全相同的结果:21。任何人都知道这个数字的来源,或者它只是基于字体大小。愚蠢的方法。如果您使用的是UITextView,请看我的答案:如果文本足够长,创建最大高度为MAXFLOAT的视图将导致应用程序崩溃。