Snail—UI学习之UILabel

看一下UILabel的属性学习一下

在开始写代码之前,要保证新建了一个工程 然后新建一个RootViewController 作为window的根视图

代码要都写在RooViewController里面

我的WJJRootViewController.m文件里写的是创建UILabel的各种方法

#import "WJJRootViewController.h"

@interface WJJRootViewController ()

@end

@implementation WJJRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    
    /*
     iPhone中 是在关于视图组件时的坐标系是以左上角为原点
     向右 横坐标为正
     向下 纵坐标为正
     */
    
    //实例化一个UILabel  初始化它的边框大小  需要一个Rect的参数(x,y,width,height)
    UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 290, 30)];
    
    //设置label的属性
    
    //设置label的文本
    label1.text = @"Snail倒萨啊啊啊啊飒飒的的飒飒撒";
    //设置label的背景颜色
    label1.backgroundColor = [UIColor redColor];
    //设置label的文本对齐方式
    /*
     NSTextAlignmentLeft 左对齐
     NSTextAlignmentRight右对齐
     NSTextAlignmentCenter 居中
     */
    label1.textAlignment = NSTextAlignmentRight;
    //设置文本的字体大小 设置字体为20
    label1.font = [UIFont systemFontOfSize:20];
    //设置字体加粗及字体的大小 下面关于字体的效果会覆盖上面这个方法
    label1.font = [UIFont boldSystemFontOfSize:20];
    //设置文本的颜色
    label1.textColor = [UIColor blackColor];
    //设置lable的透明度  0-1  0是透明  并且父视图的透明度会影响到子视图或者文本的透明度
    label1.alpha = 0.5;
    //设置lable隐藏 yes即是隐藏
    label1.hidden = NO;
    //设置lable的边框颜色
    label1.layer.backgroundColor = [[UIColor blackColor] CGColor] ;
    //设置label边框的宽度
    label1.layer.borderWidth = 2;
    //设置label边框的弧度 如果label是正方形 弧度=边长/2的时候 为圆形
    label1.layer.cornerRadius = 10;
    //把label添加到主视图中
    [self.view addSubview:label1];
    
    
    //设置文本的自适应label 宽度或者高度固定,相应高度或宽度可以根据文字大小、多小相应的变化
    //这里我们会让label的宽度固定 类似qq或者微信发的消息
   
    //要显示的文字
    NSString * string = @"wuwuuwuweuwuwwwwwweuashsjkdshjdfshjdsfhjdsafhdfsghadfsghjafhdfshgjfhdfshdsfhgjdfhshgfdshasdhjfds";
    /*
     主要是计算文本的高度
     第一个参数:我们的label固定为宽为300,MAXFLOAT设置高度为无限大
     第二个参数:只知道文本要显示多行的时候 就写这个参数
     第三个参数:文本的一些属性
     第四个参数:nil
     */
    CGRect rect = [string boundingRectWithSize:CGSizeMake(290, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil];
    
    //创建一个label 根据计算出来的矩形大小来创建的label
    UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, rect.size.width, rect.size.height)];
    //设置label的文本大小要跟计算文本高度时文字的大小
    label2.font = [UIFont systemFontOfSize:20];
    label2.backgroundColor = [UIColor greenColor];
    //设置label的行数 0代表无限行 如果不设置 文本不换多行显示 上面的工作也就白做了
    label2.numberOfLines = 0;
    label2.text = string;
    [self.view addSubview:label2];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

下面附上运行在模拟器上得效果图


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值