自动调整后是否可以获得最终字体大小?(属性adjustsFontSizeToFitWidth设置为YES,文本字体大小正在缩小以适合标签)
我在UILabel中继承了drawTextInRect以在文本上放置渐变,但是渐变大小需要与字体的大小相同.我无法获得适当大小的调整字体......它甚至可能吗?
//draw gradient
CGContextSaveGState(myContext);
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1, 1, 1, 0.25, // BOTTOM color
1, 1, 1, 0.12 }; // UPPER color
//scale and translate so that text would not be rotated 180 deg wrong
CGContextTranslateCTM(myContext, 0, rect.size.height);
CGContextScaleCTM(myContext, 1.0, -1.0);
//create mask
CGImageRef alphaMask = CGBitmapContextCreateImage(myContext);
CGContextClipToMask(myContext, rect, alphaMask);
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
//gradient should be sized …Run Code Online (Sandbox Code Playgroud) 我有一个设置的标签,adjustsFontSizeToFitWidth = YES我需要获得实际显示的字体大小.
现在,iOS 7弃用了之前有效的所有方法,所有关于SO的问题建议使用这些弃用的方法.
一旦我被SO允许,我就会把这个问题作为赏金.请不要关闭.
我希望在UILabel或UITextField中缩小某些文本的字体大小.这在iOS 7.0之前是可能的:如何获得UILabel(UITextView)自动调整的字体大小?.但是,sizeWithFont已在iOS 7.0中弃用.我已经尝试使用它的替换,sizeWithAttributes,但没有成功.在iOS 7.0中有没有办法做到这一点?
编辑:链接的"重复"问题仅涉及计算文本矩形.我需要在标签缩放后计算实际字体大小,而不是字符串大小.
现在不推荐使用此方法:
size = [self sizeWithFont:font // 20
minFontSize:minFontSize // 14
actualFontSize:&actualFontSize // 16
forWidth:maxWidth
lineBreakMode:self.lineBreakMode];
Run Code Online (Sandbox Code Playgroud)
现在在iOS 7中缩小文本大小以适应时,如何计算UILabel的字体大小?
我有一个UILabel,他的文字大小有属性
title.adjustsFontSizeToFitWidth = YES;
Run Code Online (Sandbox Code Playgroud)
这阻止我使用标准方法来调整UILabel的大小.我在这里读到另一篇文章,我应该使用这个功能
sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode
Run Code Online (Sandbox Code Playgroud)
从这个答案:当-adjustsFontSizeToFitWidth设置为YES时,如何计算UILabel的字体大小?
现在,我无法弄清楚如何使它工作..这是实际的代码
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:200];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)];
title.text = @"this is a long title that should be resized";
title.font = font;
title.numberOfLines = 1;
title.adjustsFontSizeToFitWidth = YES;
CGFloat pointSize = 0.0;
CGSize size = [title.text sizeWithFont:font
minFontSize:title.minimumFontSize
actualFontSize:&pointSize
forWidth:width
lineBreakMode:title.lineBreakMode];
title.frame = CGRectMake(title.frame.origin.x,
title.frame.origin.y,
size.width,
size.height);
Run Code Online (Sandbox Code Playgroud)
UILabel错误地调整了大小,好像字体大小仍然是200 ..任何线索?谢谢!
我有一个字体大小设置为 24.0 的 UILabel。我已经对其进行了配置,以便它可以将字体大小自动缩小到最小 12.0.0。当我从标签中获取字体时,它说它是 24.0,即使这不是它用于实际显示文本的大小。
我怎样才能得到它真正使用的字体大小?
注意:我不是在问如何绘制文本,文本已经在 UILabel 中可见。
我有一个UILabel与adjustsFontSizeToFitWidth设置为YES如预期它缩小屏幕上显示的字体当文本太大的规模要绘制font财产.
我已经查询了该font属性,但这仍然与最初设置在标签上时相同,并且UILabel似乎没有任何其他可用的属性.
有没有办法找出标签绘制的缩小文字的大小是多少?
编辑:不是建议的重复,因为sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:在iOS7中已弃用