UILabel的各种属性和方法
转自:http://liulu200888an.blog.163.com/blog/static/3498972320121214208542/
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 20.0, 180.0,50.0)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20.0, 80.0, 180.0, 50.0)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(20.0, 140.0, 180.0, 50.0)];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(20.0, 200.0, 180.0, 50.0)];
//设置显示文字
label1.text = @"labelone";
label2.text = @"labeltwo";
label3.text = @"labelthree-labelthree-labelthree-labelthree-labelthree-labelthree";
label4.text = @"labelfour";
//设置文字颜色
label1.textColor = [UIColor redColor];
label2.textColor = [UIColor blueColor];
//设置字体:粗体,正常的是 SystemFontOfSize
label1.font = [UIFont boldSystemFontOfSize:30];
//设置文字位置,居中还是靠左靠右
label1.textAlignment = UITextAlignmentRight;
label2.textAlignment = UITextAlignmentCenter;
//设置字体大小适应label宽度
label4.adjustsFontSizeToFitWidth = YES;
//设置label的行数
label3.numberOfLines = 2;
//设置label的背景为透明
label2.backgroundColor = [UIColor clearColor];
//设置高亮
label4.highlighted = YES;
label4.highlightedTextColor = [UIColor orangeColor];
//设置阴影
label1.shadowColor = [UIColor redColor];
label1.shadowOffset = CGSizeMake(1.0,1.0);
//设置是否能与用户进行交互
label3.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];
[label1 release];
[label2 release];
[label3 release];
[label4 release];
//点击label1时显示白色(是点击label1,不是点击label.Text)
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[label1 setTextColor:[UIColor whiteColor]];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[label1 setTextColor:[UIColor blackColor]];
UITouch *touch = [touches anyObject];
// CGPoint points = [touch locationInView:<#(UIView *)#>];
CGPoint points = [touch locationInView:label];
if (points.x >= label.frame.origin.x && points.y >= label.frame.origin.x && points.x <= label.frame.size.width && points.y <= label.frame.size.height)
{
// [delegate myLabel:self touchesWtihTag:self.tag];
}
}