Swift-->R.swift带你体验Android中R类的便利

前端之家收集整理的这篇文章主要介绍了Swift-->R.swift带你体验Android中R类的便利前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

R.swift可以很方便的管理IOS各种资源,有点类似Android中的R类….
先来看看效果图:

库的首页: https://github.com/mac-cain13/R.swift

安装方法: http://www.jianshu.com/p/0c97ef3cdc38

安装需要2点:
1:需要运行一段脚本 "$PODS_ROOT/R.swift/rswift" "$SRCROOT"
2:脚本生成文件R.generated.swift,需要复制到项目中,这样才能愉快的使用R.swift
使用的时候,不需要import…就可以直接使用R.image.xx方式;
当你创建了新的资源,你需要重复第2步.


相关用法:https://github.com/mac-cain13/R.swift/blob/master/Documentation/Examples.md#examples

Images

R.swift will find both images from Asset Catalogs and image files in your bundle.

Vanilla

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

With R.swift

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

Custom fonts

Vanilla

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

With R.swift

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

Resource files

Vanilla

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

With R.swift

let jsonURL = R.file.seedDataJson()
let jsonPath = R.file.seedDataJson.path()

Colors

Vanilla

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

With R.swift

// Colors are extracted from the *.clr files that are in your Xcode project
label.backgroundColor = R.color.appColors.backgroundColor()
label.textColor = R.color.appColors.textColor()

There are some points to keep in mind when using Color palettes,see About Colors

Localized strings

Vanilla

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.currentLocale(),"Alice")

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

With 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)

Storyboards

Vanilla

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

With R.swift

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

Segues

Vanilla

// Trigger segue with:
performSegueWithIdentifier("openSettings",sender: self)

// And then prepare it:
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
  if let settingsController = segue.destinationViewController as? SettingsController,segue = segue as? CustomSettingsSegue
    where segue.identifier == "openSettings" {
      segue.animationType = .LockAnimation
      settingsController.lockSettings = true
  }
}

With R.swift

// Trigger segue with:
performSegueWithIdentifier(R.segue.overviewController.openSettings,sender: self)

// And then prepare it:
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
  if let typedInfo = R.segue.overviewController.openSettings(segue: segue) {
    typedInfo.segue.animationType = .LockAnimation
    typedInfo.destinationViewController.lockSettings = true
  }
}

Tip: Take a look at the SegueManager library,it makes segues block based and is compatible with R.swift.

Nibs

Vanilla

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)

With 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)

Reusable table view cells

Vanilla

class FaqAnswerController: UITableViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let textCellNib = UINib(nibName: "TextCell",bundle: nil)
    tableView.registerNib(textCellNib,forCellReuseIdentifier: "TextCellIdentifier")
  }

  override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let textCell = tableView.dequeueReusableCellWithIdentifier("TextCellIdentifier",forIndexPath: indexPath) as! TextCell
    textCell.mainLabel.text = "Hello World"
    return textCell
  }
}

With R.swift

class FaqAnswerController: UITableViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerNib(R.nib.textCell)
  }

  override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let textCell = tableView.dequeueReusableCellWithIdentifier(R.nib.textCell.identifier,forIndexPath: indexPath)!
    textCell.mainLabel.text = "Hello World"
    return textCell
  }
}

Reusable collection view cells

Vanilla

class RecentsController: UICollectionViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let talkCellNib = UINib(nibName: "TalkCell",bundle: nil)
    collectionView?.registerNib(talkCellNib,forCellWithReuseIdentifier: "TalkCellIdentifier")
  }

  override func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("TalkCellIdentifier",forIndexPath: indexPath) as! TalkCell
    cell.configureCell("Item \(indexPath.item)")
    return cell
  }
}

With R.swift

class RecentsController: UICollectionViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    collectionView?.registerNib(R.nib.talkCell)
  }

  override func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(R.reuseIdentifier.talkCell,forIndexPath: indexPath)!
    cell.configureCell("Item \(indexPath.item)")
    return cell
  }
}

至此: 文章就结束了,如有疑问: QQ群 Android:274306954 Swift:399799363 欢迎您的加入.

原文链接:https://www.f2er.com/swift/323150.html

猜你在找的Swift相关文章