• iOS label 的故事


    UIlabel   继承与UIView 实现了 NSCoding 协议    主要的作用是用于在手机界面上展示一些内容 (标签作用)

    m_label = [UILabel new];//创建一个label
        
        //设置label在手机坐标系中位置和大小
        m_label.frame = CGRectMake(10, 40, WIDTH - 20, 40);
        
        //设置label中展示的内容
        m_label.text = @"12345ytrewq";
        
        //设置label中文字的对齐方式
        m_label.textAlignment = NSTextAlignmentCenter;
        
        //设置文字的颜色
        m_label.textColor = [UIColor redColor];
        
        //设置label的背景颜色
        m_label.backgroundColor = [UIColor lightGrayColor];
        
        //设置文字 字体和字号
        m_label.font = [UIFont systemFontOfSize:14];
        
        
        //设置label圆角
        m_label.layer.borderColor = [[UIColor greenColor] CGColor];
        m_label.layer.cornerRadius = 8.0f;
        m_label.clipsToBounds = YES;
        m_label.layer.masksToBounds = YES;
        
        
        /*
         //设置高亮
         label.highlighted = YES;
         label.highlightedTextColor = [UIColor orangeColor];
         
         //设置阴影
         label.shadowColor = [UIColor redColor];
         label.shadowOffset = CGSizeMake(1.0,1.0);
         
         //设置是否能与用户进行交互
         label.userInteractionEnabled = YES;
         
         //设置label中的文字是否可变,默认值是YES
         label.enabled = NO;
         
         //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为
         label4.baselineAdjustment = UIBaselineAdjustmentNone;
         */
        
        [self.view addSubview:m_label];

    UILabel 的一些高级用法  在一个label中展示出不同颜色不同字号不同字体的文字

    /************** label的高级用法 *************/
        //label中出现多种颜色的和不同字体的文字
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
        
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
        
        
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:14.0] range:NSMakeRange(0, 5)];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0] range:NSMakeRange(6, 12)];
        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:14.0] range:NSMakeRange(19, 6)];
        
        UILabel *label1 = [UILabel new];
        label1.frame = CGRectMake(10, 100, WIDTH - 20, 40);
        label1.backgroundColor = [UIColor lightGrayColor];
        label1.attributedText = str;
        label1.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:label1];

    UILabel 的高度自动调整来适应其中的文字内容

     UILabel *lab = [self.view viewWithTag:1000];
        if (!lab)
        {
            lab = [UILabel new];
            lab.text = _textfield.text;
            lab.font = [UIFont systemFontOfSize:14];
            lab.textColor = [UIColor blackColor];
            lab.backgroundColor = [UIColor orangeColor];
            lab.numberOfLines = 0;
            lab.lineBreakMode = NSLineBreakByCharWrapping;
            [self.view addSubview:lab];
        }
        //通过文字的多少来  让lab的高度自动调整
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        CGRect rect = [_textfield.text boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:paragraphStyle} context:nil];
        
       lab.frame = CGRectMake(50, self.textfield.frame.origin.y + 60, rect.size.width , rect.size.height);
    
  • 相关阅读:
    (转)Javascript的DOM操作
    (转)关于 Java 对象序列化您不知道的 5 件事
    java计算某个日期是什么节气(24节气)
    HTML5 客户端存储数据的两种方式
    laytpl : 一款非常轻量的JavaScript模板引擎
    解决阿里云服务器3306端口无法访问的问题(windows server 2008r2)
    阿里云服务器windows server2008r2+tomcat8.0+nginx1.12
    使用log4j2打印Log,log4j不能打印日志信息,log4j2不能打印日志信息,log4j和logj2,idea控制台信息乱码(文末)
    Android 实现http通信(servlet做服务器端) HttpClient、HttpURLConnection实现登录验证
    windows查看端口占用、结束进程
  • 原文地址:https://www.cnblogs.com/Mgs1991/p/5131795.html
Copyright © 2020-2023  润新知