iOS开发UILabel的常用属性和方法

//

//  ViewController.m

//  LabelAll

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    

    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    //labele背景色

    myLabel.backgroundColor = [UIColor brownColor];

    [self.view addSubview:myLabel];

    //label文字

    myLabel.text = @"不易yi";

    //label文字颜色

    myLabel.textColor = [UIColor whiteColor];

    //label文字大小

    myLabel.font = [UIFont systemFontOfSize:14];//系统默认是17

    //label文字对齐方式

    myLabel.textAlignment = 0;

    /*

     NSTextAlignmentLeft      = 0,      左

     NSTextAlignmentCenter    = 1,   中

     NSTextAlignmentRight     = 2,    右

     */

    //label标记(tag)

    myLabel.tag = 123;

    //设置文本省略

    myLabel.lineBreakMode =NSLineBreakByTruncatingHead;//其中lineBreakMode可选值为

    /*

    linBreakMode enum{

        NSLineBreakByWordWrapping = 0,//保留整个单词,以空格为边界

        NSLineBreakByCharWrapping,//保留整个字符

        NSLineBreakByClipping,//以边界为止

        NSLineBreakByTruncatingHead,//省略开头,以省略号代替

        NSLineBreakByTruncatingTail,//省略结尾,以省略号代替

        NSLineBreakByTruncatingMiddle//省略中间,以省略号代替

    }

    */

    myLabel.numberOfLines = 1;//行数设置为1,不设置时系统会默认行数为1

    //当需要设置的行数为不限数量的时候可以用numberOfLines=0实现

    //当label大小使用sizeToFit方法,调整大小时会考虑到该属性中存储的值

//    [myLabel sizeToFit];

    //实现文本多行显示

    myLabel.lineBreakMode = NSLineBreakByCharWrapping;

    myLabel.numberOfLines = 0;

    //adjustFontSizeToFitWidth方法可实现文本自动根据label大小自动调整字体尺寸,直到文本的大小达到了自己设置的label文本尺寸最大、最小值与字符串的最大最小值,要是用这个方法还有一个很大的限制就是只有在numberOfLines设置为1时才能用

    //设置label的边框和颜色

    myLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;//边框颜色,要为CGColor

    myLabel.layer.borderWidth = 1;//边框宽度

    //设置label圆角

    myLabel.layer.cornerRadius = 10;

    //设置阴影

    myLabel.shadowColor =[UIColor grayColor];

    //设置阴影大小

    myLabel.shadowOffset = CGSizeMake(2.0, 2.0);

    //设置高亮

    myLabel.highlighted =YES;

    //设置是否能与用户交互

    myLabel.userInteractionEnabled = YES;

    //设置label文字是否可变

    myLabel.enabled = NO;//系统默认是yes

 

#pragma mark 自适应宽

    UILabel *label3 = [[UILabel alloc] init];

    label3.text = @"jfdkgiuhuhkjkllklmmlkgfhsresr";

    CGSize size1 = CGSizeMake(20,20); //设置一个行高上限

    NSDictionary *attribute = @{NSFontAttributeName: label3.font};

    CGSize labelsize = [label3.text boundingRectWithSize:size1 options:NSStringDrawingUsesDeviceMetrics attributes:attribute context:nil].size;

    label3.frame = CGRectMake(0, 400, labelsize.width + 3, labelsize.height);

    label3.backgroundColor = [UIColor redColor];

    [self.view addSubview:label3];

 

    // 系统的这个方法计算不是特别精确,所以要加3-5(不加的话,字符串长了以后label.frame.size.width会略小于字符串长度,导致文字显示不全)

#pragma mark   根据文本内容自动调整label高度

    NSString *text =[[NSString alloc]init];

    text = @"输入文本内容输入文本内容输入文本内容输入文本内容";

    CGSize size = CGSizeMake(280, 180);

    UIFont *fonts = [UIFont systemFontOfSize:14.0];

    CGSize msgSie = [text sizeWithFont:fonts constrainedToSize:size lineBreakMode: NSLineBreakByCharWrapping];

    UILabel *textLabel  = [[UILabel alloc] init];

    [textLabel setFont:[UIFont boldSystemFontOfSize:14]];

    textLabel.frame = CGRectMake(20,300, 280,msgSie.height);

    textLabel.text = text;

    textLabel.backgroundColor = [UIColor whiteColor];

    textLabel.lineBreakMode = NSLineBreakByCharWrapping;//实现文字多行显示

    textLabel.numberOfLines = 0;

    [self.view addSubview:textLabel];

    

#pragma mark 设置label背景图

    CGSize sizeone= CGSizeMake(100, 200);

    UIImage *image =[UIImage imageNamed:@"1.jpg"];

    //调用修改背景图的大小与label大小一致的方法

    UIImage *laterImage =[self scaleImage:image ToSize:sizeone];

    UIColor * color = [UIColor colorWithPatternImage:laterImage];

    UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];

    [label1 setBackgroundColor:color];

    [self.view addSubview:label1];

    

    

    

    

    

}

-(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize{

    UIImage *i;

    // 创建一个bitmap的context,并把它设置成为当前正在使用的context

    UIGraphicsBeginImageContext(itemSize);

    CGRect imageRect=CGRectMake(0, 0, itemSize.width, itemSize.height);

    // 绘制改变大小的图片

    [img drawInRect:imageRect];

    // 从当前context中创建一个改变大小后的图片

    i=UIGraphicsGetImageFromCurrentImageContext();

    // 使当前的context出堆栈

    UIGraphicsEndImageContext();

    // 返回新的改变大小后的图片

    return i;

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

 

转载于:https://www.cnblogs.com/freeleader/p/7389596.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值