ios – 在minimumFontSize之后获取UILabel字体

前端之家收集整理的这篇文章主要介绍了ios – 在minimumFontSize之后获取UILabel字体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UILabel,字体大小为17.如果我调用label.font.pointSize,我会得到17,这一切都很好. BBUUUUTTT我也有一个最小字体大小设置为8,现在如果我在标签中塞入一些文本导致点大小缩小然后调用label.font.pointsize即使我知道点大小较小,我仍然得到17

任何想法如何在系统调整字体大小后获得真正的磅值?

解决方法

我不知道用于在缩小内容获取UILabel的当前磅值的API.您可以尝试使用sizeWithFont API来近似“缩放因子”.

只是一个想法:

// Get the size of the text with no scaling (one line)
CGSize sizeOneLine = [label.text sizeWithFont:label.font];

// Get the size of the text enforcing the scaling based on label width
CGSize sizeOneLineConstrained = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size];

// Approximate scaling factor
CGFloat approxScaleFactor = sizeOneLineConstrained.width / sizeOneLine.width;

// Approximate new point size
CGFloat approxScaledPointSize = approxScaleFactor * label.font.pointSize;
原文链接:https://www.f2er.com/iOS/328323.html

猜你在找的iOS相关文章