import UIKit
import Foundation
import RxCocoa
extension UIView {
//MARK:创建View
class func createViewWith(frame:CGRect) -> UIView {
let view = UIView(frame: frame)
return view
}
//MARK: - 手势扩展
func addTapAction(target: Any, action: Selector) {
self.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: target, action: action)
self.addGestureRecognizer(tap)
}
func addTapAction() -> Driver<UITapGestureRecognizer> {
self.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer()
self.addGestureRecognizer(tap)
return tap.rx.event.asDriver()
}
func addSwipeAction(direction: UISwipeGestureRecognizer.Direction = .up) -> Driver<UISwipeGestureRecognizer> {
self.isUserInteractionEnabled = true
let recognizer = UISwipeGestureRecognizer()
recognizer.direction = direction
addGestureRecognizer(recognizer)
return recognizer.rx.event.asDriver()
}
}
extension UILabel {
//MARK:创建Label
class func createLabelWith(frame:CGRect, Text:String, Font:CGFloat, TextColor:UIColor) -> UILabel {
let label = UILabel(frame: frame)
label.text = Text
label.textColor = TextColor
label.font = PublicFunction.suitFont(fontSize: Font)
return label
}
//MARK:创建下划线Label
class func createUnderlineLabelWith(frame:CGRect, Text:String, Font:CGFloat, TextColor:UIColor) -> UILabel {
let label = UILabel(frame: frame)
label.textColor = TextColor
label.font = PublicFunction.suitFont(fontSize: Font)
//下划线
let attributedString = NSMutableAttributedString(string: Text)
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: NSRange(location: 0, length: attributedString.length))
label.attributedText = attributedString
label.adjustsFontSizeToFitWidth = true
return label
}
}
extension UIImageView {
//MARK:创建imageView
class func createImageViewWith(frame:CGRect, ImageName:String) -> UIImageView {
let imageView = UIImageView(frame: frame)
if !ImageName.isEmpty {
imageView.image = UIImage(named:ImageName)
}
return imageView
}
}
extension UIButton {
//MARK:创建button
class func createButtonWith(frame:CGRect, ImageName:String, bgImageName:String, Text:String, Font:CGFloat, TextColor:UIColor) -> UIButton {
let button = UIButton(type:.custom)
button.frame = frame
if !Text.isEmpty {
button.setTitle(Text, for:.normal)
button.titleLabel?.font = PublicFunction.suitFont(fontSize: Font)
button.setTitleColor(TextColor, for: .normal)
}
if !ImageName.isEmpty {
button.setImage(UIImage(named:ImageName), for:.normal)
}
if !bgImageName.isEmpty {
button.setBackgroundImage(UIImage(named:bgImageName), for:.normal)
}
return button
}
}
extension UITextField {
//MARK:创建UITextField
class func createTextFieldWith(frame:CGRect, passWord:Bool, Font:CGFloat, placeholder:String) -> UITextField {
let textField = UITextField(frame: frame)
//字体大小
textField.font = PublicFunction.suitFont(fontSize: Font)
textField.textColor = PublicFunction.generateDynamicColor(lightColor: UIColor.white, darkColor: UIColor.black)
//灰色提示
textField.placeholder = placeholder
//文字对齐方式
textField.textAlignment = .left
//是否加密
textField.isSecureTextEntry = passWord
//边框
textField.borderStyle = .roundedRect
//键盘类型
textField.keyboardType = .emailAddress
//关闭首字母大写
textField.autocapitalizationType = .none
//清除按钮
textField.clearButtonMode = .always
//编辑状态下一直存在
textField.rightViewMode = .whileEditing
return textField
}
}
extension UISlider {
//MARK:创建Slider
class func createSliderWith(frame:CGRect, minValue:Float, maxValue:Float, currentValue:Float, leftColor:UIColor, rightColor:UIColor, continuous:Bool) -> UISlider {
let slider:UISlider = UISlider(frame: frame)
slider.minimumValue = minValue //最小值
slider.maximumValue = maxValue //最大值
slider.value = currentValue //当前值
slider.minimumTrackTintColor = leftColor //右边槽的颜色
slider.maximumTrackTintColor = rightColor //左边槽的颜色
slider.isContinuous = continuous //滑块值改变响应
return slider
}
}
extension UIProgressView {
//MARK:创建ProgressView
class func createProgressViewWith(frame:CGRect, currentValue:Float, currentColor:UIColor, bgColor:UIColor) -> UIProgressView {
let progressView:UIProgressView = UIProgressView(progressViewStyle: .default)
progressView.frame = frame
progressView.trackTintColor = bgColor //背景色
progressView.progressTintColor = currentColor //进度条颜色
progressView.progress = currentValue
return progressView
}
}
//标题
let leftLabel = UILabel.createLabelWith(frame: CGRect.zero, Text: "登录", Font: 14, TextColor: UIColor.blue)
//icon
let iconImageView = UIImageView.createImageViewWith(frame: CGRect.zero, ImageName: "logo")
//输入框
let accountField = UITextField.createLoginTextFieldWith(frame: CGRect.zero, passWord: false, Font: 18, placeholder: "输入手机号", placeholderFont: 14)
//登录按钮
let loginButton = UIButton.createButtonWith(frame: CGRect.zero, ImageName: "", bgImageName: "按钮背景图片", Text: "登录", Font: 12, TextColor: UIColor.white)
//下载进度
let progressView = UIProgressView.createProgressViewWith(frame: CGRect.zero, currentValue: 0, currentColor: UIColor.black, bgColor: UIColor.gray)
//播放进度
let customSlider = UISlider.createSliderWith(frame: CGRect.zero, minValue: 0, maxValue: 100, currentValue: 0, leftColor: UIColor.black, rightColor: UIColor.gray, continuous: true)
网友评论