美文网首页
R.swift 使用

R.swift 使用

作者: xinnyu | 来源:发表于2017-04-13 17:01 被阅读180次

安装

使用CocoaPods来对R.Swift进行安装:
pod 'R.swift'

配置

  1. 进入项目的配置界面,在左边的TARGETS项下面选择我们的项目,并在右边点击Build Phases这个tab。
  2. 进入<Build Phases>这个tab后,我们看到左上角有一个"+"按钮,点击并在弹出的选项卡中选择New Run Script Phase
  3. 我们会看到界面的下方多出了一个Run Script项,展开它,并在脚本输入区域输入"\$PODS_ROOT/R.swift/rswift" "$SRCROOT" (第二对双引号括起来所代码的是项目的根目录,你也可以放到根目录下的其他目录中,只需将其修改为"$SRCROOT/XXX",XXX为目标目录名)。
    4.我们按住新建的这个Run Script项向上移动,移到Compile Source项的上方,不过也要保证此时它也在Check Pods Manifest.lock项的下方。
  4. Command + B,编译一下,编译成功后,在Finder进入到刚刚我们制定的目录中,此时我们会看到一个名为R.generated.swift的文件已经创建了,直接把此文件拖入Xcode项目中,记住不要勾选Copy items if needed项
  5. 配置到此完成,我们可以构建自己的项目了

使用

Image - 图片

//  不使用R.Swift
let pImage = UIImage(named: "image_test")
//  使用R.Swift
let nImage = R.image.image_test()

File - 数据文件

//  不使用R.Swift
let pFile = NSBundle.mainBundle().pathForResource("DataFile", ofType: "json")
//  使用R.Swift
let nFile = R.file.dataFileJson.path()

Font - 字体

//  不使用R.Swift
let pFont = UIFont(name: "chalkduster", size: 35)
//  使用R.Swift
let nFont = R.font.chalkduster(size: 35)
//  你看,非常神奇,在上面的方法中你不仅可以选择字体类型,还能设置字体大小

Color - 颜色
把.clr文件拖入项目中即可使用颜色配置
let appRedColor = R.color.myAppColor.red()

Nibs

不使用R.swift

let nameOfNib = "CustomView"
let customViewNib = UINib(nibName: "CustomView", bundle: nil)
let rootViews = customViewNib.instantiate(withOwner: 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)
let customView = R.nib.customView.firstView(owner: nil)

let viewControllerWithNib = CustomViewController(nib: R.nib.customView)

Localized strings

不使用R.swift

let welcomeMessage = NSLocalizedString("welcome.message", comment: "")
let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "")

// Formatted strings
let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.current, "Alice")

// Stringsdict files
let progress = String(format: NSLocalizedString("copy.progress", comment: ""), locale: NSLocale.current, 4, 23)

使用 R.swift

// Localized strings are grouped per table (.strings file)
let welcomeMessage = R.string.localizable.welcomeMessage()
let settingsTitle = R.string.settings.title()

// Functions with parameters are generated for format strings
let welcomeName = R.string.localizable.welcomeWithName("Alice")

// Functions with named argument labels are generated for stringsdict keys
let progress = R.string.localizable.copyProgress(completed: 4, total: 23)

相关文章

  • R.swift 使用详解

    R.swift 使用详解 R.Swift[https://github.com/mac-cain13/R.swif...

  • cocoapods组件化(3)

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

  • R.swift 使用

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

  • iOS 13 多语言适配

    本文基于 R.swift 中的 strings。R.swift 的具体使用就不介绍了,主要说应用内的语言设置和 i...

  • R.swift的使用和安装

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

  • R.swift的使用和安装

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

  • R.swift用法

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

  • 如何安装R.swift

    强烈建议使用CocoaPods 添加 'R.swift' 到你的pod file,然后 pod install ...

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

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

  • R.swift 使用

    1. 在Podfile 中添加 pod 'R.swift' R.swift 2. 在项目中添加脚本 3. 修改脚本...

网友评论

      本文标题:R.swift 使用

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