spa*_*der 6 size user-interface fonts ios
标题几乎解释了它.我有一个图像,我正在绘制文字.我希望根据图像的大小调整文本的大小,并希望找到一种方法来获得比图像本身略短的字体高度.
小智 18
好的,所以对于那些认为迭代无法避免的人:
NSString *string = @"The string to render";
CGRect rect = imageView.frame;
UIFont *font = [UIFont fontWithSize:12.0]; // find the height of a 12.0pt font
CGSize size = [string sizeWithFont:font];
float pointsPerPixel = 12.0 / size.height; // compute the ratio
// Alternatively:
// float pixelsPerPoint = size.height / 12.0;
float desiredFontSize = rect.size.height * pointsPerPixel;
// Alternatively:
// float desiredFontSize = rect.size.height / pixelsPerPoint;
Run Code Online (Sandbox Code Playgroud)
desiredFontSize将包含字体大小,其高度与指定矩形的高度完全相同.您可能希望将它乘以0.8,使字体比rect的实际尺寸小一点,使其看起来很好.