部分知识点记录(一)

读取本地json文件

    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

判断两个数组是否相等,顺序不考虑

- (BOOL)isTwoArrayEqualWithOneArray:(NSArray *)oneArray otherArray:(NSArray *)otherArray {
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", oneArray];
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", otherArray];
    if (([otherArray filteredArrayUsingPredicate:predicate1].count > 0) || ([oneArray filteredArrayUsingPredicate:predicate2].count > 0)) {
        return NO;
    }
    return YES;
}

为UIView添加虚线框

- (void)addBorder {
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.bounds = self.bounds;
    borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:2.0].CGPath;
    borderLayer.lineWidth = 0.5;
    //虚线边框
    borderLayer.lineDashPattern = @[@8, @4];
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = SAColorByRGB(220, 220, 220).CGColor;
    [self.layer addSublayer:borderLayer];
}

截图

- (UIImage *)imageForCutScreen{
    UIGraphicsBeginImageContext(self.frame.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenImage;
}

runtime打印属性、变量

    typedef struct objc_ivar *Ivar;
    unsigned int count;
    Ivar *ivars = class_copyIvarList([UIPickerView class], &count);
    for (int i = 0; i < count; i++) {
        const char *ivarName = ivar_getName(ivars[i]);
        NSString *str = [NSString stringWithCString:ivarName encoding:NSUTF8StringEncoding];
        NSLog(@"ivarName : %@", str);
    }
    
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList([UIView class], &count);
    for (int i = 0; i < count; i++) {
        const char *propertiesName = property_getName(properties[i]);
        NSString *str = [NSString stringWithCString:propertiesName encoding:NSUTF8StringEncoding];
        NSLog(@"propertyName : %@", str);
    }

改变图标颜色

//写在UIImage的类别中的
- (UIImage *)imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    [tintColor setFill];
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    UIRectFill(bounds);
    
    [self drawInRect:bounds blendMode:blendMode alpha:1.0f];
    
    if (blendMode != kCGBlendModeDestinationIn) {
        [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
    }
    
    UIImage *tintImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return tintImage;
}

获取当前控制器

- (UIViewController*)topMostController
{
    UIViewController *topController = [self rootViewController];
    
    //  Getting topMost ViewController
    while ([topController presentedViewController]) topController = [topController presentedViewController];
    
    //  Returning topMost ViewController
    return topController;
}

- (UIViewController*)currentViewController;
{
    UIViewController *currentViewController = [self topMostController];
    
    while ([currentViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)currentViewController topViewController])
        currentViewController = [(UINavigationController*)currentViewController topViewController];
    
    return currentViewController;
}

UITextField关闭系统自动联想和首字母大写功能

//clearBUtton偏移
[_textField setValue:[NSValue valueWithCGPoint:CGPointMake(-10 - 15*kSACardWidthRatio(), 0)] forKeyPath:@"_clearButtonOffset"];
//不联想
[_txtField setAutocorrectionType:UITextAutocorrectionTypeNo];
//首字母不大写
[_txtField setAutocapitalizationType:UITextAutocapitalizationTypeNone];

textField clearButton图片替换

- (void)changeClearButtonTintImageWithTextField:(UITextField *)textField {
    UIButton *clearButton = (UIButton *)[textField valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"clearButton"] forState:UIControlStateNormal];
    [clearButton setImage:[UIImage imageNamed:@"clearButton"] forState:UIControlStateHighlighted];
}

xcode 修改类名

打开类的 .h 文件,将光标移至类名处右键 Refactor -> Rename:


Refactor -> Rename.png

出现如下界面,编辑名称点击 Rename 即可:


Rename.png

iPhone 通讯录中的手机号复制粘贴长度为 15, 去除空字符串后为13,但显示正确为11位数字:因存在不会显示的Unicode码导致长度与显示字符不符,去除Unicode码代码如下

[str stringByReplacingOccurrencesOfString:@"\\p{Cf}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, str.length)];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 面向对象主要针对面向过程。 面向过程的基本单元是函数。 什么是对象:EVERYTHING IS OBJECT(万物...
    sinpi阅读 4,776评论 0 4
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,326评论 1 14
  • passwordTextField.secureTextEntry = YES; // 输入密码 安全样式 //初...
    失忆的程序员阅读 3,757评论 0 2
  • 工欲善其事必先利其器,作为PC客户端开发,Visual Studio是我们每天都要使用的开发工具,IDE提供了非常...
    小猪啊呜阅读 10,123评论 1 10
  • 带 您 走 进 鸿 安 湖南鸿安医药有限公司成立于2006年12月,是一家医药批发企业。面向全...
    吴莉博宇阅读 2,806评论 0 1