您的位置:首页 > 产品设计 > UI/UE

iOS控件之UILabel

2016-06-01 12:29 381 查看
一,LabelDemo

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(90, 100, 140, 40)];
label.backgroundColor = [UIColor redColor];
label.text = @"abcdefgkdkdkdkdkdkdkdkd";
//    label.font = [UIFont systemFontOfSize:14];
//    label.numberOfLines = 0; // no limit 默认文本是一行显示
label.lineBreakMode = NSLineBreakByTruncatingHead;
//label宽度不够时,对文本进行打断的方式
//NSLineBreakByTruncatingHead文本太长会在前面显示...
label.shadowColor = [UIColor yellowColor];//阴影颜色
label.shadowOffset = CGSizeMake(-2, 2);//偏移量
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blueColor];
[label sizeToFit];//文本内容比较长会自适应
[self.window addSubview:label];
[label release];

[self.window makeKeyAndVisible];
return YES;
}


二,Label属性

//创建uilabel

 

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];

 

//设置背景色

label1.backgroundColor = [UIColor grayColor];

 

//设置tag

label1.tag = 91;

 

//设置标签文本

label1.text = @CCBASE.NET!;

 

//设置标签文本字体和字体大小

label1.font = [UIFont fontWithName:@Arial size:30];

 

//设置文本对齐方式

label1.textAlignment = UITextAlignmentCenter;

 

//文本对齐方式有以下三种

 //typedef enum {

// UITextAlignmentLeft = 0,左对齐

// UITextAlignmentCenter,居中对齐

// UITextAlignmentRight, 右对齐 

//} UITextAlignment;

 

//文本颜色

label1.textColor = [UIColor blueColor];

 

//超出label边界文字的截取方式

label1.lineBreakMode = UILineBreakModeTailTruncation;

 

//截取方式有以下6种

//typedef enum { 

// UILineBreakModeWordWrap = 0, 以空格为边界,保留整个单词 

 

// UILineBreakModeCharacterWrap, 保留整个字符 

 

// UILineBreakModeClip, 到边界为止 

 

// UILineBreakModeHeadTruncation, 省略开始,以……代替 

 

// UILineBreakModeTailTruncation, 省略结尾,以……代替 

 

// UILineBreakModeMiddleTruncation,省略中间,以……代替,多行时作用于最后一行 

 

//} UILineBreakMode;

 

 

//文本文字自适应大小

 

label1.adjustsFontSizeToFitWidth = YES;

 

//当adjustsFontSizeToFitWidth=YES时候,如果文本font要缩小时

//baselineAdjustment这个值控制文本的基线位置,只有文本行数为1是有效

label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

 

//有三种方式

//typedef enum {

// UIBaselineAdjustmentAlignBaselines = 0, 默认值文本最上端于label中线对齐

// UIBaselineAdjustmentAlignCenters,//文本中线于label中线对齐

// UIBaselineAdjustmentNone,//文本最低端与label中线对齐

//} UIBaselineAdjustment;

 

//文本最多行数,为0时没有最大行数限制

label1.numberOfLines = 2;

//最小字体,行数为1时有效,默认为0.0

label1.minimumFontSize = 10.0;

//文本高亮

label1.highlighted = YES;

//文本是否可变

label1.enabled = YES;

//去掉label背景色

//label1.backgroundColor = [UIColor clearColor];

//文本阴影颜色

label1.shadowColor = [UIColor grayColor];

//阴影大小

label1.shadowOffset = CGSizeMake(1.0, 1.0);

//是否能与用户交互

label1.userInteractionEnabled = YES;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: