本文主要是介绍[Swift]UITextField在左侧添加内边距或图标,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
self.field_main.addLeftTextPadding(10)
import UIKitextension UITextField {/// 添加左内边距public func addLeftTextPadding(_ blankSize: CGFloat) {let leftView = UIView()leftView.frame = CGRect(x: 0, y: 0, width: blankSize, height: frame.height)self.leftView = leftViewself.leftViewMode = UITextFieldViewMode.always}/// 在文本框的左边添加一个图标public func addLeftIcon(_ image: UIImage?, frame: CGRect, imageSize: CGSize) {let leftView = UIView()leftView.frame = framelet imgView = UIImageView()imgView.frame = CGRect(x: frame.width - 8 - imageSize.width, y: (frame.height - imageSize.height) / 2, w: imageSize.width, h: imageSize.height)imgView.image = imageleftView.addSubview(imgView)self.leftView = leftViewself.leftViewMode = UITextFieldViewMode.always}}
这篇关于[Swift]UITextField在左侧添加内边距或图标的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!