zoukankan      html  css  js  c++  java
  • 动态计算label 高度

    一、动态计算label高度 (忽略请看二、三)

    - (CGFloat)updateCellContentFrame{

        CGRect myRect = [self boundingRectWithInitSize:_contentLabel.frame.size label:_contentLabel];

        _contentLabel.frame = CGRectMake(0, 30, kScreenWidth, myRect.size.height);

        _timeLabel.frame = CGRectMake(0, myRect.size.height+30, kScreenWidth, 30);

        return myRect.size.height + 30*2;

    }

    // 动态 计算label  的大小

    -(CGRect)boundingRectWithInitSize:(CGSize)size label:(UILabel *)label{

     label.lineBreakMode=NSLineBreakByWordWrapping;

      CGRect rect=[label.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil] context:nil];

     return rect;

    }

    二、设置label的行间距,字间距 (推荐使用,相比三)

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"首页";

    // 创建label
    CGRect labRec = CGRectMake(0, 100, kScreenWidth, 0);
    UILabel *label = [[UILabel alloc] initWithFrame:labRec];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor redColor];
    //初始化NSMutableParagraphStyle
    NSString *string = @"引子:有的时候,当你用UILable加载一段话的时候,由于那些字可能会很多,系统的布局之后的字间距会很挤,但是,UI设计会要求你,,把字于字的间距调大一些,这时候就要程序猿敲代码改变字体的间距。接下来就给出实现这种效果的代码。另";
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    //行间距
    paraStyle.lineSpacing = 0;
    //字间距
    UIFont *fount = label.font;
    NSDictionary *titSpaceDic = @{NSFontAttributeName:fount,
    NSParagraphStyleAttributeName:paraStyle,
    NSKernAttributeName:@0.0f};

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:titSpaceDic];

    label.attributedText = attString;
    [label sizeToFit];
    [self.view addSubview:label];

    NSLog(@" %@",[NSValue valueWithCGRect:label.frame]);
    NSLog(@" %@",NSStringFromCGRect(label.frame));
    CFShow((__bridge CFTypeRef)(NSStringFromCGRect(label.frame)));

    }

    三、 通过系统方法设置 (会有问题,文字不能完全显示)

    - (CGRect)boundingRectWithSize: options: attributes: context:

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"申办管理";

    CGRect labRec = CGRectMake(0, 0, kScreenWidth, 0);
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor redColor];
    label.text = @"引子:有的时候,当你用UILable加载一段话的时候,由于那些字可能会很多,系统的布局之后的字间距会很挤,但是,UI设计会要求你,,把字于字的间距调大一些,这时候就要程序猿敲代码改变字体的间距。接下来就给出实现这种效果的代码。另";
    [self.view addSubview:label];
    CGRect labFrame =[self boundingRectWithInitSize:labRec.size label:label];
    label.frame = labFrame;
    CFShow((__bridge CFTypeRef)NSStringFromCGRect(labFrame));

    }

    // 动态 计算label 的大小
    -(CGRect)boundingRectWithInitSize:(CGSize)size label:(UILabel *)label{

    label.lineBreakMode=NSLineBreakByWordWrapping;
    CGRect rect=[label.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil] context:nil];
    CFShow((__bridge CFTypeRef)NSStringFromCGRect(rect));
    // rintf 四舍五入
    return CGRectMake(0, 100, rect.size.width, rintf(rect.size.height));


    }

  • 相关阅读:
    hibernate和ibatis的区别
    [转] hibernate和ibatis的对比
    如何理解java的引用传递
    代理模式与装饰器模式的区别
    j2ee的异步消息机制
    什么是j2ee ??EJB与j2ee的关系?? 请看百度百科
    spring事务的传播性的理解
    Hibernate与IBatis的优缺点及可行性分析
    jQuery插件实现的方法和原理简单说明
    iReport使用教程
  • 原文地址:https://www.cnblogs.com/tom2015010203/p/5482458.html
Copyright © 2011-2022 走看看