iOS开发学习笔记之UILabel的使用
来源:互联网 发布:淘宝上的狗狗疫苗 编辑:程序博客网 时间:2024/06/07 19:36
iOS开发学习笔记之UILabel的使用// 1、创建letrect:CGRect=CGRectMake(100,100,100,100)letlabel:UILabel=UILabel(frame:rect)// 2、设置和读取文本内容,默认为nillabel.text="文本信息"// 3、设置文字颜色,默认为黑色label.textColor=UIColor.redColor()// 4、font设置文字大小,默认为17label.font=UIFont.systemFontOfSize(20)//一般方法label.font=UIFont.boldSystemFontOfSize(20)//加粗方法label.font=UIFont.init(name:"Arial", size:20)//指定字体的方法//label.font = [UIFont fontWithName:@"" size:16]; //指定字体的方法// 5、textAlignment设置标签文本对齐方式label.textAlignment=NSTextAlignment.Center/**其他的对齐方式NSTextAlignmentLeft= 0,// Visually left alignedNSTextAlignmentCenter= 1,// Visually centeredNSTextAlignmentRight= 2,// Visually right alignedNSTextAlignmentRight= 1,// Visually right alignedNSTextAlignmentCenter= 2,// Visually centeredNSTextAlignmentJustified = 3,// Fully-justified. The last line in a paragraph is natural-aligned.NSTextAlignmentNatural= 4,// Indicates the default alignment for script*/// 6、numberOfLines标签最多显示行数,如果为0则表示多行label.numberOfLines=2// 7、enabled只是决定了Label的绘制方式,将它设置为NO时文本变暗,表示没有激活,这是向她设置颜色值都是无效的。label.enabled=false// 8、highlighted是否高亮显示label.highlighted=true;label.highlightedTextColor=UIColor.orangeColor()//高亮显示时候的文本颜色// 9、ShadowColor设置阴影颜色label.shadowColor=UIColor.blackColor()// 10、ShadowOffset设置阴影偏移量label.shadowOffset=CGSizeMake(-1, -1)// 11、baselineAdjustment如果==YES,控制文本基线的行为label.baselineAdjustment=UIBaselineAdjustment.None;/*UIBaselineAdjustmentAlignBaselines = 0, // default. used when shrinking text to position based on the original baseline默认,文本最上端与中线对齐。UIBaselineAdjustmentAlignCenters, //文本中线与label中线对齐。UIBaselineAdjustmentNone, //文本最低端与label中线对齐。*/// 12、Autoshrink是否自动收缩/*Fixed Font Size默认,如果label宽度小于文字长度时,文字大小不自动缩放minimumScaleFactor设置最小收缩比例,如果Label宽度小于文字长度时,文字进行收缩,收缩超过比例后,停止收缩。minimumFontSize设置最小收缩字号,如果label宽度小于文字长度时,文字字号减小,低于设定字号以后,不再减小。// 6.0以后不再使用了。*/label.minimumScaleFactor=0.5// 13、adjustsLetterSpacingToFitWidth改变字母之间的间距来适应Label大小//label.adjustsLetterSpacingToFitWidth = YES;// NS_DEPRECATED_IOS(6_0,7_0) __TVOS_PROHIBITED// Non-functional.Hand tune by using NSKernAttributeName to affect tracking, or consider using the allowsDefaultTighteningForTruncation property.// 14、lineBreakMode设置文字过长时的显示格式label.lineBreakMode=NSLineBreakMode.ByCharWrapping//以字符为显示单位显示,后面部分省略不显示label.lineBreakMode=NSLineBreakMode.ByClipping//剪切与文本宽度相同的内容长度,后半部分被删除。label.lineBreakMode=NSLineBreakMode.ByTruncatingHead//前面部分文字以……方式省略,显示尾部文字内容。label.lineBreakMode=NSLineBreakMode.ByTruncatingMiddle//中间的内容以……方式省略,显示头尾的文字内容。label.lineBreakMode=NSLineBreakMode.ByTruncatingTail//结尾部分的内容以……方式省略,显示头的文字内容。label.lineBreakMode=NSLineBreakMode.ByWordWrapping//以单词为显示单位显示,后面部分省略不显示。// 15、adjustsFontSizeToFitWidth设置字体大小适应label宽度label.adjustsFontSizeToFitWidth=true// 16、attributedText设置标签属性文本lettext:NSString="doubiqiu"lettextLabelStr:NSMutableAttributedString=NSMutableAttributedString.init(string: textasString)letbodyFont = [NSFontAttributeName:UIFont.preferredFontForTextStyle(UIFontTextStyleBody)]textLabelStr.setAttributes(bodyFont, range:NSMakeRange(2,5))label.attributedText= textLabelStr// 17、竖排文字显示每个文字加一个换行符,这是最方便和简单的实现方式。label.text="这\n个\n是\n竖\n排\n方\n向\n的\n显\n示"label.numberOfLines=0// 18、计算UILabel随字体多行后的高度letbounds:CGRect=CGRectMake(0,0,200,300);letheightLabel:CGRect= label.textRectForBounds(bounds, limitedToNumberOfLines:3)//计算20行之后的Label的Frameprint("%f",heightLabel.size.height)// 19、UILabel根据字数多少自动实现适应高度letmsgLabel:UILabel=UILabel.init(frame:CGRectMake(15,170,0,0))msgLabel.backgroundColor=UIColor.lightTextColor()msgLabel.numberOfLines=0msgLabel.lineBreakMode=NSLineBreakMode.ByWordWrappingmsgLabel.font=UIFont.init(name:"Arial", size:12)letsize:CGSize=CGSizeMake(290,1000)msgLabel.text="获取到的deviceToken,我们可以通过webservice服务提交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使用。"//CGSize msgSize = [msgLabel.text sizeWithFont:msgLabel.font constrainedToSize:size];//msgLabel.frame = CGRectMake(15, 150, 290, msgSize.height)//[msgLabel setFrame:CGRectMake(15, 150, 290, msgSize.height)];// 20、渐变字体Label//let img:UIImage = UIImage.init(named: "btn.png")!//let titleColor:UIColor = UIColor.init(patternImage: img)lettitleColor:UIColor=UIColor.init(patternImage:UIImage.init(named:"btn.png")!)lettitle:NSString="Setting"lettitleLabel:UILabel=UILabel.init(frame:CGRectMake(100,200,80,44))titleLabel.textColor= titleColortitleLabel.text= titleasStringtitleLabel.font=UIFont.boldSystemFontOfSize(20)titleLabel.backgroundColor=UIColor.clearColor()self.view.addSubview(titleLabel)// 21、Label添加边框titleLabel.layer.borderColor=UIColor.grayColor().CGColortitleLabel.layer.borderWidth=2// 22、设置圆角titleLabel.layer.cornerRadius=10titleLabel.backgroundColor=UIColor.cyanColor()// 23、设置背景色圆角titleLabel.clipsToBounds=trueself.view.addSubview(label)self.view.addSubview(msgLabel)我的代码://// ViewController.swift// ButtonAndText//// Created by SC Chen on 17/04/2017.// Copyright © 2017 SC Chen. All rights reserved.//import UIKitclass ViewController: UIViewController {@IBOutlet weak var attributeLabel: UILabel!@IBOutlet weak var mutilLineDisplayLabel: UILabel!@IBOutlet weak var displayText: UILabel!@IBOutlet weak var changeButton: UIButton!@IBAction func changeButton(_ sender: UIButton) { if sender.currentTitle == "Current" { displayText.text = "Hello World!" displayText.backgroundColor = UIColor(red: 0.6, green: 0.5, blue: 0.3, alpha: 1.0) changeButton.setTitle("Change", for: .normal) changeButton.backgroundColor = UIColor(red: 0.6, green: 0.5, blue: 0.3, alpha: 1.0) }else if sender.currentTitle == "Change"{ displayText.text = "chenling" displayText.backgroundColor = UIColor(red: 0.3, green: 0.5, blue: 0.6, alpha: 1.0) changeButton.setTitle("Current", for: .normal) changeButton.backgroundColor = UIColor(red: 0.3, green: 0.5, blue: 0.6, alpha: 1.0) }}var customizeButton: UIButton!override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //=======初始化设置changeButton===============// changeButton.setTitleColor(UIColor.red, for: .normal) changeButton.setTitleColor(UIColor.green, for: .highlighted) displayText.text = "chenling" displayText.backgroundColor = UIColor(red: 0.3, green: 0.5, blue: 0.6, alpha: 1.0) changeButton.setTitle("Current", for: .normal) changeButton.backgroundColor = UIColor(red: 0.3, green: 0.5, blue: 0.6, alpha: 1.0) //end //=======设置customizeButton的属性===========// //创建一个system类型的按键 customizeButton = UIButton(type: .custom) //设置按键的大小和位置 //customizeButton.frame = CGRect(x:10, y:150, width:200, height:40) customizeButton.frame = changeButton.frame customizeButton.frame.origin.x = changeButton.frame.origin.x + changeButton.frame.size.width + 10 //设置按键文字 customizeButton.setTitle("按键", for: .normal) customizeButton.setTitleColor(UIColor.red, for: .normal) customizeButton.setTitleColor(UIColor.green, for: .highlighted) //设置字体大小 customizeButton.titleLabel?.font = UIFont.systemFont(ofSize: 15) //设置背景颜色 customizeButton.backgroundColor = UIColor(red: 0.5, green: 0.3, blue: 0.1, alpha: 1.0) //customizeButton.backgroundColor = UIColor(red: 0.1, green: 0.3, blue: 0.5, alpha: 1.0) self.view.addSubview(customizeButton) //end //=========设置一行label显示不同字体,不同大小,不同颜色============// //定义一个attribute字符串 let attributeStr = NSMutableAttributedString(attributedString: NSAttributedString(string: "attribute string hello")) //设置字符串的字体和大小 attributeStr.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial-BoldItalicMT", size: 20)!, range: NSRange(location: 0, length: 9)) attributeStr.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 20)!, range: NSRange(location: 10, length: 6)) attributeStr.addAttribute(NSFontAttributeName, value: UIFont(name: "Courier-BoldOblique", size: 20)!, range: NSRange(location: 10, length: 6)) //设置字符串的颜色 attributeStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location: 0, length: 9)) attributeStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: 10, length: 6)) attributeStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellow, range: NSRange(location: 17, length: 5)) //将字符串显示到label attributeLabel.attributedText = attributeStr //end //===========IB添加的Label多行显示==============// mutilLineDisplayLabel.font = UIFont(name: "Arial-BoldItalicMT", size: 15) mutilLineDisplayLabel.textAlignment = NSTextAlignment.left mutilLineDisplayLabel.textColor = UIColor.red mutilLineDisplayLabel.backgroundColor = UIColor.blue mutilLineDisplayLabel.isHighlighted = true mutilLineDisplayLabel.numberOfLines = 0 mutilLineDisplayLabel.text = "Welcome to study swift\nLet's go!" //===========代码生成的Label多行显示==============// let mutilLineDisplayLabelByCodeCreate = UILabel(frame: CGRect(x: mutilLineDisplayLabel.frame.origin.x, y: mutilLineDisplayLabel.frame.origin.y + mutilLineDisplayLabel.frame.size.height + 10, width: mutilLineDisplayLabel.frame.size.width - 100, height: mutilLineDisplayLabel.frame.size.height)) mutilLineDisplayLabelByCodeCreate.font = UIFont(name: "HelveticaNeue-Bold", size: 15) mutilLineDisplayLabelByCodeCreate.textAlignment = NSTextAlignment.left mutilLineDisplayLabelByCodeCreate.textColor = UIColor.black mutilLineDisplayLabelByCodeCreate.backgroundColor = UIColor.green mutilLineDisplayLabelByCodeCreate.isHighlighted = true mutilLineDisplayLabelByCodeCreate.numberOfLines = 0 mutilLineDisplayLabelByCodeCreate.layer.cornerRadius = 50 mutilLineDisplayLabelByCodeCreate.backgroundColor = UIColor.cyan mutilLineDisplayLabelByCodeCreate.text = "Welcome to study swift\nLet's go!" self.view.addSubview(mutilLineDisplayLabelByCodeCreate)}override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated.}}运行效果图:
阅读全文
0 0
- iOS开发学习笔记之UILabel的使用
- iOS开发之UILabel的使用
- IOS开发学习笔记-----UILabel
- iOS开发笔记--iOS组件之UILabel
- iOS开发笔记--iOS组件之UILabel
- iOS开发笔记之三十——UILabel的使用总结
- IOS开发学习笔记(1)-----UILabel 详解
- IOS开发学习笔记-----UILabel 详解
- IOS开发学习笔记 UILabel 详解
- IOS开发学习笔记(1)-----UILabel 详解
- IOS开发学习笔记(1)-----UILabel 详解
- iOS开发之三:常用控件--UILabel的使用
- iOS开发UI系列之UILabel的基本使用
- iOS学习笔记之UILabel文本自适应
- 【ios开发学习 - 第六课】UILabel使用
- IOS开发之UILabel
- iOS开发之UILabel
- ios开发-UIView和UILabel的学习
- TF/02_TensorFlow_Way/05 Implementing Back Propagation
- 面试题17合并两个排序链表
- CSS
- h264 NAL头解析
- NGINX的原理、安装、配置以及使用tomcat在linux上搭载集群环境
- iOS开发学习笔记之UILabel的使用
- android volley
- 计算机视觉 -- 最优化阈值
- web.xml在web项目中的作用,没有web.xml怎么配置项目
- 八大排序算法实战:思想与实现
- MySQL数据备份"--no-beep"异常解决
- sizeof的基本用法的整理
- 贵安新区飘起“数据云”
- CS231nNeural Networks Part 1: Setting up the Architecture