当myLabel.adjustsFontSizeToFitWidth = YES,UILabel将自动调整字体大小,以防文本对于标签太长.例如,如果我的标签仅仅是100像素宽,我的文字太长,以适应当前的字体大小,直到文本适合的标签,它就会减少下来的字体大小.
当字体缩小时,我需要从UILabel获取实际显示的字体大小.例如,假设我的字体大小实际上是20,但UILabel不得不将其缩小到10.当我向UILabel询问字体和字体大小时,我得到的是旧字体大小(20),但不是显示的字体大小(10).
自动调整后是否可以获得最终字体大小?(属性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) 我正在尝试使用iOS 6的属性字符串API来计算文本的大小,并在必要时缩小字体大小.但是,正如文档所说,我无法让它工作.
NSString *string = @"This is a long text that doesn't shrink as it should";
NSStringDrawingContext *context = [NSStringDrawingContext new];
context.minimumScaleFactor = 0.5;
UIFont *font = [UIFont fontWithName:@"SourceSansPro-Bold" size:32.f];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByClipping;
NSDictionary *attributes = @{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle };
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.title attributes:attributes];
CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(512.f, 512.f) options:NSStringDrawingUsesLineFragmentOrigin context:context];
NSLog(@"rect: %@, context: %@", NSStringFromCGRect(rect), context.debugDescription);
Run Code Online (Sandbox Code Playgroud)
但是文本没有缩小并被截断.actualScaleFactor永远1.日志结果如下:
rect:{{0, 0}, {431.64801, 80.447998}}, context:<NSStringDrawingContext: 0x14e85770> minimumScaleFactor:0.500000 …Run Code Online (Sandbox Code Playgroud) 我希望在UILabel或UITextField中缩小某些文本的字体大小.这在iOS 7.0之前是可能的:如何获得UILabel(UITextView)自动调整的字体大小?.但是,sizeWithFont已在iOS 7.0中弃用.我已经尝试使用它的替换,sizeWithAttributes,但没有成功.在iOS 7.0中有没有办法做到这一点?
该方法(sizeWithFont: forWidth: lineBreakMode:)在iOS 7.0中已弃用.
但是如何在iOS 7.0中执行相同的操作呢?
CGSize fontSize =[self.text sizeWithFont:self.font forWidth:self.frame.size.width lineBreakMode:NSLineBreakByTruncatingTail];
Run Code Online (Sandbox Code Playgroud)
可以(boundingRectWithSize:options:attributes:context:)这样吗?我不确定,但这是我在Apple文档中搜索的方法.
谢谢你的协助.
我有一个UILabel与adjustsFontSizeToFitWidth设置为YES如预期它缩小屏幕上显示的字体当文本太大的规模要绘制font财产.
我已经查询了该font属性,但这仍然与最初设置在标签上时相同,并且UILabel似乎没有任何其他可用的属性.
有没有办法找出标签绘制的缩小文字的大小是多少?
编辑:不是建议的重复,因为sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:在iOS7中已弃用