UsefulExtensions 0.2.2

UsefulExtensions 0.2.2

测试已测试
Lang语言 SwiftSwift
许可 MIT
Released最后发布2017年12月
SwiftSwift 版本4.0
SPM支持 SPM

AndreaBellotto 维护。



  • Andrea Bellotto

UsefulExtensions

这个库通过扩展标准类和类型,为您提供一些有用的变量和函数。

扩展的类型

Locale

  • 从简单的静态变量获取 Locale
Locale.italy // returns Locale with string "it-IT"
Locale.usa // returns Locale with string "en-US"

Double

  • 四舍五入到小数位数
2.123131.round(fractionDigits:2) // output: 2.12
  • 返回从 double 格式化的字符串,包括区域设置和样式
let price:Double = 100.00
print(price.format(with: .italy, and: .currencyISOCode)) // prints 100.00 EUR

String

  • 返回本地化字符串
"alert_message".localized // output: whatever you have in your .strings file at alert_message string
  • 返回不带无用的空白字符的字符串
" hello world".trimmed // output: hello world

UIApplication

  • 返回活动的 ViewController
UIApplication.toppestViewController() 

UIColor

  • 从 RGB 获取 UIColor 的更可读的函数
UIColor(r:200, g:200, b:200) 

UINavigationItem

  • 创建一个可取消的左侧按钮
self.navigationItem.addDismissLeftButton(target: self, selector: #selector(...), image:UIImage(named:"asd"), scale: 10) 

UITableView

  • 检查 TableView 是否滚动到顶部
self.tableView.isScrolledToBottom // output: true or false
  • 将 tableView 滚动到顶部
self.tableView.scrollToBottom(animated:true)
  • 以更优雅的方式从 xib 注册 uitableview
self.tableView.register(CustomTableViewCell.self)

UITextField

  • 更改占位符 UIColor(也可直接在 storyboard 或 xib 中修改)
textField.placeholderColor = UIColor.red

UIViewController

  • 从 UIViewController 类获取字符串名
TestViewController.nameIdentifier // output: "TestViewController"
  • 如果想在 NavigationController 中嵌入,请呈现 ViewController
self.present(viewControllerToPresent: vc, animated: true, embedInNavigationController: true, completion: nil)

UIView

  • 从 UIView 类获取字符串名
MyCustomView.nameIdentifier // output: "MyCustomView"
  • 从类中直接获取 UIView 子类从它的 nib
MyCustomView.loadViewFromNib()
  • 服务于 IBDesignable nib 类的函数。

如果在 IBDesignable 类的 initWithCoderinitWithFrame 函数中调用 setup(),您可以在 storyboard 中看到您自定义的 UIView


@IBDesignable

class CustomLabel: UIView {

    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)!
        setup()
    }
}

  • 使用动画更新您的约束
myView.change(topConstraint, constantWith:20.0, andAnimating:true)
  • 渐显您的 UIView
myView.fadeIn(withAnimation:true)
  • 渐隐您的 UIView
myView.fadeOut(withAnimation:true)
  • 一般渐显您的 UIView
myView.fade(withAnimation:true)
  • 获取顶部约束数组
let constraints:[NSLayoutConstraint] = myView.topConstraints
  • 有用的 IBInspectable 变量
myView.cornerRadius = 5.0
myView.borderWidth = 2.0
myView.borderColor = UIColor.blue

MKMapView

  • 将地图中心定位在指定位置,并设置所需半径
mapView.centerMapOnLocation(location, radius:50000)

UIStackView

  • 向UIStackView添加一个布局子视图,并设置所需的宽度
stackView.add(myView, width:150)