gpt4 book ai didi

cocoa-touch - 我如何获得 -[NSString sizeWithFont :forWidth:lineBreakMode:] to work?

转载 作者:行者123 更新时间:2023-12-03 07:07:38 25 4
gpt4 key购买 nike

我用建议的解决方案描述了我的问题,更新了我的问题“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。看起来更像是这样。

最佳答案

该高度以像素为单位。我预计它会被截断,即为您提供在包含 1 行的 UILabel 上设置此文本时看到的指标。

尝试使用 sizeWithFont:constrainedToSize: 并为其指定 MAXFLOAT 作为尺寸的高度部分。

关于cocoa-touch - 我如何获得 -[NSString sizeWithFont :forWidth:lineBreakMode:] to work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/419222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com