UILabel 的简单实用

来源:互联网 发布:手机上有数控编程软件 编辑:程序博客网 时间:2024/05/17 15:37

 UILabel *label1 = [[UILabel alloc]init];    label1.frame = CGRectMake(20, 20, 200, 200);    label1.backgroundColor = [UIColor yellowColor];    //文本    label1.text = @"hello,hello,hello,hello,hello,hello,hello,hello,hello,";    //文字颜色    label1.textColor = [UIColor blackColor];    //文字布局模式    label1.textAlignment = NSTextAlignmentLeft;    label1.alpha = 0.5f;    //设置字体    label1.font = [UIFont systemFontOfSize:30];    label1.font = [UIFont boldSystemFontOfSize:30];    label1.font = [UIFont italicSystemFontOfSize:30];    label1.font = [UIFont fontWithName:@"Didot" size:30];   //设置字体和字号    //设置阴影    label1.shadowColor = [UIColor grayColor];    label1.shadowOffset = CGSizeMake(1, 2);        //换行 labe要有足够空间    label1.lineBreakMode = NSLineBreakByWordWrapping; //换行模式    label1.numberOfLines = 10;        CGSize size = [label1.text sizeWithFont:label1.font constrainedToSize:CGSizeMake(200, 10000) lineBreakMode:NSLineBreakByWordWrapping];    label1.frame = CGRectMake(label1.frame.origin.x                              , label1.frame.origin.y                              , label1.frame.size.width, size.height);        [self.view addSubview:label1];

文本

    label1.text =@"hello,hello,hello,hello,hello,hello,hello,hello,hello,";


文字颜色

    label1.textColor = [UIColorblackColor];


文字布局模式

    label1.textAlignment =NSTextAlignmentLeft;

    label1.alpha =0.5f;


设置字体

    label1.font = [UIFontsystemFontOfSize:30];

    label1.font = [UIFontboldSystemFontOfSize:30];

    label1.font = [UIFontthinSystemFontOfSize:30];

    label1.font = [UIFontitalicSystemFontOfSize:30];


设置字体和字号

    label1.font = [UIFontfontWithName:@"Didot"size:30];  


设置阴影

    label1.shadowColor = [UIColorgrayColor];

    label1.shadowOffset =CGSizeMake(1,2);

    

换行 labe要有足够空间

    label1.lineBreakMode =NSLineBreakByWordWrapping;//换行模式

    label1.numberOfLines =10;


根据label的字数,和字号,以及文本最大宽度,计算出来文本矿的size,然后重新设置label的尺寸,来实现label正好包住所有的文字

CGSize size = [label1.textsizeWithFont:label1.fontconstrainedToSize:CGSizeMake(200,10000)lineBreakMode:NSLineBreakByWordWrapping];

    label1.frame =CGRectMake(label1.frame.origin.x

                              , label1.frame.origin.y

                              , label1.frame.size.width

                              , size.height);





0 0
原创粉丝点击