YZClass
功能
- YZAppConfig 类用于配置
designWidth, designHeight, navigationBarHeight
。 - YZCALayer 是 CALayer 的扩展类型,用于添加或应用
applyShadow, addCornerRadius, addBorder, addGradient
。 - YZUIView 是 UIView 的扩展类型,用于添加或应用
addCornerRadius, applyShadow, addGradient, addConstraint, addBorder
要求
- iOS 12.0+
- Xcode 11+
- Swift 5.0+
使用
1. YZAppConfig类
YZAppConfig
用于设置设计师提供的宽度、高度、导航栏高度
。通过配置设计设置,将计算宽度比率、高度比率、安全区间距
。
初始化
YZAppConfig.initialise(375, designHeight: 667, navigationBarHeight: 88)
属性
YZAppConfig.appDelegate
- 使用UIApplication.shared.delegate!
提供UIApplicationDeletage
对象。YZAppConfig.navigationBarHeight
- 提供设置的导航栏高度。YZAppConfig.designWidth
- 提供设置的设计宽度。YZAppConfig.designHeight
- 提供设置的设计高度。YZAppConfig.width
- 通过UIScreen.main.bounds.size.width
提供当前运行iOS设备/模拟器的宽度。YZAppConfig.height
- 通过UIScreen.main.bounds.size.height
提供当前运行iOS设备/模拟器的高度。YZAppConfig.widthRatio
- 通过计算宽度/设计宽度
提供宽度比率。YZAppConfig.heightRatio
- 通过计算高度/设计高度
提供高度比率。YZAppConfig.safeAreaInsets
- 通过appDelegate.window!!.safeAreaInsets
提供UIEdgeInsets
对象。YZAppConfig.safeAreaInsets
- 通过appDelegate.window!!.safeAreaInsets
提供UIEdgeInsets
对象。
2. YZCALayer
YZCALayer
是对CALayer
的扩展,增加了addCornerRadius
、applyShadow
、addGradient
、addConstraintWithSuperView
、addBorder
、addDashBorder
等功能。
函数
public func addCornerRadiusBy(_ roundingCorners: UIRectCorner = [.allCorners], cornerRadii: CGSize = .zero, fillColor: UIColor? = .black)
- 使用CAShapeLayer
添加圆角,带有特定的参数。public func applyShadowWith(_ roundingCorners: UIRectCorner = [.allCorners], cornerRadii: CGSize = .zero, fillColor: UIColor? = nil, shadowOffset: CGSize = .zero, shadowColor: UIColor, shadowRadius: CGFloat = 3, shadowOpacity:Float = 0)
- 使用CAShapeLayer
绘制阴影,带有特定的参数。public func addBorderBy(_ roundingCorners: UIRectCorner = [.allCorners], cornerRadii: CGSize = .zero, fillColor: UIColor? = .black, strokeColor: UIColor? = nil, lineWidth: CGFloat = 1)
- 使用CAShapeLayer
绘制边框,带有特定的参数。public func addDashBorderBy(_ cornerRadius: CGFloat = .leastNonzeroMagnitude, fillColor: UIColor? = .black, dashColor: UIColor? = nil, dashHeight: CGFloat = 1, dashWidth: CGFloat = 5, spaceBetweenDash: CGFloat = 5)
- 使用CAShapeLayer
绘制虚线边框,带有特定的参数。public func addGradient(_ colors: [UIColor], gradientPoint: YZGradientPoint, roundingCorners: UIRectCorner = [.allCorners], cornerRadii: CGSize = .zero)
- 使用CAGradientLayer
添加渐变颜色,带有特定的参数。
3. YZUIView
XYZUIView
是对 UIView
的扩展,增加了 addCornerRadius
、applyShadow
、addGradient
、addConstraintWithSuperView
、addBorder
、addDashBorder
方法。它将内部调用 XYZCALayer
的函数来设计满足需求的 UIView
。
属性
isRound
- 使用@IBInspectable isRound
属性可以在选定的UIView
上绘制全圆角。borderWidth
- 使用@IBInspectable borderWidth
属性可以在选定的UIView
上绘制边界。borderColor
- 使用@IBInspectable borderColor
属性可以在选定的UIView
上应用边界颜色。cornerRadius
- 使用@IBInspectable cornerRadius
属性可以在选定的UIView
上应用圆角。shadowRadius
- 使用@IBInspectable shadowRadius
属性可以在选定的UIView
上定义阴影半径。shadowOpacity
- 使用@IBInspectable shadowOpacity
属性可以在选定的UIView
上定义阴影不透明度。shadowColor
- 使用@IBInspectable shadowColor
属性可以在选定的UIView
上定义阴影颜色。shadowOffset
- 使用@IBInspectable shadowOffset
属性可以在选定的UIView
上定义阴影偏移量。zPosition
- 使用@IBInspectable zPosition
属性可以定义zPosition
。
函数
public func addCornerRadiusBy(_ roundingCorners: UIRectCorner = [.allCorners], cornerRadii: CGSize = .zero, fillColor: UIColor? = .black)
- 它可以用特定的参数在UIView
附近添加圆角。public func applyShadowWith(_ roundingCorners: UIRectCorner = [.allCorners], cornerRadii: CGSize = .zero, fillColor: UIColor? = nil, shadowOffset: CGSize = .zero, shadowColor: UIColor, shadowRadius: CGFloat = 3, shadowOpacity:Float = 0)
- 该方法用于使用特定参数绘制围绕UIView
的阴影。public func addConstraintWithSuperView(_ topConstraint: CGFloat, leadingConstraint: CGFloat, bottomConstraint: CGFloat, trailingConstraint: CGFloat)
- 该方法用于在父视图中添加约束。public func addDashBorderBy(_ cornerRadius: CGFloat = .leastNonzeroMagnitude, fillColor: UIColor? = .black, dashColor: UIColor? = nil, dashHeight: CGFloat = 1, dashWidth: CGFloat = 5, spaceBetweenDash: CGFloat = 5)
- 使用CAShapeLayer
绘制虚线边框,带有特定的参数。public func addBorderBy(_ roundingCorners: UIRectCorner = [.allCorners], cornerRadii: CGSize = .zero, fillColor: UIColor? = .black, strokeColor: UIColor? = nil, lineWidth: CGFloat = 1)
- 该方法用于使用特定参数绘制围绕UIView
的边框。public func addDashBorderBy(_ cornerRadius: CGFloat = .leastNonzeroMagnitude, fillColor: UIColor? = .black, dashColor: UIColor? = nil, dashHeight: CGFloat = 1, dashWidth: CGFloat = 5, spaceBetweenDash: CGFloat = 5)
- 该方法用于使用特定参数绘制围绕UIView
的虚线边框。public func addGradient(_ colors: [UIColor], gradientPoint: YZGradientPoint, roundingCorners: UIRectCorner = [.allCorners], cornerRadii: CGSize = .zero)
- 该方法用于在UIView
内添加渐变色,并带有特定参数。
4. YZArray
YZArray
是对集合数据类型 Array
的扩展,用于执行类似 uniqueElements
或 removeElements
的操作。
属性
uniqueElements
- 将提供唯一的元素数组。
函数
public mutating func uniqueElementsInPlace()
- 使用于在不生成另一个数组副本的情况下制作唯一元素数组类型。public mutating func remove(_ elements: [Element])
- 使用于从现有数组中移除一个或多个提供的元素。
5. YZURL
YZURL
是 URL 的扩展,并提供以下属性和功能。
属性
isFileExists
- 它将检查给定 URL 的文件是否存在。convertToData
- 它将从给定的文件 URL 生成数据对象。
函数
public func deleteFile()
- 它将检查文件是否存在,并从给定 URL 中删除。public func getThumbnailFromVideo()
- 它用于从给定的视频文件 URL 获取缩略图图像。public func compressVideo(_ outputURL: URL, handler:@escaping (_ exportSession: AVAssetExportSession?, _ compressVideoURL: URL?)-> Void)
- 它用于压缩给定 URL 的视频。public func getValueForQuery(_ parameter: String)
- 它用于从给定的查询参数获取值。
6. YZDictionary
YZDictionary
是 Dictionary
和 NSDictionary
的扩展,提供了以下属性和方法。
属性
toJSON
- 用于将Dictionary
转换为 JSON 字符串。convertToData
- 它将从给定的文件 URL 生成数据对象。
函数
public mutating func merge(_ other: Dictionary)
- 将提供的字典与现有字典合并。public func getDoubleValue(forKey: String)
- 从给定的NSDictionary
中提供Double
类型的值。public func getFloatValue(forKey: String)
- 从给定的NSDictionary
中提供Float
类型的值。public func getUIntValue(forKey: String)
- 从给定的NSDictionary
中提供UInt
类型的值。public func getIntValue(forKey: String)
- 从给定的NSDictionary
中提供Int
类型的值。public func getInt64Value(forKey: String)
- 从给定的NSDictionary
中提供Int64
类型的值。public func getInt32Value(forKey: String)
- 从给定的NSDictionary
中提供Int32
类型的值。public func getInt16Value(forKey: String)
- 从给定的NSDictionary
中提供Int16
类型的值。public func getInt8Value(forKey: String)
- 从给定的NSDictionary
中提供Int8
类型的值。public func getStringValue(forKey: String)
- 从给定的NSDictionary
中提供String
类型的值。public func getBooleanValue(forKey: String)
- 从给定的NSDictionary
中提供Bool
类型的值。
7. YZString
YZString
文件用于使用String
、NSMutableAttributedString
和NSAttributedString
扩展执行一些字符串操作,并提供了以下属性和函数。
属性
isEmailAddressValid
- 用于检查电子邮件地址是否有效,它将返回一个Bool
值。trimmedString
- 用于去除字符串中的空白符和新行,它将返回一个String
值。isOnlyNumericValue
- 用于检查给定的字符串是否为数值,它将返回一个Bool
值。toDouble
- 用于将String
类型的值转换为Double
类型的值。toFloat
- 用于将String
类型的值转换为Float
类型的值。toInt
- 用于将String
类型的值转换为Int
类型的值。toInt64
- 用于将String
类型的值转换为Int64
类型的值。toInt32
- 用于将String
类型的值转换为Int32
类型的值。toInt16
- 用于将String
类型的值转换为Int16
类型的值。toInt8
- 用于将String
类型的值转换为Int8
类型的值。camelCaseLetter
- 用于显示带有驼峰命名的字符串,它将返回一个String
类型的值。htmlToAttributedString
- 用于将HTML转换为Attributed String。htmlToString
- 用于将HTML转换为String。initialCharactersFromWord
- 用于获取单词的字符以显示为用户头像。isPhoneNumberValid
- 用于检查电话号码是否有效。它将返回一个布尔值。adjustedNationalNumber
- 在必要时添加前导零以调整国家号码以供显示。用于基本格式化功能。
函数
public func contains(_ find: String)
- 用于检查给定的字符串是否包含在内。public func isEqual(_ otherString: String)
- 用于比较两个字符串。public func singleLineHeightFor(_ font: UIFont)
- 根据提供的字体计算给定字符串的单行高度。public func heightForFixed(_ width: CGFloat, font: UIFont)
- 根据固定的宽度计算给定字符串的高度。public func widthForFixed(_ height: CGFloat, font: UIFont)
- 根据固定的长度计算给定字符串的宽度。public func capitalizingFirstLetter()
- 用于将给定字符串或单词的首字母大写。public func strikeThroughLine(_ color: UIColor, textFont: UIFont, foregroundColor: UIColor)
- 用于在给定字符串上绘制删除线。它将调用内部的NSMutableAttributedString
函数。public func addVertical(_ lineSpacing: CGFloat, alignment: NSTextAlignment, lineBreakMode: NSLineBreakMode = .byTruncatingTail, textFont: UIFont, foregroundColor: UIColor)
- 用于按给定参数在给定字符串上添加垂直间距。它将调用内部的NSMutableAttributedString
函数。public func addCharacters(_ spacing: CGFloat, alignment: NSTextAlignment = .natural, lineBreakMode: NSLineBreakMode = .byTruncatingTail, textFont: UIFont, foregroundColor: UIColor)
- 用于按给定参数在给定字符之间添加间距。它将调用内部的NSMutableAttributedString
函数。public func setString(_ alignment: NSTextAlignment = .natural, textFont: UIFont? = nil, foregroundColor: UIColor? = nil)
- 用于根据给定参数设置字符串对齐、字体和前景色。它将调用内部的NSMutableAttributedString
函数。public func addCharacters(_ shadowColor: UIColor, shadowBlurRadius: CGFloat, shadowOffset: CGSize = .zero, textFont: UIFont, foregroundColor: UIColor)
- 将用于为字符应用阴影效果。
8. YZButtons
YZButtons
文件包含多个 UIButton
类和 UIButton
的扩展,提供了以下类和函数。
类
YZParentWidthBTN
- 当您想根据多个设备支持来设计UIButton
并应用widthRatio
时使用。YZParentHeightBTN
- 当您想根据多个设备支持来设计UIButton
并应用heightRatio
时使用。
函数
public func setAttributed(_ texts: [String], attributes: [[NSAttributedString.Key : Any]], state: UIControl.State)
- 用于设置文本及其属性。它将内部调用NSAttributedString
函数。
9. YZLabels
YZLabels
文件包含多个 UILabel
类和 UILabel
扩展,提供以下类、属性和函数。
类
YZParentWidthLBL
- 当需要根据多设备支持设计UILabel
并应用widthRatio
时使用。YZParentHeightLBL
- 当需要根据多设备支持设计UILabel
并应用heightRatio
时使用。
属性
isTruncated
- 用以检查给定的标签是否已截断。它将返回Bool
类型的值。
函数
public func addCharacters(_ spacing: CGFloat, alignment: NSTextAlignment = .natural, lineBreakMode: NSLineBreakMode = .byTruncatingTail, textFont: UIFont, foregroundColor: UIColor)
- 用以根据给定的参数添加字符之间的空间。它将内部调用String
函数。public func setAttributed(_ texts: [String], attributes: [[NSAttributedString.Key : AnyObject]])
- 用以添加文本及其属性。public func setVertical(_ lineSpacing: CGFloat, alignment: NSTextAlignment, lineBreakMode: NSLineBreakMode = .byTruncatingTail, textFont: UIFont, foregroundColor: UIColor)
- 用以根据给定的参数在给定的文本上添加垂直行距。它将内部调用String
函数。public func addStrikeThroughLine(_ color: UIColor, textFont: UIFont, foregroundColor: UIColor)
- 用以在给定的文本上绘制删除线。它将内部调用String
函数。
10. YZTextViews
YZTextViews
文件包含多个 UITextView
类及其扩展,提供了以下类、属性、协议和函数。
类
YZParentWidthTV
- 当您想将UITextView
设计为支持多设备并应用widthRatio
时使用。YZParentHeightTV
- 当您想将UITextView
设计为支持多设备并应用heightRatio
时使用。YZWithoutInsetTV
- 当您想设计无需任何额外内边距或填充的UITextView
时使用。YZLinkableTV
- 当您想获取UIDataDetectorTypes
的值时使用。
协议
YZLinkableTVDelegate
- 当您想获取UIDataDetectorTypes
的值时使用。
函数
public func newHeightUsing(_ baseHeight: CGFloat)
- 计算新的文本视图高度(基于内容)是否大于基本高度。
11. YZTextFields
YZTextFields
文件包含多个 UITextField
类和 UITextField
扩展,提供了以下类和函数。
类
YZParentWidthTF
- 当需要根据多设备支持和应用widthRatio
设计UITextField
时使用。YZParentHeightTF
- 当需要根据多设备支持和应用heightRatio
设计UITextField
时使用。YZParentPaddingTF
- 用于设置 TextRect、PlaceHolderRect 和 EditingRect 的填充,使用UIEdgeInsets
。
函数
public func addInputAccessory(_ text: String, textFont: UIFont, tintColor: UIColor? = nil, rect: CGRect = .zero)
- 用于向UITextField
添加输入辅助视图。public func fixCaretPosition()
- 用于通过移除尾随空格将光标移动到正确的位置。public func setAttributed(_ placeHolder: String?, color: UIColor, font: UIFont)
- 用于根据给定参数设置带有属性的占位符。
12. YZColor
YZColor
文件包含 UIColor
扩展,提供了以下函数。
函数
public convenience init(_ hexaValue: String, alpha: CGFloat = 1.0)
- 用于从给定的十六进制值和透明度值初始化UIColor
对象。public static func initWith(_ red: Int, green: Int, blue: Int, alpha: CGFloat)
- 用于从给定的 RGBA 值初始化UIColor
对象。public func brightenedBy(_ factor: CGFloat)
- 用于为现有颜色添加亮度效果。
13. YZImage
YZImage
文件包含 UIImage
扩展,提供了以下属性和函数。
属性
fixedOrientation
- 用于固定给定图像的方向。
函数
public static func generateQRCodeFrom(_ string: String)
- 用于从给定字符串生成 QRCode 图像。public static func getSquareImage()
- 用于生成方形图像。public func resizeImageWith(_ width: CGFloat, height: CGFloat)
- 用于根据给定参数调整图像大小。public func scaleTo(_ newSize: CGSize, isAspectFill: Bool = true)
- 用于将图像缩放到适合传入尺寸的边界内,并保持纵横比。public func scaleAndManageAspectRatio(_ width: CGFloat)
- 用于降低图像分辨率并保持纵横比。
14. YZIndexPath
YZIndexPath
文件包含一个IndexPath
扩展,提供了函数。
函数
public static func indexPathForCellContaining(_ view: UIView, tableView: UITableView)
- 用于从UITableView
获取IndexPath
。public static func indexPathForCellContaining(_ view: UIView, collectionView: UICollectionView)
- 用于从UICollectionView
获取IndexPath
。
15. YZViewController
YZViewController
文件包含对 UIWindow
、UIViewController
和 UIApplication
的多个扩展,提供了以下属性和方法。
属性
currentVisibleViewController
- 用于获取UIWindow
、UIViewController
和UIApplication
的当前可见的UIViewController
。
函数
public static func getCurrentVisibleVCFor(_ vc: UIViewController)
- 用于获取给定UIViewController
的当前可见的UIViewController
。
16. YZActivityIndicator
用于使用图像初始化自定义指示器视图的 YZActivityIndicator
类,具有以下属性和功能。
属性
isAnimating
- 提供一个Bool
值以检查isAnimating
或否。hidesWhenStopped
- 设置当动画停止时隐藏指示器视图的属性。
函数
public func startAnimating()
- 用于开始动画。public func stopAnimating()
- 用于停止动画。
17. YZConstrainedViews
YZConstrainedViews
文件包含多个用于设置应用程序父类的类,以下为提供的相关类。
协议
YZUserTapDelegate
- 用来获取单元格、集合单元格和表头/表尾视图文本上的用户点击事件。YZTextFieldDelegate
- 用来获取UITextField
的事件。
类
YZParentControl
- 是UIControl
的子类,用作父类。YZParentCVC
- 是UICollectionViewCell
的子类,用作父类。YZParentCRV
- 是UICollectionReusableView
的子类,用作父类。YZParentHFV
- 是UITableViewHeaderFooterView
的子类,用作父类。YZParentTVC
- 是UITableViewCell
的子类,用作父类。YZParentView
- 是UIView
的子类,用作父类。
17. YZLinkLabel
YZLinkLabel
类用于检测给定字符串中的提及、哈希标签或网址,并提供了以下属性和功能。
属性
arrOfHashTags
- 用于获取哈希标签的数量。arrOfMentions
- 用于获取提及标签的数量。arrOfURLs
- 用于获取网址的数量。delegate
- 用于设置代理以获取事件。
函数
- public func set(_ attributedString: NSAttributedString, linebreak : NSLineBreakMode = .byTruncatingTail) - 用于设置属性的文本。
18. YZPermission
YZPermission
类用于检查相机访问、照片库访问等权限,具有以下功能。
函数
public func cameraAccess(permissionWithStatus block: @escaping PermissionStatus)
- 用于检查相机访问权限。public func photoLibraryAccess(permissionWithStatus block: @escaping PermissionStatus)
- 用于检查照片库访问权限。public func locationAccess(permissionWithStatus block: @escaping PermissionStatus)
- 用于检查位置访问权限。
19. YZNumbers
YZNumbers
类用于处理一些数值相关的操作,以下展示了其属性和方法。
属性
suffixNumber
- 此方法用于将Double
值转换为 K(千)和 M(百万)形式的String
。
函数
public mutating func roundTo(_ places: Int)
- 此方法用于将Double
值四舍五入到指定的小数位数。
安装
YZClass可以通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile中
pod 'YZClass', '~> 0.1.0'
作者
Vipul Patel (Yudiz Solutions Pvt. Ltd.), [email protected]
许可证
YZClass处于MIT许可证下。有关更多信息,请参阅LICENSE文件。