YZLibraries
功能
- YZAppConfig 类用于配置
设计宽度,设计高度,导航栏高度
。 - YZCALayer 是一种扩展的 CALayer,用于添加或应用
应用阴影,添加圆角,添加边框,添加渐变
。 - YZUIView 是一种扩展的 UIView,用于添加或应用
添加圆角,应用阴影,添加渐变,添加约束,添加边框
。
需求
- iOS 12.0+
- Xcode 11+
- Swift 5.0+
使用
1. YZAppConfig 类
YZAppConfig
用于设置设计师提供的 width, height, navigationBarHeight
设计。通过配置设计设置,将计算 widthRatio, heightRatio, safeAreaInsets
初始化
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
- 通过计算width/designWidth
提供宽度比例。YZAppConfig.heightRatio
- 通过计算height/designHeight
提供高度比例。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
YZUIView
是 UIView
的扩展,可以添加 addCornerRadius
、applyShadow
、addGradient
、addConstraintWithSuperView
、addBorder
、addDashBorder
功能。它会在内部调用 YZCALayer
函数来按照需求设计 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
类型值转换为toFloat
类型值。toInt
- 用于将String
类型值转换为toInt
类型值。toInt64
- 用于将String
类型值转换为toInt64
类型值。toInt32
- 用于将String
类型值转换为toInt32
类型值。toInt16
- 用于将String
类型值转换为toInt16
类型值。toInt8
- 用于将String
类型值转换为toInt8
类型值。camelCaseLetter
- 用于显示首字母大写的字符串,它将返回String
类型的值。htmlToAttributedString
- 用于将 HTML 转换为富文本字符串。htmlToString
- 用于将 HTML 转换为普通字符串。initialCharactersFromWord
- 用于获取用于显示为用户头像的单词字符。isPhoneNumberValid
- 用于检查电话号码是否有效。它将返回Bool
类型的值。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
类及其扩展,提供了以下类和函数。
类
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
类和 UITextView
的扩展,提供了以下类、属性、协议和函数。
类
YZParentWidthTV
- 当您想要根据多设备支持设计UITextView
并应用widthRatio
时使用。YZParentHeightTV
- 当您想要根据多设备支持设计UITextView
并应用heightRatio
时使用。YZWithoutInsetTV
- 当您想要设计不带任何额外内边距或填充的UITextView
时使用。YZLinkableTV
- 当您想要获取UIDataDetectorTypes
的值时使用。
协议
YZLinkableTVDelegate
- 当您想要获取UIDataDetectorTypes
的值时使用。
函数
public func newHeightUsing(_ baseHeight: CGFloat)
- 计算新文本视图(基于内容)的高度是否大于基础高度。
11. YZTextFields
YZTextFields
文件包含多个 UITextField
类和 UITextField
扩展,提供以下类和函数。
类
YZParentWidthTF
- 当您想根据多种设备支持设计UITextField
并应用widthRatio
时使用。YZParentHeightTF
- 当您想根据多种设备支持设计UITextField
并应用heightRatio
时使用。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)
- 用于从给定的十六进制值和 alpha 值初始化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)
- 用于从给定字符串生成二维码图像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
- 它将提供值来检查是否正在动画。 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
用于检测给定字符串中提及、哈希标签或URL的YZLinkLabel类,提供以下属性和函数。
属性
arrOfHashTags
- 用于获取哈希标签的数量。arrOfMentions
- 用于获取提及标签的数量。arrOfURLs
- 用于获取URL的数量。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(百万)形式的字符串。
函数
- :
public mutating func roundTo(_ places: Int)
- 用于将Double
值四舍五入到指定的小数位。
安装
YZLibraries 通过 CocoaPods 提供。要安装它,只需在你的 Podfile 中添加以下行
pod 'YZLibraries', '~> 4.2'
作者
维普尔·帕特尔(Yudiz Solutions Pvt. Ltd.), [email protected]
许可证
YZLibraries 可在 MIT 许可下使用。有关更多信息,请查看 LICENSE 文件。