• 字体大小调整(UILabel、CATextLayer)


    如何根据宽度来计算字体的大小

    失效了

    //使用时,font指定字体和最大字体大小,minFontSize指定最小字体,actualFontSize传递实际大小的引用。width指定总宽度,lineBreakMode指定换行模式。
    - (CGSize)sizeWithFont:(UIFont *)font
            minFontSize:(CGFloat)minFontSize
         actualFontSize:(CGFloat *)actualFontSize
               forWidth:(CGFloat)width
          lineBreakMode:(UILineBreakMode)lineBreakMode
    

    示例:

    UILabel *_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
    [_label setNumberOfLines:0];
    CGFloat fontSize;
    NSString  *testString =  @"All hail Sakuya!" ;
    UIFont *font = [UIFont systemFontOfSize:100];
    [testString sizeWithFont:font
                 minFontSize:7.0f
              actualFontSize:&fontSize
                    forWidth:(320)
               lineBreakMode:NSLineBreakByCharWrapping];
    [_label setFont:[UIFont systemFontOfSize:fontSize]];
    [_label setText:testString];
    [self.view addSubview:_label];          
    

    根据字体大小调整宽度高度(单行文字)

    对于单行的UILabel,使用sizeToFit完全可以实现,

    对于单行的CATextLayer,可以使用sizeWithFont计算大小

    //公用属性
    UIFont *font = [UIFont systemFontOfSize:23.0];
    NSString *text = @"mgen 一二三";
    
    //UILabel
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
    lbl.backgroundColor = [UIColor greenColor];
    lbl.font = font;
    lbl.text = text;
    [lbl sizeToFit];
    
    //CATextLayer
    CATextLayer *layer = [CATextLayer layer];
    layer.frame = CGRectMake(50, 150, 100, 50);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.string = text;
    layer.font = (__bridge CFTypeRef)font.fontName;
    layer.foregroundColor = [UIColor blackColor].CGColor;
    layer.fontSize = font.pointSize;
    
    CGSize textSize = [text sizeWithFont:font];
    CGRect frame = layer.frame;
    frame.size = textSize;
    layer.frame = frame;
    
    [self.view addSubview:lbl];
    [self.view.layer addSublayer:layer];
    

    根据字体大小调整高度(指定行数文字,宽度不变)

    多行且有行数限制的情况,UILabel有numberOfLines属性,限定行数,然后使用sizetoFit

    CATextLayer则没有相应的属性,所以然后在sizeWithFont:constrainedToSize:方法中把高度设置成一个较大的值,完成不限制行数的多行CATextLeyer。

    //公用属性
    UIFont *font = [UIFont systemFontOfSize:17.0];
    NSString *text = @"mgen 一二三 mgen 一二三 mgen 一二三 mgen 一二三 mgen 一二三 ";
    
    
    //UILabel
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 50)];
    lbl.backgroundColor = [UIColor greenColor];
    lbl.numberOfLines = 2;
    lbl.font = font;
    lbl.text = text;
    [lbl sizeToFit];
    
    //CATextLayer
    CATextLayer *layer = [CATextLayer layer];
    layer.frame = CGRectMake(50, 150, 150, 50);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    layer.string = text;
    layer.font = (__bridge CFTypeRef)font.fontName;
    layer.foregroundColor = [UIColor blackColor].CGColor;
    layer.fontSize = font.pointSize;
    
    layer.wrapped = YES;
    CGSize textSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(CGRectGetWidth(layer.frame), CGRectGetHeight(self.view.bounds))];
    CGRect frame = layer.frame;
    frame.size = textSize;
    layer.frame = frame;	
    

    有一个太不合适的方法就是直接用UILabel的高度,也就是这样:

    CATextLayer没有对戳断字符的处理方式,虽然CATextLayer有truncationMode属性,但是设置了没有任何作用。

    CGSize textSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(CGRectGetWidth(layer.frame), CGRectGetHeight(lbl.frame))];    
    

    根据字体大小,调整UILabel的高度,并折行显示。

        //公用属性
    UIFont *font = [UIFont systemFontOfSize:17.0];
    NSString *text = @"可以更改此内容进行测试,宽度不变,高度根据内容自动调节";
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 50)];
    label.font = font;  //UILabel的字体大小
    label.numberOfLines = 0;  //必须定义这个属性,否则UILabel不会换行
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    [label setBackgroundColor:[UIColor redColor]];
    
    //宽度不变,根据字的多少计算label的高度
    //    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(label.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(CGRectGetWidth(label.frame), CGRectGetHeight(self.view.bounds))];
    //根据计算结果重新设置UILabel的尺寸
    [label setFrame:CGRectMake(50, 50, size.width, size.height)];
    label.text = text;
    NSLog(@" %f %f ",150,size.height);
    [self.view addSubview:label];
    

    高度不变,根据字体多少,自动调整UILabel的宽度,并折行显示

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 20, 20)];
    label.font = [UIFont boldSystemFontOfSize:20.0f];  //UILabel的字体大小
    label.numberOfLines = 0;  //必须定义这个属性,否则UILabel不会换行
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    [label setBackgroundColor:[UIColor redColor]];
    
    //高度固定不折行,根据字的多少计算label的宽度
    NSString *str = @"高度不变获取宽度,获取字符串不折行单行显示时所需要的长度";
    CGSize size = [str sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, label.frame.size.height)];
    NSLog(@"size.width=%f, size.height=%f", size.width, size.height);
    //根据计算结果重新设置UILabel的尺寸
    [label setFrame:CGRectMake(0, 10, size.width, 20)];
    label.text = str;
  • 相关阅读:
    Codeforces Round #422 (Div. 2) D. My pretty girl Noora 数学
    Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心
    Codeforces Round #422 (Div. 2) B. Crossword solving 枚举
    XJTUOJ wmq的A×B Problem FFT/NTT
    BZOJ 3527: [Zjoi2014]力 FFT
    Educational Codeforces Round 9 E. Thief in a Shop NTT
    focal loss for dense object detection
    国内敏捷项目协作工具亲测推荐
    Leangoo背景更新-看板背景任你选!!!
    Leangoo新功能-卡片ID
  • 原文地址:https://www.cnblogs.com/sunyanyan/p/5359719.html
Copyright © 2020-2023  润新知