label 详细用法

来源:互联网 发布:阿里蒲公英是什么软件 编辑:程序博客网 时间:2024/06/17 01:34

一label基本设置

self.view.backgroundColor = [UIColor redColor];

    //创建第一个标签控件

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 30)];

    //对位置设置

    //对控件的中心点进行设置

    label.center = self.view.center;

    label.frame = CGRectMake(20, 20, 30, 30);

    

加粗;

[UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];

加粗并且倾斜

[UILabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:20]];

    //显示文字

    label.text = @"我是美女";

    

    //设置字体大小

    label.font = [UIFont systemFontOfSize:30];

    

    //自适应大小的方法   标签的大小由字体的大小长度决定

    [label sizeToFit];

    

    //字体的颜色  alpha 透明度 0 - 1   0- 1

    label.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:195/255.0 alpha:1];

    

    //Red Green Blue 0 - 255   255  255  255

    //  0 - 1

    //字体对齐格式  右侧是枚举类型

    label.textAlignment = NSTextAlignmentCenter;

    

    //加背景颜色

    label.backgroundColor = [UIColor greenColor];

    

    //显示出来 将标签 放到视图上 进行显示

    

    [self.view addSubview:label];

    //addSubview 添加子视图

    

    //不是程序崩溃前提下 问题:

    //第一点  frame是否设置了

    //第二点  是不是加到了父视图中

    //第三点  背景色和 控件颜色 一样

 

二.文字自适应

//创建label

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 200, 999)];

    label.backgroundColor = [UIColor greenColor];

    label.text = @"To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。To be or not to be, that is a question。";

    label.font = [UIFont systemFontOfSize:18];

    label.textColor = [UIColor redColor];

   //设置 label的换行模式

    label.lineBreakMode = NSLineBreakByWordWrapping; //根据单词进行换行

    //设置label显示几行  可以有无限行

    label.numberOfLines = 0;

 

    [label sizeToFit];

    

    [self.view addSubview:label];

 

 

 

 

 

text  property     

 font  property     

 textColor  property     

 textAlignment  property     

 lineBreakMode  property       

 enabled  property     

 Sizing the Label’s Text   

 adjustsFontSizeToFitWidth  property     

 baselineAdjustment  property     

 minimumFontSize  property   无例   

 numberOfLines  property     

 Managing Highlight Values   

 highlightedTextColor  property     

 highlighted  property     

 Drawing a Shadow   

 shadowColor  property     

 shadowOffset  property     

 Drawing and Positioning Overrides   

 – textRectForBounds:limitedToNumberOfLines: 无例    

 – drawTextInRect:  无例   

 Setting and Getting Attributes   

 userInteractionEnabled  property    

 

UILabel垂直居上对齐[label sizeToFit];

//设置文字过长时的显示格式 

label.lineBreakMode = UILineBreakModeWordWrap;

 

typedefenum {

    UILineBreakModeWordWrap =0,           // Wrap at word boundaries

    UILineBreakModeCharacterWrap,          // Wrap at character boundaries

    UILineBreakModeClip,           //截去多余部分 Simply clip when it hits the end of the rect截去多余部分 

    UILineBreakModeHeadTruncation, //截去头部Truncate at head of line: "...wxyz". Will truncate multiline text on first line

    UILineBreakModeTailTruncation,//截去尾部 Truncate at tail of line: "abcd...". Will truncate multiline text on last line

    UILineBreakModeMiddleTruncation,//截去中间 Truncate middle of line:  "ab...yz". Will truncate multiline text in the middle

} UILineBreakMode;

 

//设置label的行数,这个可以根据上节的UITextView自适应高度 

label.numberOfLines = 2;

label.lineBreakMode = UILineBreakModeWordWrap;

label.textAlignment =  UITextAlignmentCenter;//设置文字对齐位置,居左,居中,居右 

label.text = @ "123" ;//设置显示文字 

//设置文字颜色,可以有多种颜色可以选择

label.textColor = [UIColor whiteColor];

label.backgroundColor = [UIColor blackColor];

//设置字体:粗体,正常的是 SystemFontOfSize,调用系统的字体配置 

label.font = [UIFont boldSystemFontOfSize:20];

label.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(36.0)];

 

//[UIFont fontWithName:@ "Arial" size:14.0]]; //非加粗

 

//设置文本是否高亮和高亮时的颜色

scoreLabel.highlighted = YES; 

scoreLabel.highlightedTextColor = [UIColor orangeColor]; 

//设置阴影的颜色和阴影的偏移位置 

scoreLabel.shadowColor = [UIColor redColor]; 

scoreLabel.shadowOffset = CGSizeMake(1.0,1.0); 

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

scoreLabel.userInteractionEnabled = YES;  

//设置label中的文字是否可变,默认值是YES  

scoreLabel.enabled = NO;

 

//设置字体大小是否适应label宽度 

label.adjustsFontSizeToFitWidth = YES; 

//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为

 

coreLabel.baselineAdjustment = UIBaselineAdjustmentNone

 

typedefenum {

    UIBaselineAdjustmentAlignBaselines =0,// default. used when shrinking text to position based on the original baseline

    UIBaselineAdjustmentAlignCenters,

    UIBaselineAdjustmentNone,

} UIBaselineAdjustment;

 

//最小文字号数

minimumFontSize

设置背景色为透明 

 

 

scoreLabel.backgroudColor=[UIColor clearColor];

自定义的颜色:

scoreLabel.backgroudColor=[UIColor clearColor];

 

UIColor *color = [UIColor colorWithRed:1.0f green:50.0f blue:0.0f alpha:1.0f];

scoreLabel.textColor = [UIColor color]

//UIColor 里的 RGB 值是CGFloat类型的在0~1范围内,对应0~255的颜色值范围。

 

 

 

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.

- (void)drawTextInRect:(CGRect)rect;

 

eg:

 

UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0, 0, 75, 40)];   //声明UIlbel并指定其位置和长宽 label.backgroundColor = [UIColorclearColor];   //设置label的背景色,这里设置为透明色。 label.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];   //设置label的字体和字体大小。
//lable的旋转 label.transform = CGAffineTransformMakeRotation(0.1);     //设置label的旋转角度 label.text = @“helloworld”;   //设置label所显示的文本 label.textColor = [UIColorwhiteColor];    //设置文本的颜色 label.shadowColor = [UIColorcolorWithWhite:0.1falpha:0.8f];    //设置文本的阴影色彩和透明度。 label.shadowOffset = CGSizeMake(2.0f, 2.0f);     //设置阴影的倾斜角度。 label.textAlignment = UITextAlignmentCenter;     //设置文本在label中显示的位置,这里为居中。//换行技巧:如下换行可实现多行显示,但要求label有足够的宽度。 label.lineBreakMode = UILineBreakModeWordWrap;     //指定换行模式 label.numberOfLines = 2;    // 指定label的行数

 

 

 

 

 

 

让label自适应里面的文字,自动调整宽度和高度的

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
这样就可以对s赋值让其自动调整其大小了。


0 0