zoukankan      html  css  js  c++  java
  • iPhone控件之UILabel

    一、UILabel的多行显示问题

    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;

  • 相关阅读:
    Python文件File方法
    python的slice notation的特殊用法
    函数
    字典
    python 笔记7
    列表解析与生成器
    [CenOS7][Mac] MAC环境中dubbo连接zookeeper超时
    [JAVA][Thread] 实现Runnable接口和继承Thread类创建线程哪种方式更好?
    [Java] String字符常量类型作为参数传递的一些问题
    [JAVA]hashCode()和identityHashCode()的区别
  • 原文地址:https://www.cnblogs.com/foxmin/p/2444705.html
Copyright © 2011-2022 走看看