第二周第一天label.textfile属性

lable,button,textfile

lable,textfile,imageview属性

  • lable属性
// 创建label
        let label = UILabel()
        label.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        self.view.addSubview(label)
        // 给label设置文字
        label.text = "张三你好"
        // 文字的颜色
        label.textColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
        // 文字的字体及字号
        label.font = UIFont.systemFont(ofSize: 18)
        //label.font = UIFont(name: <#T##String#>, size: <#T##CGFloat#>)
        label.textAlignment = .center
        // label的背景颜色
        label.backgroundColor = UIColor.blue
        // 文字的换行模式如果是0的话,不限制
        label.numberOfLines = 1
        // 如果label大小不足以放下文字,省略模式
        label.lineBreakMode = .byTruncatingTail
        

  • textfile属性
// UITextField
        let textfield = UITextField(frame: CGRect(x: 100, y: 250, width: 200, height: 40))
        view.addSubview(textfield)
        // 设置placeholder, 提示
        textfield.placeholder = "请输入密码"
        // 边框样式
        textfield.borderStyle = .none
        // 文字的清除样式
        textfield.clearsOnBeginEditing = true
        // 边框样式
        let inset = UIEdgeInsets(top: 19, left: 30, bottom: 19, right: 18)
        //图片的拉伸
        textfield.background = UIImage(named: "2.jpg")?.resizableImage(withCapInsets: inset, resizingMode: UIImageResizingMode.stretch)
        //当我们编辑的时候出来删除按钮
        textfield.clearButtonMode = .whileEditing
        // 左边视图的样式
        //        textfield.leftView = UIImageView(image: UIImage(named: "钥匙.png"))
        //        // 什么时候显示左边视图
        //        textfield.leftViewMode = .always
        
        // 代理方法
        textfield.delegate  = self;
        self.userTextfield = textfield
        // 显示密码
        textfield.isSecureTextEntry = true
    }
    
    // textfieldDelegate方法
    // 是否可以开始编辑
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true
    }
    // 开始编辑
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print("开始编辑")
    }
    // 结束编辑
    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
        print("结束编辑")
    }
    // 是不是可以结束编辑
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        return true
    }
    
    // return 键
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    // 是否可以被清除
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        return true
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //取消第一响应者
        self.userTextfield?.resignFirstResponder()
        
        // 当光标在textfield之上的时候FirstResponder
        // 如果我们想让textfield处于编辑状态,becomeFirstResponder()
        // 当我们想键盘回收的时候
        // resignFirstResponder()
    }
}

// 图片拉伸
extension UIImage {
    
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,404评论 0 1
  • * 列表标签 》有序列表(ul>li) 北京 你好 上海 成都 效果-——》 北京 你好 上海 成都 》无序列表...
    竹溪穆褕阅读 186评论 0 0
  • 一、UIView常见属性 1.frame 位置和尺寸(以父控件的左上角为原点(0,0)) 2.center 中点(...
    脱下国际篮化身程序猿阅读 317评论 0 0
  • 逻辑思路:button设置图片和文字后后再设置titleEdgeInsets属性和 imageEdgeInsets...
    CharlyZheng阅读 4,666评论 2 21
  • 这两天馒头感冒扁桃体发炎了,做了雾化,今天回家晚他已经睡着,外婆说他发烧了,让我给测下体温:39.8腋下。外婆紧张...
    我是KK唷阅读 323评论 1 0