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 !

  • 相关阅读:
    cygwin 下配置ssh
    使用MarsEdit写博客
    bash no job control in this shell
    安装devtoolset-2:因由安装gcc 4.8而引起
    AFNetworking Property with 'retain (or strong)' attribute must be of object type
    从xib 创建 collectionViewCell
    CocoaPods 安装
    个人理解的 Https 通信流程
    cellforrowatindexpath 不执行 的原因
    do{} while(0) 的意义和用法
  • 原文地址:https://www.cnblogs.com/xingchen/p/2532666.html
Copyright © 2011-2022 走看看