zoukankan      html  css  js  c++  java
  • UILabel的多行显示

    来自:http://blog.csdn.net/shark0001/article/details/7007238

     

    1.N行完全自适应:
            UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 100, 21)];
            NSString *txt = @"dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
            testLabel.numberOfLines = 0; ///相当于不限制行数
            testLabel.text = txt;
    这样不行,还需要调用 [testLabel sizeToFit];


    2.限制在N行内自适应:
            UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 100, 21)];
            NSString *txt = @"dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
            testLabel.numberOfLines = 3;   ////限制在3行内自适应
            testLabel.text = txt;
         [testLabel sizeToFit];
    结果不起作用,全部在一行显示了。

    3.为了实现2的需求,需要这么做:
            CGSize maxSize = CGSizeMake(100, 21*3);
            UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 100, 21)];
            NSString *txt = @"dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
            CGSize labelSize = [txt sizeWithFont:testLabel.font constrainedToSize:maxSize lineBreakMode: UILineBreakModeTailTruncation];

            testLabel.frame = CGRectMake(testLabel.frame.origin.x, testLabel.frame.origin.y, labelSize.width, labelSize.height);
            testLabel.text = txt;

     

     

    THE END !

  • 相关阅读:
    邻接表怎么写
    hiho一下 第二十五周(SPFA)
    hdu 1426 Sudoku Killer(DFS)
    hdu5147 (sequence 2) 树状数组
    hdu1233 prim
    输入输出外挂
    RMQ-ST求区间最值
    最近公共祖先(简单版)
    【Java】【20】后台发送GET/POST方法
    【实战问题】【11】导入Maven项目后报错,Project configuration is not up-to-date with pom.xml. Run project configuration update
  • 原文地址:https://www.cnblogs.com/xingchen/p/2532666.html
Copyright © 2011-2022 走看看