UILabel主要是一些标签文本的使用控件,教程里主要涉及到的是UILabel的普通创建及转变效果,带背景边框的label,还有圆角的设置,给UILabel增加点击时间,阴影效果,文本的排版效果(根据Label的宽度调整字体),多行文本的使用,显示HTML标签代码等等.下面是代码,可以复制到Xcode里执行.其他的相关属性,详细查看苹果开发文档
importUIKit
classViewController:UIViewController{
overridefuncviewDidLoad(){
super.viewDidLoad()
//普通label
varlabel:UILabel=UILabel(frame:CGRect(x:50,y:50,width:100,height:35))
label.text="普通Label"
label.transform=CGAffineTransformMakeRotation(0.2)
self.view.addSubview(label)
//带背景和边框的label
//Label的圆角对其背景不起作用,只对其边框起作用
varlabel2:UILabel=UILabel(frame:CGRect(x:50,y:90,height:35))
label2.text="圆角Label"
label2.textColor=UIColor.whiteColor()
label2.backgroundColor=UIColor.blackColor()
label2.textAlignment=NSTextAlignment.Center
label2.layer.cornerRadius=10
label2.layer.borderWidth=2
label2.layer.borderColor=UIColor.redColor().CGColor
self.view.addSubview(label2)
//UILabel拥有点击事件
varlabel3:UILabel=UILabel(frame:CGRect(x:50,y:130,height:35))
label3.text="我有点击事件"
label3.adjustsFontSizetoFitWidth=true//根据label的宽度,改变字体的大小
vartap:UITapGestureRecognizer=UITapGestureRecognizer(target:self,action:"click:")
label3.userInteractionEnabled=true
label3.addGestureRecognizer(tap)
label3.shadowColor=UIColor.purpleColor()//设置shadow
label3.shadowOffset=CGSize(width:2,height:2)
self.view.addSubview(label3)
//让Label显示html标签
varlabel4:UILabel=UILabel(frame:CGRect(x:50,y:170,width:300,height:35))
lethtml="thisishtml<ahref=\"http://www.baidu.com\">link</a>"
letdata=html.dataUsingEncoding(NSUTF32StringEncoding,allowLossyConversion:false)
varatext=NSAttributedString(data:data!,options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],documentAttributes:nil,error:nil)
label4.multipletouchEnabled=true
label4.attributedText=atext
self.view.addSubview(label4)
//设置多行文本
varlabel5:UILabel=UILabel(frame:CGRect(x:50,y:210,width:200,height:80))
label5.backgroundColor=UIColor.purpleColor()
label5.textColor=UIColor.whiteColor()
label5.text="在设置rootViewController之后,在这个control里的viewDidLoad方法里,添加相应的代码.设置了9个不同类型的UIButton";
label5.lineBreakMode=NSLineBreakMode.ByTruncatingTail
label5.numberOfLines=10
label5.adjustsFontSizetoFitWidth=true
self.view.addSubview(label5)
}
overridefuncdidReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//dispoSEOfanyresourcesthatcanberecreated.
}
funcclick(sender:AnyObject){
vartap:UITapGestureRecognizer=senderas!UITapGestureRecognizer
NSLog("thisisclick")
}
}
效果如下:

转载自吴统威的博客:http://www.wutongwei.com/front/infor_showone.tweb?id=89