ios - 在 Deprecated sizeWithFont :minFontSize:actualFontSize to get the change in font size? 中复制 actualFontSize 指针返回

标签 ios objective-c ios7 fonts sizewithfont

所以,

sizeWithFont:minFontSize:actualFontSize 中,我们传递了一个指向 actualFontSize 的指针,然后可以使用它来缩小标签内的字体。如:

[s sizeWithFont:self.font
                minFontSize:minimumScaleFactor
                actualFontSize:&actualFontSize
                    forWidth:width
                lineBreakMode:lineBreakMode];

现在,我们可以通过 self.frame.font.pointSize = actualSize 来修改字体大小。我的问题是:既然 sizeWithFont 已被弃用,我该如何取回该指针?

基本上,推荐的替换方法是 boundingRectWithSize :

[s boundingRectWithSize:CGSize(width,CGFLOAT_MAX)
                options:(NSStringDrawingUsesLineFragmentOrigin)
                attributes://An NSDictionary with font
                context:nil];

但正如你所看到的,它只返回大小。我想做一些类似的事情:

changeInFontSize = (newSize.height - oldSize.height) + (newSize.width - oldSize.width)
                   -------------------------------------------------------------------
                                                  2

但这并不能真正给我想要的答案。有人可以帮助我如何做到这一点吗?准确地说 - 我需要字体大小的差异来更改当前字体大小!

最佳答案

问题是 sizeWithFont 方法会根据需要减小字体大小。因此,如果该函数使用较小的字体大小,您如何知道它使用的是哪种字体大小?这是 actualFontSize 的!来自文档:

Although it computes where line breaks would occur, this method does not actually wrap the text to additional lines. If the entire string does not fit within the given width using the initial font size, this method reduces the font size until the string does fit or until it reaches the specified minimum font size.

推荐的新方法boundingRectWithSize 不会减小字体大小。因此,如果该函数不减少 fonsize,这意味着它将使用您在 attributes 中指定的大小,则无需返回实际使用的大小(因为它正是您指定的大小,因此,您已经知道了)。

编辑:
如果您想计算适合某些预定义边界框所需的字体大小,您需要测试几个磅值并计算出最适合的字体大小。检查this link对于一些示例(链接上的最后一个答案看起来很有趣以优化效率)

关于ios - 在 Deprecated sizeWithFont :minFontSize:actualFontSize to get the change in font size? 中复制 actualFontSize 指针返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22156095/

相关文章:

ios - 无法将 searchBar 设置为 firstResponder

iphone - iOS 7 中带有 Cap Insets 问题的可调整大小的图像

ios - IAP Auto Renewable Subscription 在到期日之前手动续订

ios - GRPCcall2 的用法示例

ios - UIActivityViewController 的故障

ios - PebbleFramework给了我很多警告和错误

objective-c - iOS, View 之间的转换

iphone - 使用 Flash iOS 开发连接到互联网

ios - 带有选项的 Swift IOS 7 通知警报

ios - 使用 ESTBeaconManager 来自 Beacon iOS 的后台通知