链式编程 函数编程---iOS笔记摘录

链式编程
增强可读性(通过.将所有操作通过一行代码搞定)

  UILabel
  其他控件(见工具库)

UILabel

使用

 UILabel *nameL=YLable.yfont(UIFont(20)).ytextColor(RGBOF(0xffffff)).InView(headView);

UILabel+LinkBlock.h

#import 

#define   YTLable    [UILabel new]

@interface UILabel (LinkBlock)

+(UILabel *)Init;
- (UILabel * (^)(CGRect bounds))ybounds;
- (UILabel * (^)(CGRect frame))yframe;
- (UILabel * (^)(UIView *View))InView;

- (UILabel * (^)(NSString *text))ytext;
- (UILabel * (^)(UIFont *font))yfont;
- (UILabel * (^)(UIColor *textColor))ytextColor;
- (UILabel * (^)(NSTextAlignment textAlignment))ytextAlignment;

- (UILabel * (^)(NSInteger numberOfLines))ynumberOfLines;
- (UILabel * (^)(UIColor *backgroundColor))ybackgroundColor;
- (UILabel * (^)(NSAttributedString *attributedText))yattributedText;
- (UILabel * (^)(CGFloat cornerRadius))ycornerRadius;
@end

UILabel+LinkBlock.m

#import "UILabel+LinkBlock.h"

@implementation UILabel (LinkBlock)

+(UILabel *)Init{
    
    return [[UILabel alloc] init];
}


// bounds
- (UILabel * (^)(CGRect bounds))ybounds{
    return ^id(CGRect ybounds){
        self.bounds= ybounds;
        return self;
    };
}
// frame
- (UILabel * (^)(CGRect frame))yframe{
    return ^id(CGRect yframe){
        self.frame= yframe;
        return self;
    };
}
// text
- (UILabel * (^)(NSString *text))ytext{
    return ^id(NSString  *ytext){
        self.text=ytext;
        return self;
    };
}
// color
- (UILabel * (^)(UIColor *textColor))ytextColor{
    return ^id(UIColor *ytextColor ){
        self.textColor=ytextColor;
        return self;
    };
}
// font
- (UILabel * (^)(UIFont *font))yfont{
    return ^id(UIFont *yfont){
        self.font= yfont;
        return self;
    };
}
// textAlignment
- (UILabel * (^)(NSTextAlignment textAlignment))ytextAlignment{
    return ^id(NSTextAlignment  ytextAlignment){
        self.textAlignment=ytextAlignment;
        return self;
    };
}
// numberOfLines
- (UILabel * (^)(NSInteger numberOfLines))ynumberOfLines{
    return ^id(NSInteger ynumberOfLines ){
        self.numberOfLines=ynumberOfLines;
        return self;
    };
}
// backgroundColor
- (UILabel * (^)(UIColor *backgroundColor))ybackgroundColor{
    return ^id(UIColor *ybackgroundColor ){
        self.backgroundColor=ybackgroundColor;
        return self;
    };
}
// attributedText
- (UILabel * (^)(NSAttributedString *attributedText))yattributedText{
    return ^id(NSAttributedString  *yattributedText){
        self.attributedText=yattributedText;
        return self;
    };
}
// cornerRadius
- (UILabel * (^)(CGFloat cornerRadius))ycornerRadius{
    return ^id(CGFloat  ycornerRadius){
        self.layer.cornerRadius=ycornerRadius;
        self.layer.masksToBounds=YES;
        return self;
    };
}
// add in SupperView
- (UILabel * (^)(UIView *View))InView{
    return ^id(UIView  *InView){
        [InView addSubview:self];
        return self;
    };
}
@end
函数编程
函数做完操作后,返回对象本身。

你可能感兴趣的:(链式编程 函数编程---iOS笔记摘录)