- //UILable的大小自适应实例
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, 2)];//对控件UILabel进行初始化并设定位置与大小
[myLable setFont:[UIFont fontWithName:@"Helvetica" size:20.0]];//设置显示的字体格式和大小
[myLable setNumberOfLines:0];//行数,只有设为0才能自适应
[myLable setBackgroundColor:[UIColor clearColor]];//背景色
myLable.shadowColor = [UIColor darkGrayColor];//阴影颜色
myLable.shadowOffset = CGSizeMake(1., 1.0);//阴影大小
NSString *text = @"abcdefghijklmnopqrstuvwxyz";//创建变量存放控件要显示的内容
UIFont *font = [UIFont fontWithName:@"Helvetica" size:20.0];//创建变量存放内容的字体格式和字体大小
- CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap];//设置要显示字体、控件大小、文字截取
- //lineBreakModeWordWrap 使用的包装和标签的文本截断技术。
-
CGRect rect = myLable.frame;
rect.size = size;
[myLable setFrame:rect];
[myLable setText:text];//设置控件显示的内容
myLable.shadowColor = [UIColor darkGrayColor];//阴影颜色
myLable.shadowOffset = CGSizeMake(2.0, 2.0);//阴影大小
[self.view addSubview:myLable];//视图添加控件,并显示
[myLable release];
//UILable的基本用法
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 40.0, 200.0, 30.0)];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)];
UILabel *lbl3 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)];
UILabel *lbl4 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)];
UILabel *lbl5 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)];
UILabel *lbl6 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)];
UILabel *lbl7 = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)];
//设置显示文字
lbl1.text = @"lable1";
lbl2.text = @"lable2";
lbl3.text = @"lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--lable3--11个";
lbl4.text = @"lable4--lable4--lable4--lable4--4个";
lbl5.text = @"lable5--lable5--lable5--lable5--lable5--lable5--6个";
lbl6.text = @"lable6";
lbl7.text = @"lable7";
//设置字体:粗体,正常的是SystemFontOfSize
lbl1.font = [UIFont boldSystemFontOfSize:20];
//设置文字颜色
lbl1.textColor = [UIColor orangeColor];
lbl2.textColor = [UIColor purpleColor];
//设置背景颜色
lbl1.backgroundColor = [UIColor clearColor];
lbl2.backgroundColor = [UIColor colorWithRed:0.5f green:30/255.0f blue:0.3f alpha:0.5f];
//设置字体位置
lbl1.textAlignment = UITextAlignmentRight;//右对齐
lbl2.textAlignment = UITextAlignmentCenter;//居中对齐
//设置字体的小适应lable的宽度(该值指示字体大小是否应该被缩小以将标题字符串匹配到标签的边界矩形中。默认是NO )
lbl4.adjustsFontSizeToFitWidth = YES;
//设置lable 的行数(用于绘制文本的最大行数)默认值为1(单行)。价值0指无限制.如果文本的高度达到线或视图的高度#小于线路允许#,文本将
采用线中断模式。
lbl5.numberOfLines = 2;
//设置高亮
lbl6.highlighted = YES;//将改控件设置为高亮
lbl6.highlightedTextColor = [UIColor orangeColor];//设置高亮颜色
//设置阴影
lbl7.shadowColor = [UIColor redColor];//设置阴影颜色
lbl7.shadowOffset = CGSizeMake(1.0, 1.0);//设置阴影大小
//设置是否能与用户进行交互
lbl7.userInteractionEnabled = YES;
//设置lable中文字是否可变,默认为YES;(在绘制标签文本时使用的状态。)
lbl3.enabled = NO;
//设置lable中文字过长时的显示格式(文本截取技术)默认是nslinebreakbytruncatingtail。用于单个和多行文本
lbl3.lineBreakMode = UILineBreakModeMiddleTruncation; //截去中间
// typedef enum{
// UILineBreakModeWordWrap = 0,
// UILineBreakModeCharacterWrap,
// UILineBreakModeClip,//截去多余部分
// UILineBreakModeHeadTruncation,//截取头部
// UILineBreakModeTailTruncation,//截去尾部
// UILineBreakModeMiddleTruncation,//截去中间
// }UILineBreakMode;
//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就用来控制文本基线的行为(如何控制文本基线时调整文本需要缩小到适合的标签。默认是UIBaselineAdjustmentAlignBaselines)
lbl4.baselineAdjustment = UIBaselineAdjustmentNone;
[self.view addSubview:lbl1];
[self.view addSubview:lbl2];
[self.view addSubview:lbl3];
[self.view addSubview:lbl4];
[self.view addSubview:lbl5];
[self.view addSubview:lbl6];
[self.view addSubview:lbl7];
[lbl1 release];
[lbl2 release];
[lbl3 release];
[lbl4 release];
[lbl5 release];
[lbl6 release];
[lbl7 release]; -