UILabel(标签):是显示文本的控件,是能显示文字的视图,UILable继承自UIView增加了文字显示功能
-创建UILable:
// UILable 继承自UIView 增加了文字显示功能
UILabel *lablel = [[UILabel alloc] initWithFrame:CGRectMake(70, 100, 200, 100)];
lablel.backgroundColor = [UIColor whiteColor];
// text属性: 为label添加文本显示 默认系统17号字 黑色
lablel.text = @"label";
// textColor属性
lablel.textColor = [UIColor redColor];
// textAlignmeng 属性 枚举类型(左对齐,右对齐,居中)
// lablel.textAlignment = NSTextAlignmentRight;
// lablel.textAlignment = NSTextAlignmentLeft;
lablel.textAlignment = NSTextAlignmentCenter;
// font属性 设置字体 系统默认17号字体
lablel.font = [UIFont systemFontOfSize:20];
// lablel.font = [UIFont fontWithName:@"Bangla" size:20];
shadowColor属性 字体阴影颜色
// lablel.shadowColor = [UIColor magentaColor];
shadowOffSet 阴影偏移
// lablel.shadowOffset = CGSizeMake(5, 5);
// li'nelineBreakMode属性:label换行标准
lablel.text = @"Yaoxu have a dream Yaoxu have a dream Yaoxu have a dream ";
// 如果设置行数 0 代表无限行数 (只要label 的尺寸内可以显示)
lablel.numberOfLines = 1;
// lablel.lineBreakMode = NSLineBreakByWordWrapping;
lablel.lineBreakMode = NSLineBreakByClipping;
// lablel.lineBreakMode = NSLineBreakByCharWrapping;
// lablel.lineBreakMode = NSLineBreakByTruncatingTail;// 末尾省略号
// lablel.lineBreakMode = NSLineBreakByTruncatingMiddle;//中间省略号
// lablel.lineBreakMode = NSLineBreakByTruncatingHead; // 开头省略号
// lablel.alpha = 0.2;
-设置label边框:
self.isTicketLabel.layer.borderWidth=1;
-设置label边框颜色:
self.isTicketLabel.layer.borderColor= [UIColorredColor].CGColor;
-iOS使用UILabel制作超链接:
在ios中没有直接的组件来显示并响应超链接。在ios实现超链接首先可以使用UIlabel和UIview两个组件可以实现具有下滑线的图标,然后使用gesture recognizer增加对超链接的响应。
1、制作有下划线的图标
1)将UIlabel和UIview拖入storyboard或者xib文件中,在label中设置显示的文字
2)选中UIview,在show the size inspector中将view的height设置为1,调整X、Y坐标值,使得界面显示合适
2、增加对超链接的响应
在viewDidLoad函数中增加对label触摸的响应
UITapGestureRecognizer*tapRecognizer1=[[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(clickurl1:)];
self.label2.userInteractionEnabled=YES;
[self.label2addGestureRecognizer:tapRecognizer1];
3、实现响应的函数
-(void)clickurl1:(id)sender
{
NSString* path=[NSStringstringWithFormat:@"http://www.baidu.com"];
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:path]];
}