读取本地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:
出现如下界面,编辑名称点击 Rename 即可:
iPhone 通讯录中的手机号复制粘贴长度为 15, 去除空字符串后为13,但显示正确为11位数字:因存在不会显示的Unicode码导致长度与显示字符不符,去除Unicode码代码如下
[str stringByReplacingOccurrencesOfString:@"\\p{Cf}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, str.length)];