zoukankan      html  css  js  c++  java
  • YYCategories阅读代码

    1NSString+YYAdd

    - (BOOL)containsString:(NSString *)string {
    
        if (string == nil) return NO;
    
        return [self rangeOfString:string].location != NSNotFound;
    
    }

    2NSNumber+YYAdd

    + (NSNumber *)numberWithString:(NSString *)string {
        NSString *str = [[string stringByTrim] lowercaseString];
        if (!str || !str.length) {
            return nil;
        }
        
        static NSDictionary *dic;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            dic = @{@"true" :   @(YES),
                    @"yes" :    @(YES),
                    @"false" :  @(NO),
                    @"no" :     @(NO),
                    @"nil" :    [NSNull null],
                    @"null" :   [NSNull null],
                    @"<null>" : [NSNull null]};
        });
        NSNumber *num = dic[str];
        if (num) {
            if (num == (id)[NSNull null]) return nil;
            return num;
        }
        
        // hex number
        int sign = 0;
        if ([str hasPrefix:@"0x"]) sign = 1;
        else if ([str hasPrefix:@"-0x"]) sign = -1;
        if (sign != 0) {
            NSScanner *scan = [NSScanner scannerWithString:str];
            unsigned num = -1;
            BOOL suc = [scan scanHexInt:&num];
            if (suc)
                return [NSNumber numberWithLong:((long)num * sign)];
            else
                return nil;
        }
        // normal number
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
        return [formatter numberFromString:string];
    }

     

    3.NSArray+YYAdd

    - (id)objectOrNilAtIndex:(NSUInteger)index {
        return index < self.count ? self[index] : nil;
    }
    - (void)prependObjects:(NSArray *)objects {
    
        if (!objects) return;
    
        NSUInteger i = 0;
    
        for (id obj in objects) {
    
            [self insertObject:obj atIndex:i++];
    
        }
    
    }

     4.NSTimer+YYAdd

    + (void)_yy_ExecBlock:(NSTimer *)timer {
        if ([timer userInfo]) {
            void (^block)(NSTimer *timer) = (void (^)(NSTimer *timer))[timer userInfo];
            block(timer);
        }
    }
    
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats {
        return [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(_yy_ExecBlock:) userInfo:[block copy] repeats:repeats];
    }
    
    + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats {
        return [NSTimer timerWithTimeInterval:seconds target:self selector:@selector(_yy_ExecBlock:) userInfo:[block copy] repeats:repeats];
    }

    5.NSNotificationCenter+YYAdd

    - (void)postNotificationOnMainThreadWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo waitUntilDone:(BOOL)wait {
        if (pthread_main_np()) return [self postNotificationName:name object:object userInfo:userInfo];
        NSMutableDictionary *info = [[NSMutableDictionary allocWithZone:nil] initWithCapacity:3];
        if (name) [info setObject:name forKey:@"name"];
        if (object) [info setObject:object forKey:@"object"];
        if (userInfo) [info setObject:userInfo forKey:@"userInfo"];
        [[self class] performSelectorOnMainThread:@selector(_yy_postNotificationName:) withObject:info waitUntilDone:wait];
    }

    6UIImage+YYAdd

    + (UIImage *)imageWithSize:(CGSize)size drawBlock:(void (^)(CGContextRef context))drawBlock {
        if (!drawBlock) return nil;
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        if (!context) return nil;
        drawBlock(context);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }

    7.UIControl+YYAdd

    - (NSMutableArray *)_yy_allUIControlBlockTargets {
        NSMutableArray *targets = objc_getAssociatedObject(self, &block_key);
        if (!targets) {
            targets = [NSMutableArray array];
            objc_setAssociatedObject(self, &block_key, targets, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }
        return targets;
    }
    
    - (void)removeAllTargets {
        [[self allTargets] enumerateObjectsUsingBlock: ^(id object, BOOL *stop) {
            [self removeTarget:object action:NULL forControlEvents:UIControlEventAllEvents];
        }];
        [[self _yy_allUIControlBlockTargets] removeAllObjects];
    }

    8.UIBarButtonItem+YYAdd

    #import "UIBarButtonItem+YYAdd.h"
    #import "YYCategoriesMacro.h"
    #import <objc/runtime.h>
    
    YYSYNTH_DUMMY_CLASS(UIBarButtonItem_YYAdd)
    
    
    static const int block_key;
    
    @interface _YYUIBarButtonItemBlockTarget : NSObject
    
    @property (nonatomic, copy) void (^block)(id sender);
    
    - (id)initWithBlock:(void (^)(id sender))block;
    - (void)invoke:(id)sender;
    
    @end
    
    @implementation _YYUIBarButtonItemBlockTarget
    
    - (id)initWithBlock:(void (^)(id sender))block{
        self = [super init];
        if (self) {
            _block = [block copy];
        }
        return self;
    }
    
    - (void)invoke:(id)sender {
        if (self.block) self.block(sender);
    }
    @property (nullable, nonatomic, copy) void (^actionBlock)(id);
    
    - (void)setActionBlock:(void (^)(id sender))block {
        _YYUIBarButtonItemBlockTarget *target = [[_YYUIBarButtonItemBlockTarget alloc] initWithBlock:block];
        objc_setAssociatedObject(self, &block_key, target, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        
        [self setTarget:target];
        [self setAction:@selector(invoke:)];
    }
    
    - (void (^)(id)) actionBlock {
        _YYUIBarButtonItemBlockTarget *target = objc_getAssociatedObject(self, &block_key);
        return target.block;
    }

     9.UIView+YYAdd

    - (void)removeAllSubviews {
        //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        while (self.subviews.count) {
            [self.subviews.lastObject removeFromSuperview];
        }
    }
    
    
    - (UIViewController *)viewController {
        for (UIView *view = self; view; view = view.superview) {
            UIResponder *nextResponder = [view nextResponder];
            if ([nextResponder isKindOfClass:[UIViewController class]]) {
                return (UIViewController *)nextResponder;
            }
        }
        return nil;
    }

    10UITableView+YYAdd

    - (void)clearSelectedRowsAnimated:(BOOL)animated {
        NSArray *indexs = [self indexPathsForSelectedRows];
        [indexs enumerateObjectsUsingBlock:^(NSIndexPath* path, NSUInteger idx, BOOL *stop) {
            [self deselectRowAtIndexPath:path animated:animated];
        }];
    }

    11UITextField+YYAdd

    - (void)selectAllText {
        UITextRange *range = [self textRangeFromPosition:self.beginningOfDocument toPosition:self.endOfDocument];
        [self setSelectedTextRange:range];
    }
    
    - (void)setSelectedRange:(NSRange)range {
        UITextPosition *beginning = self.beginningOfDocument;
        UITextPosition *startPosition = [self positionFromPosition:beginning offset:range.location];
        UITextPosition *endPosition = [self positionFromPosition:beginning offset:NSMaxRange(range)];
        UITextRange *selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
        [self setSelectedTextRange:selectionRange];
    }

    12

    + (CGFloat)screenScale {
        static CGFloat screenScale = 0.0;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            if ([NSThread isMainThread]) {
                screenScale = [[UIScreen mainScreen] scale];
            } else {
                dispatch_sync(dispatch_get_main_queue(), ^{
                    screenScale = [[UIScreen mainScreen] scale];
                });
            }
        });
        return screenScale;
    }
  • 相关阅读:
    TextBox类的成员
    【VB.Net】字节数与字符数的取得
    限制文本框的输入类型
    【转】FxCop 设计规则
    【Spread Sheet 应用(三)】Enter移向下一个单元格
    【ASP.NET】打开关闭新窗体
    常用字符对应ASCII码值
    初识CodeSmith分享两个模板
    【VB.NET】自定义控件(一)属性说明
    LeetCode: Length of Last Word
  • 原文地址:https://www.cnblogs.com/X-Bin/p/5598140.html
Copyright © 2011-2022 走看看