Swift-Utils
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
需求
Swift-Utils 需要 iOS 10+ 并且与 Swift 4.2 项目兼容。
使用
Swift Utils 可以用来简化各种任务。实际上,它们是对您的 UIView、UIViewController、NSObject 等的扩展。
UIView 扩展
绘制具有圆角阴影
view.shadowWithRoundCorner(cornerRadius: 10)
从特定角落为UIView创建圆角
view.roundCorners([UIRectCorner.bottomLeft, UIRectCorner.topLeft], radius: 10)
为UIView添加虚线边框
view.dashedBorder(borderColor: .black, lineDashPattern: [2,2])
将任何视图作为弹出窗口添加到控制器中
You can alter the animation directiona and position of the pop up.
// If you pass nil in controller, pop up will be added to UIWindow, else into the controller
view.addIn(nil, addFrom: .top, position: .center)
获取UIView的父控制器
let parentController = view.parentContainerViewController()
从UIView获取顶级控制器
self.view.topControllerInHierarchy()
将您的UIView转换为模糊视图
myView.addBlurView(style: UIBlurEffect.Style.light)
向任意UIView组件添加/移除loader
view.lock()
button.lock()
view.unlock()
button.unlock()
您可以通过Appearance类或通过在lock()函数中传递参数来自定义loader。例如,要使用Appearance类更改loader样式,只需在AppDelegate中添加以下几行代码
ASLoader.appearance().tintColor = .red
ASLoader.appearance().textColor = .white
ASLoader.appearance().font = UIFont.systemFont(ofSize: 12.0)
ASLoader.appearance().size = CGSize(width: 40, height: 40)
通过传递参数来自定义loader。您不需要传递所有参数,只需传递需要自定义的参数即可。
self.view.lock(text: "loading",
tintColor: .red, textColor: .white,
font: UIFont.systemFont(ofSize: 16),
centerImage: UIImage(named: "logo"),
size: CGSize(width: 30, height: 30))
显示toast
self.showToast(message: "Success")
您可以通过Appearance类或通过传递参数来自定义toast。
ASToast.appearance().toastBackgroundColor = .black
ASToast.appearance().textColor = .white
self.showToast(message: "Success",
backgroundColor: .black, textColor: .white,
font: UIFont.systemFont(ofSize: 16))
可自定义视图
只需在故事板中添加一个视图并分配类 "ASCustomizableView"。当你想要带有阴影的圆角时,这是最佳选择。然后你可以从属性检查器中更改某些属性,例如
showShadow // If true shadow will be drawn. If false, you can changes attributes like corner radius, border width and color etc.
borderColor
borderWidth
shadowRadius
shadowOpacity
cornerRadius
shadowColor
shadowOffset
字符串扩展
这些扩展可以帮助您验证字符串,用于有效邮箱、文本、电话号码等。
let myString = "[email protected]"
if myString.isBlank {
print("success")
}
if myString.isEmail {
print("success")
}
if myString.isAlphanumeric {
print("success")
}
if myString.isValidPassword {
print("success")
}
if myString.isPhoneNumber {
print("success")
}
UIColor 扩展
从 hexString 或 hex intvalue 生成 UIColor
let colorFromString = UIColor.hexColor("FF0000")
let colorFromInt = UIColor(rgb: 0xFF0000)
UIDate Extensions
Date Extensions 用于比较日期、从日期获取字符串格式或获取时间戳等。
let date1 = Date()
let date2 = Date()
if date1.isGreaterThanDate(date: date2) {
}
if date1.isLessThanDate(date: date2) {
}
if date1.isEqualToDate(date: date2) {
}
let stringDate = date1.toDateString()
let stringDate2 = date1.toDateString(format: "yyyy-MM-dd", timezone: TimeZone(identifier: "UTC"))
// This method will return the time span between the date1 and current time. This is an ease for
//showing timestamp in applications like social timeline or chatting applications.
let time = date1.timeAgoValue() // returns "1 min Ago"
UITableView Extensions
轻松处理空表格视图
let tableView = UITableView()
// Add no Data text for empty table
tableView.handleEmptyTable(text: "No Data")
// Remove no Data text
tableView.handleEmptyTable(text: nil)
// Get index of an view inside a UITableViewCell
let indexPath = tableView.indexPath(for: sender) // Sender can be anything. UIView/UIButton/UILabel etc
UITableViewCell Extensions
从 UITableViewCell 获取 UITableView。当您想在 UITableViewCell 类内部获取 UIViewController 的引用时,此功能非常有用。
let tableView = cell.tableView
UIViewController 扩展
使用这些简单的扩展轻松处理导航返回按钮
/// This hides the backButton title for the next controller
self.hideBackButtonTitle()
// This removes the back button from the navigation bar
self.removeBackButton()
/// You can set a custom back button using this extension
self.setCustomBackButton(image: UIImage(named: "back"))
// Show an alert from a controller
self.showAlert(title: "Alert", message: "Success", style: .alert)
UITextfield 扩展
更改 UITextfield 占位符颜色
textField.setPlaceholder(text: question.placeholder, color: .gray)
安装
Swift-Utils 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
pod 'Swift-Utils'
作者
amrit42087, [email protected]
许可
Swift-Utils遵循MIT许可。有关更多信息,请参阅LICENSE文件。