大佬教程收集整理的这篇文章主要介绍了swift基础篇:iOS控件大拌菜:UILable UIButton UITextView,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<span style="font-size:14px;">override func viewDidLoad() { super.viewDidLoad() let label1 = UILabel(frame: CGRectMake(10,50,300,400)) label1.text = "swift" // 背景颜色,字体颜色,对齐方式 label1.BACkgroundColor = UIColor.grayColor() label1.textColor = UIColor.yellowColor() label1.textAlignment = NSTextAlignment.Center // 文字大小自适应 label1.adjustsFontSizeToFitWidth = true // 设置最小能接受的缩放比例 label1.minimumScaleFactor = 0.8 // 设置行数 0表示行数不限 label1.numberOfLines = 0 // 换行方式(按字母换行) label1.lineBreakmode = .byCharWrapping // 字体设置:系统字体的30号 正常,粗体,斜体;个性化字体 label1.font = UIFont.systemFontOfSize(30) label1.font = UIFont.boldSystemFontOfSize(30) label1.font = UIFont.italicSystemFontOfSize(30) label1.font = UIFont(name: "Zapfino",size: 20) // 通过如下方式可以获得系统支持的左右字体名称 print(UIFont.familyNames()) // 阴影设置 label1.shadowColor = UIColor.lightGrayColor() label1.shadowOffset = CGSizeMake(3,3) self.view.addSubview(label1) } </span>
如上为UILabel的设置方式
下面来说一说UIButton
下面是一些基本的设置:
<span style="font-size:14px;">@IBOutlet weak var button: UIButton! override func viewDidLoad() { super.viewDidLoad() button.BACkgroundColor = UIColor.lightGrayColor() // 另一种按钮的创建方式 let button1 = UIButton(type: UIButtonType.RoundedRect) button1.layer.cornerRadius = 5 button1.frame = CGRectMake(10,44) button1.BACkgroundColor = UIColor.lightGrayColor() button1.settitle("bello Hello",forState: UIControlState.Normal) self.view.addSubview(button1) // 设置按钮的文字和颜色 并且是在普通状态下的字 button.settitleColor(UIColor.greenColor(),forState: UIControlState.Normal) button.settitle("bello Hello",forState: UIControlState.Normal) button.layer.cornerRadius = 5 // 设置按钮的字体和行数 button.titleLabel?.font = UIFont(name: "Zapfino",size: 30) button.titleLabel?.numberOfLines = 0 }<span style="color:#3366ff;"> </span></span>
接下来是按钮的状态:
有的按钮在吃饭,有的按钮在睡觉。。。噗,开玩笑的:
state:
1、Normal:普通状态,此时木有与按钮进行任何交互
2、Highlighted:高亮状态,此时为按下按钮,但是木有离开按钮
3、Disabled:按钮不可用(大概是睡着了)
4、SELEcted:按钮选中时
栗子代码:
<span style="font-size:14px;"><span style="white-space:pre"> </span>// 按钮状态 button.settitleColor(UIColor.blackColor(),forState: UIControlState.Normal) button.settitleColor(UIColor.blueColor(),forState: UIControlState.Highlighted) button.settitle("这时按钮无效",forState: UIControlState.Disabled) button.enabled = false</span>
按钮的类型:栗子:
<span style="font-size:14px;"><span style="white-space:pre"> </span>// 按钮的类型 let button2 = UIButton(type: UIButtonType.DetailDisclosurE) button2.frame = CGRectMake(10,100,44) button2.settitle("bello Hello",forState: UIControlState.Normal) self.view.addSubview(button2) let button3 = UIButton(type: UIButtonType.InfoDark) button3.frame = CGRectMake(10,150,44) button3.settitle("bello Hello",forState: UIControlState.Normal) self.view.addSubview(button3) let button4 = UIButton(type: UIButtonType.ContactAdd) button4.frame = CGRectMake(10,200,44) button4.settitle("bello Hello",forState: UIControlState.Normal) self.view.addSubview(button4)</span>效果是酱紫的:
还可以用图片给按钮设置背景:
这种方式下图片会被拉伸以适应按钮
********************************
这种方式为图片的平铺
********************************
方法名:setImage
在项目中使用图片,需要先将图片导入到工程中,然后在代码中加载图片,加载图片有两种方式:
1、UIImage(contentsOfFile:图片路径)
第一种方式加载的图片不会一直保留在程序运行的活跃内存中,多用来加载大图
第二种方式加载的图片会一直留在内存中,多用于加载常用的小图标
如果使用UIButton.System类型创建一个按钮,设置按钮图片时,图片会被纯色代替
<span style="font-size:14px;"> var img = UIImage(named: "1") img = img!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)</span>
以上是解决办法。
按钮事件类型:
常用的按钮能够响应的事件类型如下:
TouchDown:在按钮范围内按下
TouchDownRepeat:在按钮上连按两下
TouchDragInside:在按钮响应范围内滑动
TouchDragOutside:在按钮响应范围之外滑动(滑动的起点在按钮的外围内)
TouchDragEnter:从按钮的响应范围内滑出按钮,再次进入按钮的响应范围
TouchDragExit:从按钮的响应范围内滑出按钮
TouchUpInside:在按钮的响应范围内抬起手指
TouchUpOutside:在按钮的响应范围外抬起手指
如上所有的响应事件类型想要触发都有一个前提,那就是能够被响应,即手指对于按钮的起始作用点一定是要在按钮的响应范围内
呼呼,按钮的东西好多,毕竟是一个很重要的控件~下面来说UITextView
单行文本框~
应用场景也很多,比如,登录注册的页面,输入用户名和密码的地方~就是它,是它是它就是它~
默认状态下,输入框的背景是透明的,而且木有边框,所以有时候我们看不到它~
说句题外话,包括做前端什么的时候,经常有那种,诶?代码明明有的啊,怎么木有 出现?调试一下@R_448_10585@,原来只是因为这家伙是透明的。。。所以,写代码的各位要细心~
言归正传了~
<span style="font-size:14px;"> let tf = UITextField(frame: CGRectMake(10,44)) // 边框,背景,字体颜色,字体,边框的设置 tf.borderstyle = UITextBorderstyle.bezel tf.BACkgroundColor = UIColor.lightGrayColor() tf.textColor = UIColor.blueColor() tf.font = UIFont(name: "Zapfino",size: 10) tf.borderstyle = UITextBorderstyle.RoundedRect // 提示字符 tf.placeholder = "兔几喜欢fu萝卜" // 密码输入框,输入的密码会自动变成点点,不是坏掉了~ tf.secureTextEntry = true // 对齐方式 tf.contentVerticalAlignment = .Top tf.textAlignment = NSTextAlignment.Left // 成为第一响应者,这时候,界面一出现,光标就被定位在该输入框中 tf.becomeFirstResponder() // 取消第一响应者 tf.resignFirstResponder() // 可以使用如下方法使得一个视图内所有的输入框都失去第一响应者 self.view.endEdiTing(true) // 文字的清除模式,在输入框输入的时候,右侧可以显示一个清除按钮 tf.clearButtonMode = UITextFieldviewmode.WhileEdiTing//只有当输入框处于第一响应者时才显示 tf.clearButtonMode = UITextFieldviewmode.UnlessEdiTing//只有当输入框处于第一响应者的时候隐藏 self.view.addSubview(tf)</span>
另外,在使用输入框的时候,需要对键盘进行个性化设置,可以设置成英文,或者数字模式等等,如下是键盘风格的定义代码:
<span style="font-size:14px;">public enum UIKeyboardType : Int { case Default // Default type for the current input method. case ASCIICapable // Displays a keyboard which can enter ASCII characters,non-ASCII keyboards remain active case numbersAndPunctuation // numbers and assorted punctuation. case URL // A type optimized for URL entry (shows . / .com prominently). case numberPad // A number pad (0-9). Suitable for PIN entry. case PhonePad // A phone pad (1-9,*,#,with letters under the numbers). case NamePhonePad // A type optimized for entering a person's name or phone number. case EmailAddress // A type optimized for multiple email address entry (shows space @ . prominently). @available(iOS 4.1,*) case decimaLPAD // A number pad with a decimal point. @available(iOS 5.0,*) case Twitter // A type optimized for twitter text entry (easy access to @ #) @available(iOS 7.0,*) case WebSearch // A default keyboard type with URL-oriented addition (shows space . prominently). public static var Alphabet: UIKeyboardType { get } // Deprecated } </span>使用方式如下:
<span style="font-size:14px;"> tf.keyboardType = UIKeyboardType.EmailAddress</span>
另外,如果需要监控输入的内容,则需要使用UITextView的代理方法来实现(UITextFeildDelegatE)
<span style="font-size:14px;"> tf.delegate = self</span>呼呼~这一篇差不多了,作为一只兔子要去蹦跶一会儿了~
以上是大佬教程为你收集整理的swift基础篇:iOS控件大拌菜:UILable UIButton UITextView全部内容,希望文章能够帮你解决swift基础篇:iOS控件大拌菜:UILable UIButton UITextView所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。