开源分享 FlowerCocoa

104 阅读3分钟

FlowerCocoa是我四月份的时候,在开发Rx项目的时候编写的一个基础库,其目的是为了视觉上分离UI层,使UIKit设置效果和添加约束代码隔开,顺便提升开发效率,中途有其它的任务就利用业余时间一直停停改改,然后就一直鸽到了现在。

FlowerCocoa一组基于UIKit的控件库,我参照Rx的表示法独立封装,其核心依赖于Link。 github.com/marst123/Fl…

设计思路

在设计之前,我一直认为代码层面的语法和自由度是最初的设计理念。Swift为我提供了这样的便利,我需要让它更方便。

To

swift
let label = UILabel()
label.text = name
label.textColor = color
​
chain
let label = UILabel()
      .text(name)
      .textColor(color)

显然,我更喜欢的是下一种方法。这种联合构成了可用的功能,与以前的方法相比,它消除了重复的权利要求。

这是我最初的想法,我只是想用链式思维使事情简单化,通过调用链式来初始化UI和定义属性。然后我意识到,在实际操作中,我还需要能够相互交流,所以在此基础上,我使用“Link”来插入其他属性。通过这个“链接”层,我可以在NSObject允许的范围内定义新的角色,而不受哪个角色的限制(参考,Rx)。

假设Link是一个支持其他电子设备的多功能插线板,电子设备可以具象化为UIKit、QuartzCore等。在电子设备中,不同的品牌有不同的特性,但也有相同的协议特性,子类继承充分体现了其异同。

这是一个非常简单的库,尽可能地保持UI定义的简单(而我也正在逐渐测试和添加与CA库相关的内容)。

不管怎样,这很简单,这就足够了。

特性

  • 'Link'为基础,支持扩展。
  • 扩展自 UIAlertController,UIAlertAction,UIButton, UICollectionView,UICollectionViewFlowLayout,UIControl,UIControl,UILabel,UIStackView,UITableView,UIView..
  • 一行代码设置字体,颜色和图像。
  • 简单的手势操作。
  • 编译富文本更加方便。
  • 干净的代码分离。

安装

FlowerCocoa可通过CocoaPods获得。安装

简单地添加以下行到你的Podfile:

pod 'FlowerCocoa'

使用方法

为了获得更好的体验,你需要:

UIView

    lazy var flower_View: UIView = {
        return UIView().link
            .bgColor(.white)
            .isUserInteractionEnabled(true)
            .base
    }()

UILabel

    lazy var flower_label: UILabel = {
        return UILabel().link
            .text("FlowerCocoa")
            .titleColor(.hex("#333333"))
            .font(.regular(12))
            .lines(0)
            .alignment(.justified)
            .base
    }()

UIButton

    lazy var flower_button: UIButton = {
        return UIButton().link
            .stateNormal({$0.title("FlowerCocoa")})
            .isEnabled(true)
            .setTag(tag: 10000)
            .base
    }()

UIStackView

    lazy var flower_Cocoa: UIStackView = {
        return UIStackView().link
            .config(.horizontal, alignment: .fill, distribution: .fill, spacing: 0)
            .arranged([flower_label, flower_button])
            .base
    }()

UITableView

    lazy var flower_tableView: UITableView = {
        return UITableView(frame: .zero, style: .plain).link
            .rowHeight(49)
            .separator(.none)
            .base
    }()

UICollectionView

    lazy var flower_collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout().link
            .scrollDirection(.vertical)
            .itemSize(.zero)
            .base
        
        return UICollectionView(frame: .zero, collectionViewLayout: layout).link
            .isScrollEnabled(false)
            .showsVerticalScrollIndicator(false)
            .showsHorizontalScrollIndicator(false)
            .base
    }()

你也可以这样使用它:

block

var flower_block: BlockHandler<Bool>?
var flower_nullblock: NullHandler?

register List

 flower_tableView.link
    .register(cellWithClass: Cell.self)
    .register(nibClass: Cell.self)
        
 flower_collectionView.link
    .register(viewClass: Cell.self)
    .registerHeader(viewClass: Cell.self)

color

.hex("#333333", alpha: 1)
.rgb(65, 65, 65, 1)

font

.regular(12)

image

.findName("flower")

AttributedString

NSMutableAttributedString(string: "")
    .append(string: "¥")
    .appendStyle(.font(.ping(.bold(16))), string: "100")
    .append(string: "/天")

action

flower_button.link.setAction { sender in }
flower_View.link.addGesture([UITapGestureRecognizer(), UILongPressGestureRecognizer()]) { sender in
  if sender is UITapGestureRecognizer {
    print("Tap Action")
  }else {
    print("Long Action")
  }
}

Author

marst123, tianlan2325@qq.com

License

FlowerCocoa is available under the MIT license. See the LICENSE file for more info.