Swift-使用 R.swift 优雅的使用资源文件

来源:互联网 发布:深圳软件开发工资水平 编辑:程序博客网 时间:2024/05/20 07:51

R.swift 让 Swift 更方便快捷安全的使用资源文件, 一旦学会使用, 我保证你会爱上它

R.swift GitHub 地址


R.swift 特点:

  • 每当项目build时,R.swift开始运行
  • 这个文件根据项目里的资源文件自动在 R.generated.swift 文件中按照类型生成结构体
  • 强类型,无需类型判断和转换,自动返回对应类型
  • 支持多种资源类型
  • 自动完成,无需猜测图像名称,避免资源名称拼写错误

安装

  1. 添加 pod 'R.swift' 到你的 Podfile 文件中, 然后运行 pod install

  2. 在项目中: 点击项目名称, 选择 TARGETS, 点击 Build Phases ,在点击左上角 + 号添加 New Run Script Phase
    这里写图片描述

  3. 打开并复制下面 "$PODS_ROOT/R.swift/rswift" "$SRCROOT"到黑色输入框中

这里写图片描述
4. Build 项目, 在 $SRCROOT-folder目录下找到 R.generated.swift 文件 , 拖拽 R.generated.swift 文件到项目中,并不勾选 Copy items if needed
这里写图片描述


R.swift 的具体使用


一、图片-Images

原始用法

let settingsIcon = UIImage(named: "settings-icon")

使用 R.swift

let settingsIcon = R.image.settingsIcon()

二、字体-Fonts

原始用法

let lightFontTitle = UIFont(name: "Acme-Light", size: 22)

使用 R.swift

let settingsIcon = R.image.settingsIcon()

三、文件-Files

原始用法

let plistURL = Bundle.main.url(forResource: "book", withExtension: "plist")let jsonPath = Bundle.main.path(forResource: "data", ofType: "json")

使用 R.swift

let plistURL = R.file.bookPlist()let jsonPath = R.file.DataJson.path()

四、颜色-Colors

R.swift 对于颜色是通过(.clr)文件获取颜色
(.clr)文件具体详情请看上篇博客(Swift-颜色设置技巧和(.clr)文件的创建和使用)
R.swift 通过 R.color.xxx 使用颜色

原始用法

label.textColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)

使用 R.swift

Colors are extracted from the *.clr files that are in your Xcode project

label.textColor = R.color.appColors.textColor()

五、故事版-Storyboards

原始用法

let storyboard = UIStoryboard(name: "Main", bundle: nil)let tabBarController = storyboard.instantiateInitialViewController() as? UITabBarControllerlet settingsController = storyboard.instantiateViewController(withIdentifier: "settingsController") as? SettingsController

使用 R.swift

let storyboard = R.storyboard.main()let tabBarController = R.storyboard.main.initialViewController()let settingsController = R.storyboard.main.settingsController()

六、Segues

原始用法

performSegue(withIdentifier: "openSettings", sender: self)

使用 R.swift

performSegue(withIdentifier: R.segue.overviewController.openSettings, sender: self)

七、Nibs

原始用法

let nib = UINib(nibName: "ToolBar", bundle: nil)let toolBar = nib.instantiate(withOwner: nil, options: nil).first as? ToolBar

使用 R.swift

let toolBar  = R.nib.toolBar.firstView(owner: self)let toolBar2 = R.nib.toolBar.secondView(owner: self)

八、Reusable identifier / table view cells

原始用法

class FaqAnswerController: UITableViewController {  override func viewDidLoad() {    super.viewDidLoad()    let textCellNib = UINib(nibName: "TextCell", bundle: nil)    tableView.register(textCellNib, forCellReuseIdentifier: "TextCellIdentifier")  }  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {    let textCell = tableView.dequeueReusableCell(withIdentifier: "TextCellIdentifier", for: indexPath) as! TextCell    textCell.mainLabel.text = "Hello World"    return textCell  }}

使用 R.swift

class FaqAnswerController: UITableViewController {  override func viewDidLoad() {    super.viewDidLoad()    tableView.register(R.nib.textCell)  }  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {    let textCell = tableView.dequeueReusableCell(withIdentifier: R.nib.textCell.identifier, for: indexPath)!    textCell.mainLabel.text = "Hello World"    return textCell  }}
原创粉丝点击