美文网首页链式编程
Masonry的链式编成思想

Masonry的链式编成思想

作者: Sniper_Zheng | 来源:发表于2016-07-26 18:02 被阅读35次

    Masonry的链式编成思想

    mas_makeConstraints 给控件设置布局

    参数是一个block
    返回值是一个NSArray

    - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
        block(constraintMaker);
        return [constraintMaker install];
    }
    
    

    个人理解: 拿我特别喜欢吃的木屋烧烤举例子.

    1.创建一个约束制造者

    • 拿下菜单.

    2.调用block(maker) 让外界把约束加好

    • 把点菜那个单给顾客, 想吃啥自己往上面写.

    3.[constraintMaker install]; 遍历约束制造者的所有约束并返回.

    • 上菜给顾客
    • install是老板烤串,把串烤好在return

    MASConstraintMaker 约束制造者类 (点菜单) 在Masonry里也封装了些添加约束的方法. 通过链式编成思想,使得添加约束简单明了.

    • 点菜时候各种菜名很复杂,或许有不认识的字,所以顾客点菜时候不用写菜的全名,只要把相应的编号写上去就好 比如想点3L的朝日啤酒 就写2310.
    另外关于链式编成思想.我以Masonry的源码为基础 写了一个简单的图文混排,以便更好的理解链式编成思想.
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *str = @"Sniper";
        NSMutableAttributedString *waterStr = [str makeString:^(ZNStringMaker *maker) {
            maker.addStr(@" is").addStr(@" a").addStr(@" Developer ").addImgName(@"water").addStr(@" yo~!");
        }];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 50, 240, 40)];
        [self.view addSubview:label];
        label.attributedText = waterStr;
        label.backgroundColor = [UIColor orangeColor];
    }
    
    

    结果:

    屏幕快照 2016-07-26 下午5.53.18.png

    代码:

    #import <Foundation/Foundation.h>
    #import "ZNStringMaker.h"
    
    @interface NSString (ZNAdd)
    
    - (NSMutableAttributedString *)makeString:(void (^)(ZNStringMaker *maker))block;
    
    @end
    
    #import "NSString+ZNAdd.h"
    
    @implementation NSString (ZNAdd)
    
    - (NSMutableAttributedString *)makeString:(void (^)(ZNStringMaker *maker))block; {
        ZNStringMaker *maker = [[ZNStringMaker alloc] initWithStr:self];
        block(maker);
        return [maker makeAttrStr];
    }
    
    @end
    
    
    #import <Foundation/Foundation.h>
    
    @interface ZNStringMaker : NSObject
    
    - (ZNStringMaker * (^)(NSString *str))addStr;
    - (ZNStringMaker * (^)(NSString *imgNameStr))addImgName;
    
    - (instancetype)initWithStr:(NSString *)str;
    
    - (NSMutableAttributedString *)makeAttrStr;
    
    @end
    
    #import "ZNStringMaker.h"
    #import <UIKit/UIKit.h>
    
    @interface ZNStringMaker ()
    
    @property (nonatomic, copy)NSMutableString *strResult;
    @property (nonatomic, copy)NSMutableAttributedString *attrStrResult;
    
    @end
    
    @implementation ZNStringMaker
    
    - (ZNStringMaker * (^)(NSString *str))addStr {
        return ^(NSString *str) {
            if (str) {
                if (_attrStrResult) {
                    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:str];
                    [_attrStrResult appendAttributedString:attrStr];
                } else {
                    [_strResult appendString:str];
                }
            }
            return self;
        };
    }
    
    -(ZNStringMaker *(^)(NSString *))addImgName {
        return ^(NSString *str) {
            if (str) {
                NSTextAttachment *textAttach = [[NSTextAttachment alloc] init];
                NSString *imgName = str;
                textAttach.image = [UIImage imageNamed:imgName];
                NSAttributedString *attrImg = [NSAttributedString attributedStringWithAttachment:textAttach];
                
                _attrStrResult = [[NSMutableAttributedString alloc] initWithString:_strResult];
                [_attrStrResult appendAttributedString:attrImg];
    
            }
            return self;
        };
    
    }
    
    - (instancetype)initWithStr:(NSString *)str {
        self = [super init];
        if (self) {
            _strResult = [[NSMutableString alloc] initWithString:str];
        }
        
        return self;
    }
    
    - (NSMutableAttributedString *)makeAttrStr {
        return _attrStrResult;
    }
    
    @end
    
    

    相关文章

      网友评论

        本文标题:Masonry的链式编成思想

        本文链接:https://www.haomeiwen.com/subject/odonjttx.html