zoukankan      html  css  js  c++  java
  • Label根据内容来显示单行还是多行

    我所遇到的情况是,产品让显示一个Message,最多两行显示,多余的用...代替,但是这个Message有可能是一行显示也有可能是两行显示,如果直接按照label两行填写,如果内容是一行的时候label就会居中显示,这是一个蛋疼的问题.怎么来解决呢?

    话不多说,上代码:

    NSString *messageString = _detailModel.message;
        CGSize maximumLabelSizeOne = CGSizeMake(200,115/2-20);//200为我需要的UILabel的长度,后面的参数是label的最大高度,这样可以控制多出来的用...显示
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13], NSParagraphStyleAttributeName:paragraphStyle.copy};
        CGSize size = [messageString boundingRectWithSize:maximumLabelSizeOne options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        
        UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(12.5, 10, 200, size.height)];
        messageLabel.text = messageString;
        messageLabel.textAlignment  = 0;
        messageLabel.numberOfLines = 2;
        messageLabel.font = [UIFont systemFontOfSize:13];
        messageLabel.textColor = [TncTool colorWithHexString:@"666666"];
        [messageView addSubview:messageLabel];

    这样就可以实现label的高度适配问题...

  • 相关阅读:
    tar (child): gzip: Cannot exec: No such file or directory
    通过应用数字格式来修复文本格式的数字
    Logback
    Transport
    nginx note
    docker note
    k8s note
    spring boot properties
    Windows 和 Linux 的文件名
    安装配置ubuntu的web项目(新)
  • 原文地址:https://www.cnblogs.com/nsjelly/p/6112912.html
Copyright © 2011-2022 走看看