Git Product home page Git Product logo

ppmaker's Introduction

iOS链式创建UI终结者 ➜ PPMaker

做iOS开发,创建UI控件,必须的不说,还多,尤其你新入手一个项目。

我自己写代码,向来想省事:怎么能不一个一个属性写?带着这个问题,我刚开始创建了各种Tool来处理,后来用Category证据在此),可Category有个烦人的问题:有些属性我不需要但是方法参数有,而有些属性我需要方法参数没有

昨天,看到臧成威如何利用Objective-C写一个精美的DSL,唉,挺好,就想着优化下自己的,于是就有了 PPMaker.

在此,献上对臧老师的感谢。

PPMaker的不同(优点)

➊ 链式调用,代码简洁;

特别字符统一处理

➋ 点语法后面有提示(Masonry是没有的 )

PPMaker有提示

Masonry没提示

➌ 不需要终结词 臧老师给的示例有; ➍ 不需要助词 如Masonry中的with,这个也可以说是一个缺点吧; ➎ pod可根据需求随意选择。

模块可独立使用 ➏ 不需要的属性,根本不用管 主要对比Category;

PPMaker的用法

第一步、导入PPMaker

方法一、使用CocoaPods安装

pod 'PPMaker', '~> 0.0.22'

方法二、手动下载拖入

第二步、用对应的类如:UILable直接调用pp_

easyToUse_lb

easyToUse_imgV

如下创建一个简单的UILabel对象: Snip20180509_10.png

maker调用,直接调UILabel对象的属性,如texttextColorframe等,当然也有自定义的,如intoView表示要加到哪个view上、fontSize实际上[UIFont systemFontOfSize:fontSize]的简化等等。

总之,PPMaker is very easy to use.

创建UI控件的路程

起初:一个属性一个属性赋值

UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 50)];
[self.view addSubview:lb];
lb.backgroundColor = [UIColor whiteColor];
lb.text = @"我是一个lb";
lb.textColor = [UIColor blueColor];
lb.textAlignment = NSTextAlignmentCenter;
lb.font = [UIFont systemFontOfSize:18];

后来:用Category快速创建

@interface UILabel (EasyMake)
+(UILabel *)lbMakeWithSuperV:(UIView *)superV
frame:(CGRect)frame
font:(UIFont *)font
alignment:(NSTextAlignment)alignment
text:(NSString *)text
textColor:(UIColor *)textColor;

@implementation UILabel (EasyMake)
+(UILabel *)lbMakeWithSuperV:(UIView *)superV
frame:(CGRect)frame
font:(UIFont *)font
alignment:(NSTextAlignment)alignment
text:(NSString *)text
textColor:(UIColor *)textColor
{
UILabel *lb = [[UILabel alloc]init];
if (superV) {
[superV addSubview:lb];
}
if (font) {
lb.font = font;
}
if (text) {
lb.text = text;
}
if (textColor) {
lb.textColor = textColor;
}
lb.frame = frame;
lb.textAlignment = alignment;
return lb;
}
@end

现在:链式调用的 DSL

Snip20180509_11.png

Snip20180509_13.png

Snip20180509_14.png

Snip20180509_15.png

结语

PPMaker是自己写的最满意的一个库,解决了自己一直以来创建UI配置attributedText的苦恼,这其中也参考了一些大神的的blog和开源库,在此,再次表示感谢。今天,分享自己的这个库,希望帮助像我一样的同学,其次,希望觉得好的,给个star.

当然了,这个库,我会持续更新的,有什么问题,欢迎提出来。或者谁有更好的做法,热烈欢迎告知一下,深表谢意!

Version Update Record

2018-06-11

  1. 真正解决了XCode警告,同时优化提升信息更清晰,如:当用UILabel *调用UIButton *titleState后运行,控制台会提示“Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '💊请注意💊:titleState不是UILabel所拥有的属性,而是UIButton所特有的!More see -[PPMake(UIButton) titleState] 第33行'”;
  2. UIButton新增setImageEdgeInsetssetTitleEdgeInsets;

2018-06-09

  1. 优化UIImageView设置imageUIButton设置image导致PPMake想调用UIButton时调用成UIImageViewimage; ** 具体做法**:UIButton中的image -> imageState
  2. 优化UIButton中快速设置attributedStr,不再依赖对应状态的title必须有值; 具体做法attributedFontColor -> attributedFontColorTitle, normalAttributedFontColor -> normalAttributedFontColorTitle, highlightAttributedFontColor -> highlightAttributedFontColorTitle
  3. PPMake对应的Category分离出去,虽然类文件增多,但是不至于都在PPMake中,显得过于复杂or臃肿;
  4. 处理圆角+阴影的情况,如果阴影+阴影透明度都为0,只设置圆角。
  5. 新增UIButton防止重复点击功能;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.