继续发挥ios的可读性:teacup ( ios readability: teacup ) 尽管它已经过时

访问量: 1307

refer to:  https://github.com/colinta/teacup,   https://github.com/colinta/sweettea

上篇说道了 sugarcup,  这次说说teacup. 尽管官方文档说它已经过时了,后继者是 MotionKit

它的目的是 抛弃 Xcode 和 XIB 文件。 (广大人民群众喜闻乐见的对某种技术的深恶痛绝) ^_^  它是一种专门针对IOS开发的视图层(view layer) 的DSL(领域专用语言). Teacup由两部分组成:

1. 专门描述视图结构 —— 类似于 html

2. 专门描述视图的样式   —— 类似于 css

例如;

# 视图,  view layer:
class MyController < TeacupWindowController
  stylesheet :main_window

  def teacup_layout
    subview(NSButton, :hi_button)
  end
end

# 样式: style
Teacup::Stylesheet.new :main_window do
  style :hi_button,
    origin: [10, 10],
    title: 'Hi!'
end 

而 Sweettea (teacup + sugarcube = sweettea ) 把这种可读性提高到了一个新的高度(编程的终极目标,是让代码达到自然语言的高度)

例如:(对比一下第一个和第三个,是不是让人心潮澎湃? 你敢想象 第三个跟 raw object c 的对比吗?)

# what was
my_image = UIImage.imageNamed("Logo")
style :label,
  font: UIFont.fontWithName("Inconsolata", size:UIFont.systemFontSize),
  image: my_image,
  origin: [10, 10],
  size: my_image.size

# sugarcube
my_image = "Logo".uiimage
style :label,
  font: "Inconsolata".uifont,
  image: my_image,
  origin: [10, 10],
  size: my_image.size

# sweettea
style :label,
  font: "Inconsolata",
  image: "Logo",
  origin: [10, 10]

不过,该插件目前已经停止更新,原作者建议 使用 MotionKit 来代替 Teacup (同时,使用 sweetkit 代替 sweettea ) 

原文如下:(信中阐述了为何作者会停更 teacu, sweettea的原因,以及 motionkit的出现,以及其他框架/工具的特点 )

by colinta

If you've worked with XIB/NIB files, you might know that while they can be cumbersome to deal with, they have the great benefit of keeping your controllers free of layout and styling concerns. Teacup brought some of this benefit, in the form of stylesheets, but you still built the layout in the body of your controller file. This needed to be fixed.

如果你用过XIB/NIB,那么你一定会觉得它们烦透了,虽然它们可以为你的页面布局带来巨大的好处。而Teacup通过使用stylesheet的形式保留了这些好处,不过仍然是把layout代码留在了controller中。这样不合适。

Plus Teacup is a beast! Imported stylesheets, orientation change events, auto-layout support. It's got a ton of features, but with that comes a lot of complexity. This has led to an unfortunate situation - I'm the only person who understands the code base! This was never the intention of Teacup. It started out as, and was always meant to be, a community project, with contributions coming from all of its users.

另外,Teacup也很难驾驭。导入了stylesheets, orientation change  events, auto-layout ... 虽然有很多特性,但是也同时带来更多的复杂性。于是我面临这样的境地:底层代码只有我一个人看的懂!这根本不是Teacup项目的本意。它产生时就是一个社区性的项目,希望大家都能提交代码~

When ProMotion and later RMQ were released, they both included their own styling mechanisms. Including Teacup as a dependency would have placed a huge burden on their users, and they would have had to ensure compatibility. Since Teacup does a lot of method swizzling on base classes, this is not a trivial undertaking.

当 ProMotion 和 RMQ相继出现后,它们都带来了自身的 styling 工具。如果在这两种项目中再使用Teacup,会为用户带来不小的麻烦和复杂度,因为Teacup对底层的基础类做了很多修改。

If you use RMQ or ProMotion already, you'll find that MotionKit fits right in. We designed it to be something that can easily be brought into an existing project, too; it does not extend any base classes, so it's completely opt-in.

如果你正在使用RMQ 或者ProMotion, MotionKit比较适合你。我们设计MotionKit的目的就是为了让它能够简单轻松的在既有项目中被使用,它没有改动过任何底层的基础类。所以不必担心任何兼容性的问题。

Unlike Teacup, you won't have your styles reapplied due to orientation changes, but it's really easy to set that up, as you'll see. Or, use AutoLayout (the DSL is better than Teacup's, I think) and you'll get orientation changes for free!

与Teacup不同的是,你不必为你的orientation 改动而 reapply styles, 所以特别简单。或者使用AutoLayout 也行,它的DSL比Teacup的要好。  

Big thanks to everyone who contributed on this project! I hope it serves you as well as Teacup, and for even longer into the future.

感谢所有为本项目(MotionKit) 提供过支持的人!我希望MotionKit能向Teacup一样,很好的为你服务,而且能服务很长时间。

Sincerely,

Colin T.A. Gray Feb 13, 2014 

订阅/RSS Feed

Subscribe

分类/category