UILabel和UIControl一样,是UIView的子类。而UIButton是UIControl的子类,所以某种意义上,UILabel是UIButton的叔类
按照基本功能设置的界面如图。
label1演示了创建label, 字体类型,字体大小,字体颜色,对齐方式,背景色等属性设置;
//label1演示不同的设置属性的方式 UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 20.0, 200.0, 50.0)]; //设置显示文字 label1.text = @"粗体,30号字"; //设置文字颜色 label1.textColor = [UIColor orangeColor]; //设置字体大小 label1.font=[UIFont systemFontOfSize:30]; //设置靠右对齐,对齐方式有以下几种: label1.textAlignment = NSTextAlignmentRight; //设置背景色 label1.backgroundColor = [UIColor grayColor];
label2演示了不同的字符截取模式。其中本例子中采用了截去中间的方法
//label2演示不同的截取方式 UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(100, 80.0, 200.0, 50.0)]; label2.text = txt; //enum { //NSLineBreakByWordWrapping = 0, //NSLineBreakByCharWrapping, //NSLineBreakByClipping, //NSLineBreakByTruncatingHead, //NSLineBreakByTruncatingTail, //NSLineBreakByTruncatingMiddle //}; //typedef NSUInteger NSLineBreakMode label2.lineBreakMode = NSLineBreakByTruncatingMiddle;//截去中间
创建成员变量txt
NSString *txt = @"白日依山尽,黄河入海流,欲穷千里目,更上一层楼";
创建label3并设置text属性
//label3演示自动换行 b_SwitchLabel3 = NO; label3 = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 220, 100)]; label3.text = txt;
用btn0按钮来设置换行
//设置换行 label3.numberOfLines = 2 ; btn0 = [UIButton buttonWithType:UIButtonTypeCustom]; btn0.frame = CGRectMake(150, 170, 90, 35); [btn0 setTitle:@"换行" forState:UIControlStateNormal]; btn0.backgroundColor = [UIColor grayColor]; [btn0 addTarget:self action:@selector(switchLabel3:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn0];
btn0对应的方法
//btn0设置label3的换行属性 -(void)switchLabel3:(id)sender { UIButton *btnTmp = (UIButton *) sender; if(btnTmp == btn0){ if(b_SwitchLabel3==YES){ b_SwitchLabel3 = NO; label3.numberOfLines = 1; label3.adjustsFontSizeToFitWidth = YES; [btn0 setTitle:@"单行" forState:UIControlStateNormal]; } else{ b_SwitchLabel3 = YES; label3.numberOfLines = 2; label3.adjustsFontSizeToFitWidth = NO; [btn0 setTitle:@"两行" forState:UIControlStateNormal]; } } }
label4用来演示创建阴影字体。创建阴影字体的关键在于设置shadowColor和shadowOffset属性
//label4演示阴影字体 UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200.0, 50.0)]; label4.text = @"阴影字体"; label4.shadowColor = [UIColor redColor]; label4.shadowOffset = CGSizeMake(1.0,1.0);
label5用来演示label的enable和disable属性,这个在系统配置中,能够用来显示哪些配置是可选择的,哪些是不可选择的。
创建label5并设置字体和初始属性
//label5演示enable和disable切换 label5 = [[UILabel alloc]initWithFrame:CGRectMake(100, 260, 200.0, 50.0)]; label5.text = @"演示enable/disable"; label5.font =[UIFont systemFontOfSize:22]; b_Switch = YES; //设置label4的初始值属性
设置btn1,用来切换label5的enable和disable状态
//设置btn1的属性,btn用来切换label5.enable=YES/NO; btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; btn1.frame = CGRectMake(150, 300, 90, 35); btn1.backgroundColor = [UIColor grayColor]; [btn1 setTitle:@"切换" forState:UIControlStateNormal]; //设置按键字体大小 btn1.titleLabel.font = [UIFont systemFontOfSize: 20]; //设置按键字体颜色 [btn1 setTitleColor:[UIColor blackColor]forState:UIControlStateHighlighted]; //设置按键目标 [btn1 addTarget:self action:@selector(switchLabel5:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn1];
btn1对应的方法,其中关键在于设置label5.enable
//btn1设置标签的enable属性 -(void)switchLabel5:(id)sender { UIButton *btnTmp = (UIButton *) sender; if(btnTmp == btn1){ if(b_Switch==YES){ b_Switch = NO; label5.enabled = NO; } else{ b_Switch = YES; label5.enabled = YES; } } }
演示根据label尺寸,调节字体大小。其中关键属性为adjustsFontSizeToFitWidth
//label6显示字体大小自适应 label6 = [[UILabel alloc]initWithFrame:CGRectMake(100, 360, 200.0, 50.0)]; label6.text = @"字体大小可以根据Frame尺寸自适应"; b_AutoSize=YES; label6.adjustsFontSizeToFitWidth = YES;
设置btn2,用来切换自适应开关
btn2.backgroundColor = [UIColor grayColor]; [btn2 setTitle:@"自适应" forState:UIControlStateNormal]; //设置按键字体大小 btn2.titleLabel.font = [UIFont systemFontOfSize: 20]; //设置按键字体颜色 [btn2 setTitleColor:[UIColor blackColor]forState:UIControlStateHighlighted]; //设置按键目标 [btn2 addTarget:self action:@selector(autoSize:) forControlEvents:UIControlEventTouchUpInside];
btn2对应方法,关键在于设置adjustsFontSizeToFitWidth
//btn2设置标签是否自适应 -(void)autoSize:(id)sender { UIButton *btnTmp = (UIButton *) sender; if(btnTmp == btn2){ if(b_AutoSize==YES){ b_AutoSize = NO; label6.adjustsFontSizeToFitWidth = NO; } else{ b_AutoSize = YES; label6.adjustsFontSizeToFitWidth = YES; } } }