XCode中动态添加Label控件

复制代码
UILabel *label = [[ UILabel alloc] init];
label.text = @"xxx";
label.frame = CGRectMake(0,0,100,100) ;
label.backgroundColor = [UIColor redColor];

//添加事件。
label.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];  
[label addGestureRecognizer:singleTap];  
[singleTap release];

[self.view addSubview:label];
[label release] ;

//事件方法
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer 
{
    //事件处理。
}
复制代码

 

 

复制代码
#import "LabelTestViewController.h"     
@implementation LabelTestViewController     
/*   
 Accessing the Text Attributes   
 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     
 */    
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.     
- (void)viewDidLoad {     
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)];     
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];     
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];     
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];     
    UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];     
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];     
    UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];     
    //设置显示文字     
    label1.text = @"label1";     
    label2.text = @"label2";     
    label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";     
    label4.text = @"label4--label4--label4--label4--";     
    label5.text = @"label5--label5--label5--label5--label5--label5--";     
    label6.text = @"label6";     
    label7.text = @"label7";     
    //设置字体:粗体,正常的是 SystemFontOfSize     
    label1.font = [UIFont boldSystemFontOfSize:20];     
    //设置文字颜色  
    label1.textColor = [UIColor orangeColor];     
    label2.textColor = [UIColor purpleColor];     
    //设置文字位置     
    label1.textAlignment = UITextAlignmentRight;     
    label2.textAlignment = UITextAlignmentCenter;     
    //设置字体大小适应label宽度     
    label4.adjustsFontSizeToFitWidth = YES;     
  
    //设置label的行数     
    label5.numberOfLines = 2;    
    UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色   
 
    //设置高亮     
    label6.highlighted = YES;     
    label6.highlightedTextColor = [UIColor orangeColor];     
    //设置阴影     
    label7.shadowColor = [UIColor redColor];     
    label7.shadowOffset = CGSizeMake(1.0,1.0);     
    //设置是否能与用户进行交互     
    label7.userInteractionEnabled = YES;     
    //设置label中的文字是否可变,默认值是YES     
    label3.enabled = NO;     
    //设置文字过长时的显示格式     
    label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间     
//  typedef enum {     
//      UILineBreakModeWordWrap = 0,     
//      UILineBreakModeCharacterWrap,     
//      UILineBreakModeClip,//截去多余部分     
//      UILineBreakModeHeadTruncation,//截去头部     
//      UILineBreakModeTailTruncation,//截去尾部     
//      UILineBreakModeMiddleTruncation,//截去中间     
//  } UILineBreakMode;     
    //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为     
    label4.baselineAdjustment = UIBaselineAdjustmentNone;     
//  typedef enum {     
//      UIBaselineAdjustmentAlignBaselines,     
//      UIBaselineAdjustmentAlignCenters,     
//      UIBaselineAdjustmentNone,     
//  } UIBaselineAdjustment;     
    [self.view addSubview:label1];     
    [self.view addSubview:label2];     
    [self.view addSubview:label3];     
    [self.view addSubview:label4];     
    [self.view addSubview:label5];     
    [self.view addSubview:label6];     
    [self.view addSubview:label7];     
    [label1 release];     
    [label2 release];     
    [label3 release];     
    [label4 release];     
    [label5 release];     
    [label6 release];     
    [label7 release];     
    [super viewDidLoad];     
}     
/*   
 // Override to allow orientations other than the default portrait orientation.   
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {   
 // Return YES for supported orientations   
 return (interfaceOrientation == UIInterfaceOrientationPortrait);   
 }   
 */    
- (void)didReceiveMemoryWarning {     
    // Releases the view if it doesn't have a superview.     
    [super didReceiveMemoryWarning];     
    // Release any cached data, images, etc that aren't in use.     
}     
- (void)viewDidUnload {     
    // Release any retained subviews of the main view.     
    // e.g. self.myOutlet = nil;     
}     
- (void)dealloc {     
    [super dealloc];     
}     
@end  
复制代码
复制代码
label中文字跑马灯效果
 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 100)];
    label.text = @"噜啦啦噜啦啦噜啦噜啦噜,噜啦噜啦噜啦噜啦噜啦噜~~~";
    [self.view addSubview:label];
    CGRect frame = label7.frame;
frame.origin.x = -180;
label.frame = frame;
[UIView beginAnimations:@"testAnimation" context:NULL];
[UIView setAnimationDuration:8.8f];  
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
[UIView setAnimationDelegate:self];  
[UIView setAnimationRepeatAutoreverses:NO];  
[UIView setAnimationRepeatCount:999999]; 
frame = label.frame;
frame.origin.x = 350;
label.frame = frame;
[UIView commitAnimations]; 
复制代码
posted @   有容乃大  阅读(11813)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示