美文网首页
使用R.swift来管理资源

使用R.swift来管理资源

作者: ISwiftUI | 来源:发表于2017-12-09 19:41 被阅读16次

基础用法

常见的操作:
1 => let icon = UIImage(named: "settings-icon")
2 => let font = UIFont(name: "San Francisco", size: 42)
3 => performSegueWithIdentifier("openSettings")

替代方案:
1 => let icon = R.image.settingsIcon
2 => let font = R.font.sanFrancisco(size:42)
3 => performSegueWithIdentifier(R.segue.openSettings)

1.UIImage

使用原生swift

let settingsIcon = UIImage(named: "settings-icon")
let gradientBackground = UIImage(named: "gradient.jpg")

R.swift

let settingsIcon = R.image.settingsIcon
let gradientBackground = R.image.gradientJpg

2.Storyboards

使用原生swift

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialTabBarController = storyboard.instantiateInitialViewController() as? UITabBarController
let settingsController = self.instantiateViewControllerWithIdentifier("settingsController") as? SettingsController

R.swift

let storyboard = R.storyboard.main.instance
let initialTabBarController = R.storyboard.main.initialViewController
let settingsController = R.storyboard.main.settingsController
//通过这个代码来校验运行时storyboard的图片是否都能被加载
// 只在debug模式下有效,会通过断言来提示
R.storyboard.main.validateImages()

//在运行时校验所有的viewController能够被正常加载
mode.R.storyboard.main.validateViewControllers()

3.Segues

使用原生swift

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

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let settingsController = segue.destination as? SettingsController,
       let segue = segue as? CustomSettingsSegue, segue.identifier == "openSettings" {
      segue.animationType = .LockAnimation
      settingsController.lockSettings = true
    }
  }

R.swift

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

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if let typedInfo = R.segue.overviewController.openSettings(segue: segue) {
    typedInfo.segue.animationType = .LockAnimation
    typedInfo.destinationViewController.lockSettings = true
  }

4.Nibs

使用原生swift

let nameOfNib = "CustomView"
let customViewNib = UINib(nibName: "CustomView", bundle: nil)
let rootViews = customViewNib.instantiateWithOwner(nil, options: nil)
let customView = rootViews[0] as? CustomView
let viewControllerWithNib = CustomViewController(nibName: "CustomView", bundle: nil)

R.swift

let nameOfNib = R.nib.customView.name
let customViewNib = R.nib.customView
let rootViews = R.nib.customView.instantiateWithOwner(nil, options: nil)
let customView = R.nib.customView.firstView(nil, options: nil)
let viewControllerWithNib = CustomViewController(nib: R.nib.customView)

5.Reusable cells

使用原生swift

let textCellNib = UINib(nibName: "TextCell", bundle: nil)
tableView.registerNib(textCellNib, forCellReuseIdentifier: "TextCellIdentifier")

R.swift

tableView.registerNib(R.nib.textCell)

//cellForRowAtIndexPath中获取cell
let textCell = tableView.dequeueReusableCellWithIdentifier(R.nib.textCell.reuseIdentifier, forIndexPath: indexPath)

6.Custom fonts

使用原生swift

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

R.swift

let lightFontTitle = R.font.acmeLight(size: 22)

7.Resource files

使用原生swift

let jsonURL = NSBundle.mainBundle().URLForResource("seed-data", withExtension: "son")

R.swift

let jsonURL = R.file.seedDataJson

8.Localized Strings

使用原生swift

let welcomeMessage = NSLocalizedString("welcome.message", comment: "")
let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "")
let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice")
let progress = String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)

R.swift

let welcomeMessage = R.string.localizable.welcomeMessage()
let settingsTitle = R.string.settings.title()
let welcomeName = R.string.localizable.welcomeWithName("Alice")
let progress = R.string.localizable.copyProgress(completed: 4, total: 23)

相关文章

  • cocoapods组件化(3)

    资源管理库R.swift的使用 安装pod 'R.swift' 脚本配置 工程中配置 在pod私有库中使用R.sw...

  • 使用R.swift来管理资源

    基础用法 常见的操作:1 => let icon = UIImage(named: "settings-icon"...

  • 使用R.swift管理项目资源

    在使用OC开发时,有个尴尬的操作,就是: 硬编码的图片名称到处都是,当你在Assets改动图片资源时,这里就尴尬了...

  • R.swift的使用和安装

    一、先说说为啥子你可以选择使用R.swift呢?    R.swift可以获取强类型、自动完成的资源,如图像、字体...

  • R.swift-优雅地引用项目资源

    R.swift是一个高效引入iOS资源的框架,避免了使用字符串引入资源文件导致程序崩溃的尴尬。目前R.swift支...

  • R.swift的使用和安装

    为什么使用R.swift R.swift可以获取强类型、自动完成的资源,如图像、字体和段落完全类型化。更少的强制转...

  • R.swift用法

    R.swift使用和介绍 R.swift可以获取强类型、自动完成的资源,如图像、字体和段落完全类型化。更少的强制转...

  • R.swift 使用

    安装 使用CocoaPods来对R.Swift进行安装:pod 'R.swift' 配置 进入项目的配置界面,在左...

  • R.swift 的使用

    在项目中引入 R.swift,更安全的获取资源 什么是 R.swift 介绍 R.swift 前,我们先看看 R....

  • R.Swift(I)(US)

    R.Swift高效引用资源文件 US

网友评论

      本文标题:使用R.swift来管理资源

      本文链接:https://www.haomeiwen.com/subject/psepixtx.html