【非凡程序员】Swift- 手动生成UIKit

下面是在程序生成5个标签,文本框,按钮的语句

  for  var i=0;i<5;i++ {

            var y:CGFloat = CGFloat(i*100 + 50)

            var SwiftLable=UILabel()

            SwiftLable.frame = CGRectMake (10 , y, 60, 40)

            SwiftLable.text = "标题"

            SwiftLable.backgroundColor = .redColor()

            self.view.addSubview(SwiftLable)

        

            var SwiftTextField=UITextField()

            SwiftTextField.frame=CGRectMake(80, y , 180, 40)

            SwiftTextField.borderStyle = UITextBorderStyle.RoundedRect

            SwiftTextField.placeholder="请输入字符串"

            array.append(SwiftTextField)

            self.view.addSubview(SwiftTextField)


            var SwiftButton=UIButton()

            SwiftButton.frame=CGRectMake(300, y , 60, 40)

            SwiftButton.backgroundColor = .greenColor()

            SwiftButton.tag=i

            SwiftButton.setTitle("change" , forState: UIControlState.Normal)

            SwiftButton.addTarget(self, action: "tickMethod:", forControlEvents: UIControlEvents.TouchUpInside)

            self.view.addSubview(SwiftButton)

            

        }

由于文本框除了设置颜色外还有其他方法显示,搜索资料如下:

1,文本框的创建,有如下几个样式:

UITextBorderStyle.None:无边框

UITextBorderStyle.Line:直线边框

UITextBorderStyle.RoundedRect:圆角矩形边框

UITextBorderStyle.Bezel:边线+阴影

1
2
3
4
var  textField =  UITextField (frame:  CGRectMake (10,160,200,30))
//设置边框样式为圆角矩形
textField.borderStyle =  UITextBorderStyle . RoundedRect
self .view.addSubview(textField)


2,文本框提示文字

1
textField.placeholder= "请输入用户名"


3,文字大小超过文本框长度时自动缩小字号,而不是隐藏显示省略号

1
2
textField.adjustsFontSizeToFitWidth= true   //当文字超出文本框宽度时,自动调整文字大小
textField.minimumFontSize=14   //最小可缩小的字号


4,水平/垂直对齐方式

1
2
3
4
5
6
7
8
9
/** 水平对齐 **/
textField.textAlignment = . Right  //水平右对齐
textField.textAlignment = . Center  //水平居中对齐
textField.textAlignment = . Left  //水平左对齐
 
/** 垂直对齐 **/
textField.contentVerticalAlignment = . Top   //垂直向上对齐
textField.contentVerticalAlignment = . Center   //垂直居中对齐
textField.contentVerticalAlignment = . Bottom   //垂直向下对齐


5,背景图片设置

1
textField.background= UIImage (named: "background1" );


你可能感兴趣的:(【非凡程序员】Swift- 手动生成UIKit)