1.left和leading区别 NSLayoutAttributeLeft 和NSLayoutAttributeRight代表从左右进行布局 NSLayoutAttributeLeading和NSLayoutAttributeTrailing代表从前后进行布局(开始到结束的意思) 在中国的NSLayoutAttributeLeft 和 NSLayoutAttributeLeading 是一个效果的,布局习惯从左到右—> 但在有些国家地区,NSLayoutAttributeRight和NSLayoutAttributeLeading 是一个效果,布局习惯从右往左<— 使用推荐:NSLayoutAttributeLeading和NSLayoutAttributeTrailing(比较常用)
2.block中修改时,不需要__block情况
NSMutableArray *testArr =[[NSMutableArray alloc] initWithObjects:@"1",@"2", nil]; __block NSInteger a=10; /**结论:在block里面修改局部变量的值都要用__block修饰**/ void (^TestBlock)(void) = ^{ // NSMutableArray *temArr=[[NSMutableArray alloc] init]; // testArr=temArr;//testArr数组的指针发生改变时,testArr要添加__block修饰 a=100;//a的值发生改变,则要求添加__block修饰 // testArr不需要声明成__block,因为testArr数组的指针并没有变(往数组里面添加对象,指针是没变的,只是指针指向的内存里面的内容变了) [testArr addObject:[NSString stringWithFormat:@"3"]]; NSLog(@"_block testArr :%@ a:%d", testArr,a); }; a=0; TestBlock(); NSLog(@"testArr :%@ a:%d", testArr,a);3.
NSURL转化NSString 、NSMutableArray转化NSArray NSURL转化NSString NSURL *url=···· NSString *str=[url absoluteString]; NSMutableArray转化NSArray: NSMutableArray *list=···· NSArray *list=[list mutableCopy];4.数组深拷贝
NSMutableArray *arr1=[[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", nil]; NSMutableArray *arr2=[[NSMutableArray alloc] init]; arr2=[arr1 mutableCopy]; [arr1 removeObject:@"b"]; //结果arr1:a,c //arr2:a,b,c5.CGRectOffset 的作用 相对于源矩形原点(左上角的点)沿x轴和y轴偏移 ,例如:
//view沿着(0,0)x轴向右移动260个像素 [self.view setFrame:CGRectOffset(self.view.frame, 260, 0)];6.convertRect 坐标转换
使用 convertRect: fromView: 或者 convertRect: toView: 例如一个视图控制器的view中有一个UITableView,UITableView的某个cell中有个UITextField,想要得到UITextField在view中的位置,就要靠上面的两个方法了。 用 CGRect rect = [self.view convertRect:textField.frame fromView:textField.superview]; 或者 CGRect rect = [textField.superview convertRect:textField.frame toView:self.view]; 进行转换都可以。7.
%m.nf m好像是整个数据的宽度,n是小数点后的位数。 %2f 保留2位有效位数,不足2位,补0;1.234 -> / 为1.2 %.2f 保留到小数点后2位输出 ;1.2 -> %.2f 为1.208.M_PI 180度; M_PI_4 45度; iOS的变换函数使用弧度而不是角度作为单位。弧度用数学常量pi的倍数表示,一个pi代表180度,所以四分之一的pi就是45度。
9.modf函数计算出float或double型的整数小数部分
10.(1)User Defined Runtime Attributes 能够配置一些在interface builder 中不能配置的属性  (2)如果要配置CALayer的 border coloer 和 shadow color,他们都是CGColorRef类型的,并不能直接在User Defined Runtime Attributes进行配置,但请看解决方案: 为了兼容CALayer 的KVC ,你得用分类重写CALayer的borderColorFromUIColor的setter:
@implementation CALayer (Additions) - (void)setBorderColorFromUIColor:(UIColor *)color{ self.borderColor = color.CGColor; }(3)当然所选控件也可以是自定制的customView,原生控件可以用runtime定制新的属性
11.UITableView的Group样式下顶部底部空白处理
self.tableView.tableHeaderView = [UIView new]; self.tableView.tableFooterView = [UIView new];12.获取某个view所在的控制器
- (UIViewController *)viewController { UIViewController *viewController = nil; UIResponder *next = self.nextResponder; while (next) { if ([next isKindOfClass:[UIViewController class]]) { viewController = (UIViewController *)next; break; } next = next.nextResponder; } return viewController; }13.两种方法删除NSUserDefaults所有记录
//方法一 NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain]; //方法二 - (void)resetDefaults { NSUserDefaults * defs = [NSUserDefaults standardUserDefaults]; NSDictionary * dict = [defs dictionaryRepresentation]; for (id key in dict) { [defs removeObjectForKey:key]; } [defs synchronize]; }14.取图片某一像素点的颜色 在UIImage的分类中
- (UIColor *)colorAtPixel:(CGPoint)point { if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) { return nil; } CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); int bytesPerPixel = 4; int bytesPerRow = bytesPerPixel * 1; NSUInteger bitsPerComponent = 8; unsigned char pixelData[4] = {0, 0, 0, 0}; CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextSetBlendMode(context, kCGBlendModeCopy); CGContextTranslateCTM(context, -point.x, point.y - self.size.height); CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), self.CGImage); CGContextRelease(context); CGFloat red = (CGFloat)pixelData[0] / 255.0f; CGFloat green = (CGFloat)pixelData[1] / 255.0f; CGFloat blue = (CGFloat)pixelData[2] / 255.0f; CGFloat alpha = (CGFloat)pixelData[3] / 255.0f; return [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; }15.禁止锁屏,
默认情况下,当设备一段时间没有触控动作时,iOS会锁住屏幕。但有一些应用是不需要锁屏的,比如视频播放器。
[UIApplication sharedApplication].idleTimerDisabled = YES; 或 [[UIApplication sharedApplication] setIdleTimerDisabled:YES];16.模态推出透明界面
UIViewController *vc = [[UIViewController alloc] init]; UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:vc]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { na.modalPresentationStyle = UIModalPresentationOverCurrentContext; } else { self.modalPresentationStyle=UIModalPresentationCurrentContext; } [self presentViewController:na animated:YES completion:nil];17.Xcode调试不显示内存占用 editSCheme 里面有个选项叫叫做enable zoombie Objects 取消选中
18.显示隐藏文件,终端命令
//显示 defaults write com.apple.finder AppleShowAllFiles -bool true 或 defaults write com.apple.finder AppleShowAllFiles YES //隐藏 defaults write com.apple.finder AppleShowAllFiles -bool false 或 defaults write com.apple.finder AppleShowAllFiles NO19.iOS 获取汉字的拼音
+ (NSString *)transform:(NSString *)chinese { //将NSString装换成NSMutableString NSMutableString *pinyin = [chinese mutableCopy]; //将汉字转换为拼音(带音标) CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO); NSLog(@"%@", pinyin); //去掉拼音的音标 CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO); NSLog(@"%@", pinyin); //返回最近结果 return pinyin; }20.手动更改iOS状态栏的颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; } }21.判断当前ViewController是push还是present的方式显示的
NSArray *viewcontrollers=self.navigationController.viewControllers; if (viewcontrollers.count > 1) { if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) { //push方式 [self.navigationController popViewControllerAnimated:YES]; } } else { //present方式 [self dismissViewControllerAnimated:YES completion:nil]; }22.获取实际使用的LaunchImage图片
- (NSString *)getLaunchImageName { CGSize viewSize = self.window.bounds.size; // 竖屏 NSString *viewOrientation = @"Portrait"; NSString *launchImageName = nil; NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"]; for (NSDictionary* dict in imagesDict) { CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]); if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) { launchImageName = dict[@"UILaunchImageName"]; } } return launchImageName; }23.在当前屏幕获取第一响应
UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow]; UIView * firstResponder = [keyWindow performSelector:@selector(firstResponder)];24.判断对象是否遵循了某协议
if ([self.selectedController conformsToProtocol:@protocol(RefreshPtotocol)]) { [self.selectedController performSelector:@selector(onTriggerRefresh)]; }25.判断view是不是指定视图的子视图
BOOL isView = [textView isDescendantOfView:self.view];26.NSArray 快速求总和 最大值 最小值 和 平均值
NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil]; CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue]; CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue]; CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue]; CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue]; NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);27.修改UITextField中Placeholder的文字颜色
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];28.关于NSDateFormatter的格式
G: 公元时代,例如AD公元 yy: 年的后2位 yyyy: 完整年 MM: 月,显示为1-12 MMM: 月,显示为英文月份简写,如 Jan MMMM: 月,显示为英文月份全称,如 Janualy dd: 日,2位数表示,如02 d: 日,1-2位显示,如 2 EEE: 简写星期几,如Sun EEEE: 全写星期几,如Sunday aa: 上下午,AM/PM H: 时,24小时制,0-23 K:时,12小时制,0-11 m: 分,1-2位 mm: 分,2位 s: 秒,1-2位 ss: 秒,2位 S: 毫秒29.runtime获取一个类的所有子类
+ (NSArray *) allSubclasses { Class myClass = [self class]; NSMutableArray *mySubclasses = [NSMutableArray array]; unsigned int numOfClasses; Class *classes = objc_copyClassList(&numOfClasses;); for (unsigned int ci = 0; ci < numOfClasses; ci++) { Class superClass = classes[ci]; do{ superClass = class_getSuperclass(superClass); } while (superClass && superClass != myClass); if (superClass) { [mySubclasses addObject: classes[ci]]; } } free(classes); return mySubclasses; }30.阿拉伯数字转中文格式
+(NSString *)translation:(NSString *)arebic { NSString *str = arebic; NSArray *arabic_numerals = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"]; NSArray *chinese_numerals = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"零"]; NSArray *digits = @[@"个",@"十",@"百",@"千",@"万",@"十",@"百",@"千",@"亿",@"十",@"百",@"千",@"兆"]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chinese_numerals forKeys:arabic_numerals]; NSMutableArray *sums = [NSMutableArray array]; for (int i = 0; i < str.length; i ++) { NSString *substr = [str substringWithRange:NSMakeRange(i, 1)]; NSString *a = [dictionary objectForKey:substr]; NSString *b = digits[str.length -i-1]; NSString *sum = [a stringByAppendingString:b]; if ([a isEqualToString:chinese_numerals[9]]) { if([b isEqualToString:digits[4]] || [b isEqualToString:digits[8]]) { sum = b; if ([[sums lastObject] isEqualToString:chinese_numerals[9]]) { [sums removeLastObject]; } }else { sum = chinese_numerals[9]; } if ([[sums lastObject] isEqualToString:sum]) { continue; } } [sums addObject:sum]; } NSString *sumStr = [sums componentsJoinedByString:@""]; NSString *chinese = [sumStr substringToIndex:sumStr.length-1]; NSLog(@"%@",str); NSLog(@"%@",chinese); return chinese; }