zoukankan      html  css  js  c++  java
  • UILabel范例实现代码如下

     

    #import "TWO_ViewController.h"
    
    #define SCREEN_Width [[UIScreen mainScreen] bounds].size.width
    
    @interface TWO_ViewController ()
    
    @end
    
    @implementation TWO_ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.view setBackgroundColor:[UIColor blackColor]];
        
        UIView *headerView = [[UIView alloc] init];
        UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, SCREEN_Width - 40, 20)];
        image1.backgroundColor = [UIColor redColor];
        
        UILabel *ddlabel = [[UILabel alloc] init];
        [ddlabel setText:@"根据文字 高度进行适应根据文字高度 进行自适应根据文适应 根据文字高度进行自适应根据文自适应根据文字高度进行自适应根据文字高度进行自适应根据文字高度进行自适应根据文字高度进行自适应"];
        ddlabel.numberOfLines = 4;
        ddlabel.font = [UIFont systemFontOfSize:24];
        
        CGRect rect = [ddlabel.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: ddlabel.font} context:nil];
        
        ddlabel.frame = CGRectMake(20, CGRectGetMaxY(image1.frame),SCREEN_Width - 40,rect.size.height);
        
        [self setLineSpacing:20 label:ddlabel];
        
        //[ddlabel sizeToFit];
        
        
        UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(ddlabel.frame), SCREEN_Width - 40, 20)];
        image2.backgroundColor = [UIColor blueColor];
        
        headerView.frame = CGRectMake(0, 64, SCREEN_Width, CGRectGetMaxY(image2.frame)+20);
        NSLog(@"test height = %f", ddlabel.frame.size.height);
        headerView.backgroundColor = [UIColor whiteColor];
        [headerView addSubview:image1];
        [headerView addSubview:ddlabel];
        [headerView addSubview:image2];
        [self.view addSubview:headerView];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)setLineSpacing:(CGFloat)spacing label:(UILabel *)label
    {
        if(!label.text.length) return;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:spacing];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];
        [label setAttributedText:attributedString];
        label.lineBreakMode = NSLineBreakByTruncatingTail;
        [label sizeToFit];
        
        
        //[attributeString setAttributes:@{NSForegroundColorAttributeName:NF_Color_C19,NSFontAttributeName:[UIFont systemFontOfSize:Near_Final_Font_T9]} range:NSMakeRange(i, 1)];
    }
    
    
    @end
    
  • 相关阅读:
    Mac ssh 免密码登录 Mac 或者 Linux
    windows系统下将nginx作为系统服务启动
    eclipse的Git忽略某些不需要提交的文件
    spring boot 日志介绍 以及 logback配置示例
    nginx for Windows
    Eclipse 中Git的使用及如何解决冲突
    jquery是如何封装的
    SpringMVC接收json对象
    JSP中使用JSTL,并且解决XSS安全问题
    timestamp Invalid default value
  • 原文地址:https://www.cnblogs.com/firstrate/p/6725866.html
Copyright © 2011-2022 走看看