|
| 1 | +// |
| 2 | +// UIView+JCChain.m |
| 3 | +// JCKitDemo |
| 4 | +// |
| 5 | +// Created by molin.JC on 2017/12/1. |
| 6 | +// Copyright © 2017年 molin. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "UIView+JCChain.h" |
| 10 | + |
| 11 | +#define _Chain_set(Cla, p, sel, r) \ |
| 12 | +- (Cla *(^)(r))sel { return ^(r p){self.p = p; return self;};} |
| 13 | + |
| 14 | + |
| 15 | +@implementation UIView (JCChain) |
| 16 | + |
| 17 | ++ (instancetype)init { |
| 18 | + return [[self alloc] init]; |
| 19 | +} |
| 20 | + |
| 21 | +_Chain_set(UIView, backgroundColor, setBackgroundColor, UIColor *) |
| 22 | +@end |
| 23 | + |
| 24 | +@implementation UILabel (JCChain) |
| 25 | +_Chain_set(UILabel, backgroundColor, setBackgroundColor, UIColor *) |
| 26 | +_Chain_set(UILabel, text, setText, NSString *) |
| 27 | +_Chain_set(UILabel, font, setFont, UIFont *) |
| 28 | +_Chain_set(UILabel, textColor, setTextColor, UIColor *) |
| 29 | +_Chain_set(UILabel, textAlignment, setTextAlignment, NSTextAlignment) |
| 30 | +_Chain_set(UILabel, numberOfLines, setNumberOfLines, NSInteger) |
| 31 | +_Chain_set(UILabel, attributedText, setAttributedText, NSAttributedString *) |
| 32 | +@end |
| 33 | + |
| 34 | +@implementation UITextField (JCChain) |
| 35 | +_Chain_set(UITextField, delegate, setDelegate, id<UITextFieldDelegate>) |
| 36 | +_Chain_set(UITextField, backgroundColor, setBackgroundColor, UIColor *) |
| 37 | +_Chain_set(UITextField, text, setText, NSString *) |
| 38 | +_Chain_set(UITextField, font, setFont, UIFont *) |
| 39 | +_Chain_set(UITextField, textColor, setTextColor, UIColor *) |
| 40 | +_Chain_set(UITextField, textAlignment, setTextAlignment, NSTextAlignment) |
| 41 | +_Chain_set(UITextField, attributedText, setAttributedText, NSAttributedString *) |
| 42 | +_Chain_set(UITextField, placeholder, setPlaceholder, NSString *) |
| 43 | +_Chain_set(UITextField, attributedPlaceholder, setAttributedPlaceholder, NSAttributedString *) |
| 44 | +_Chain_set(UITextField, defaultTextAttributes, setDefaultTextAttributes, NSDictionary *) |
| 45 | +_Chain_set(UITextField, borderStyle, setBorderStyle, UITextBorderStyle) |
| 46 | +_Chain_set(UITextField, clearButtonMode, setClearButtonMode, UITextFieldViewMode) |
| 47 | +_Chain_set(UITextField, keyboardType, setKeyboardType, UIKeyboardType) |
| 48 | +_Chain_set(UITextField, returnKeyType, setReturnKeyType, UIReturnKeyType) |
| 49 | +_Chain_set(UITextField, keyboardAppearance, setKeyboardAppearance, UIKeyboardAppearance) |
| 50 | +_Chain_set(UITextField, inputView, setInputView, UIView *) |
| 51 | +_Chain_set(UITextField, inputAccessoryView, setInputAccessoryView, UIView *) |
| 52 | +@end |
| 53 | + |
| 54 | +@implementation UITextView (JCChain) |
| 55 | +_Chain_set(UITextView, delegate, setDelegate, id<UITextViewDelegate>) |
| 56 | +_Chain_set(UITextView, backgroundColor, setBackgroundColor, UIColor *) |
| 57 | +_Chain_set(UITextView, text, setText, NSString *) |
| 58 | +_Chain_set(UITextView, font, setFont, UIFont *) |
| 59 | +_Chain_set(UITextView, textColor, setTextColor, UIColor *) |
| 60 | +_Chain_set(UITextView, textAlignment, setTextAlignment, NSTextAlignment) |
| 61 | +_Chain_set(UITextView, attributedText, setAttributedText, NSAttributedString *) |
| 62 | +_Chain_set(UITextView, linkTextAttributes, setLinkTextAttributes, NSDictionary *) |
| 63 | +_Chain_set(UITextView, keyboardType, setKeyboardType, UIKeyboardType) |
| 64 | +_Chain_set(UITextView, returnKeyType, setReturnKeyType, UIReturnKeyType) |
| 65 | +_Chain_set(UITextView, keyboardAppearance, setKeyboardAppearance, UIKeyboardAppearance) |
| 66 | +_Chain_set(UITextView, inputView, setInputView, UIView *) |
| 67 | +_Chain_set(UITextView, inputAccessoryView, setInputAccessoryView, UIView *) |
| 68 | +@end |
| 69 | + |
| 70 | +@implementation UIControl (JCChain) |
| 71 | +_Chain_set(UIControl, backgroundColor, setBackgroundColor, UIColor *) |
| 72 | + |
| 73 | +- (UIControl *(^)(id, SEL, UIControlEvents))addTarget { |
| 74 | + return ^(id target, SEL action, UIControlEvents event) { |
| 75 | + [self addTarget:target action:action forControlEvents:event]; return self; |
| 76 | + }; |
| 77 | +} |
| 78 | +@end |
| 79 | + |
| 80 | +@implementation UIButton (JCChain) |
| 81 | + |
| 82 | ++ (UIButton *(^)(UIButtonType))buttonType { |
| 83 | + return ^(UIButtonType type) { |
| 84 | + return [UIButton buttonWithType:type]; |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +- (UIButton *(^)(id, SEL, UIControlEvents))addTarget { |
| 89 | + return ^(id target, SEL action, UIControlEvents event) { |
| 90 | + [self addTarget:target action:action forControlEvents:event]; return self; |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +- (UIButton *(^)(UIFont *))setFont { |
| 95 | + return ^(UIFont *font) { self.titleLabel.font = font; return self; }; |
| 96 | +} |
| 97 | + |
| 98 | +#define _Chain_buttonSet(sel, r) \ |
| 99 | +- (UIButton *(^)(r, UIControlState))sel##OrState {return ^(r p, UIControlState state){[self sel:p forState:state]; return self;}; } \ |
| 100 | +- (UIButton *(^)(r))sel {return ^(r p){self.sel##OrState(p, UIControlStateNormal); return self;};} |
| 101 | + |
| 102 | +_Chain_buttonSet(setTitle, NSString *) |
| 103 | +_Chain_buttonSet(setTitleColor, UIColor *) |
| 104 | +_Chain_buttonSet(setAttributedTitle, NSAttributedString *) |
| 105 | +_Chain_buttonSet(setImage, UIImage *) |
| 106 | +_Chain_buttonSet(setBackgroundImage, UIImage *) |
| 107 | +@end |
| 108 | + |
| 109 | +@implementation UIImageView (JCChain) |
| 110 | + |
| 111 | ++ (UIImageView *(^)(UIImage *))initWithImage { |
| 112 | + return ^(UIImage *image) { return [[UIImageView alloc] initWithImage:image]; }; |
| 113 | +} |
| 114 | +_Chain_set(UIImageView, backgroundColor, setBackgroundColor, UIColor *) |
| 115 | +_Chain_set(UIImageView, image, setImage, UIImage *) |
| 116 | +@end |
| 117 | + |
| 118 | +@implementation UIScrollView (JCChain) |
| 119 | +_Chain_set(UIScrollView, delegate, setDelegate, id<UIScrollViewDelegate>) |
| 120 | +_Chain_set(UIScrollView, backgroundColor, setBackgroundColor, UIColor *) |
| 121 | +_Chain_set(UIScrollView, contentOffset, setOffset, CGPoint) |
| 122 | +_Chain_set(UIScrollView, contentSize, setContentSize, CGSize) |
| 123 | +_Chain_set(UIScrollView, bounces, setBounces, BOOL) |
| 124 | +_Chain_set(UIScrollView, pagingEnabled, setPagingEnabled, BOOL) |
| 125 | +_Chain_set(UIScrollView, scrollEnabled, setScrollEnabled, BOOL) |
| 126 | +_Chain_set(UIScrollView, showsHorizontalScrollIndicator, setHorizontalScrollIndicator, BOOL) |
| 127 | +_Chain_set(UIScrollView, showsVerticalScrollIndicator, setVerticalScrollIndicator, BOOL) |
| 128 | + |
| 129 | +- (UIScrollView *(^)(CGFloat))setOffsetX { |
| 130 | + return ^(CGFloat x) { |
| 131 | + self.contentOffset = CGPointMake(x, self.contentOffset.y); |
| 132 | + return self; |
| 133 | + }; |
| 134 | +} |
| 135 | + |
| 136 | +- (UIScrollView *(^)(CGFloat))setOffsetY { |
| 137 | + return ^(CGFloat y) { |
| 138 | + self.contentOffset = CGPointMake(self.contentOffset.x, y); |
| 139 | + return self; |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +- (UIScrollView *(^)(CGFloat))setContentWidth { |
| 144 | + return ^(CGFloat width) { |
| 145 | + self.contentSize = CGSizeMake(width, self.contentSize.height); |
| 146 | + return self; |
| 147 | + }; |
| 148 | +} |
| 149 | + |
| 150 | +- (UIScrollView *(^)(CGFloat))setContentHeight { |
| 151 | + return ^(CGFloat height) { |
| 152 | + self.contentSize = CGSizeMake(self.contentSize.width, height); |
| 153 | + return self; |
| 154 | + }; |
| 155 | +} |
| 156 | + |
| 157 | +- (UIScrollView *(^)(CGFloat, CGFloat, CGFloat, CGFloat))setInset { |
| 158 | + return ^(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) { |
| 159 | + self.contentInset = UIEdgeInsetsMake(top, left, bottom, right); |
| 160 | + return self; |
| 161 | + }; |
| 162 | +} |
| 163 | + |
| 164 | +@end |
| 165 | + |
| 166 | +@implementation UITableView (JCChain) |
| 167 | + |
| 168 | ++ (UITableView *(^)(UITableViewStyle))initWithStyle { |
| 169 | + return ^(UITableViewStyle style) { return [[UITableView alloc] initWithFrame:CGRectZero style:style];}; |
| 170 | +} |
| 171 | +_Chain_set(UITableView, delegate, setDelegate, id<UITableViewDelegate>) |
| 172 | +_Chain_set(UITableView, dataSource, setDataSource, id<UITableViewDataSource>) |
| 173 | +_Chain_set(UITableView, rowHeight, setRowHeight, CGFloat) |
| 174 | +_Chain_set(UITableView, backgroundColor, setBackgroundColor, UIColor *) |
| 175 | +_Chain_set(UITableView, separatorStyle, setSeparatorStyle, UITableViewCellSeparatorStyle) |
| 176 | +_Chain_set(UITableView, separatorColor, setSeparatorColor, UIColor *) |
| 177 | +_Chain_set(UITableView, tableHeaderView, setTableHeaderView, UIView *) |
| 178 | +_Chain_set(UITableView, tableFooterView, setTableFooterView, UIView *) |
| 179 | +@end |
| 180 | + |
| 181 | +@implementation UICollectionView (JCChain) |
| 182 | + |
| 183 | ++ (UICollectionView *(^)(UICollectionViewLayout *))initWithLayout { |
| 184 | + return ^(UICollectionViewLayout *layout) { return [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; }; |
| 185 | +} |
| 186 | +_Chain_set(UICollectionView, delegate, setDelegate, id<UICollectionViewDelegate>) |
| 187 | +_Chain_set(UICollectionView, dataSource, setDataSource, id<UICollectionViewDataSource>) |
| 188 | +_Chain_set(UICollectionView, backgroundColor, setBackgroundColor, UIColor *) |
| 189 | +@end |
| 190 | + |
| 191 | +@implementation CALayer (JCChain) |
| 192 | +_Chain_set(CALayer, backgroundColor, setBackgroundColor, CGColorRef) |
| 193 | +_Chain_set(CALayer, cornerRadius, setCornerRadius, CGFloat) |
| 194 | +_Chain_set(CALayer, borderWidth, setBorderWidth, CGFloat) |
| 195 | +_Chain_set(CALayer, borderColor, setBorderColor, CGColorRef) |
| 196 | +@end |
0 commit comments