1.left和leading區(qū)別
NSLayoutAttributeLeft
和NSLayoutAttributeRight
代表從左右進(jìn)行布局
NSLayoutAttributeLeading和
NSLayoutAttributeTrailing代表從前后進(jìn)行布局(開始到結(jié)束的意思) 在中國(guó)的
NSLayoutAttributeLeft 和 NSLayoutAttributeLeading
是一個(gè)效果的,布局習(xí)慣從左到右—>
但在有些國(guó)家地區(qū)患民,NSLayoutAttributeRight
和NSLayoutAttributeLeading
是一個(gè)效果缩举,布局習(xí)慣從右往左<---
使用推薦:NSLayoutAttributeLeading
和NSLayoutAttributeTrailing
(比較常用)
2.block中修改時(shí),不需要__block情況
NSMutableArray *testArr =[[NSMutableArray alloc] initWithObjects:@"1",@"2", nil];
__block NSInteger a=10;
/**結(jié)論:在block里面修改局部變量的值都要用__block修飾**/
void (^TestBlock)(void) = ^{
// NSMutableArray *temArr=[[NSMutableArray alloc] init];
// testArr=temArr;//testArr數(shù)組的指針發(fā)生改變時(shí)匹颤,testArr要添加__block修飾
a=100;//a的值發(fā)生改變仅孩,則要求添加__block修飾
// testArr不需要聲明成__block,因?yàn)閠estArr數(shù)組的指針并沒(méi)有變(往數(shù)組里面添加對(duì)象印蓖,指針是沒(méi)變的辽慕,只是指針指向的內(nèi)存里面的內(nèi)容變了)
[testArr addObject:[NSString stringWithFormat:@"3"]];
NSLog(@"_block testArr :%@ a:%d", testArr,a);
};
a=0;
TestBlock();
NSLog(@"testArr :%@ a:%d", testArr,a);
NSURL轉(zhuǎn)化NSString 、NSMutableArray轉(zhuǎn)化NSArray
NSURL轉(zhuǎn)化NSString
NSURL *url=····
NSString *str=[url absoluteString];
NSMutableArray轉(zhuǎn)化NSArray:
NSMutableArray *list=····
NSArray *list=[list mutableCopy];
4.數(shù)組深拷貝
NSMutableArray *arr1=[[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", nil];
NSMutableArray *arr2=[[NSMutableArray alloc] init];
arr2=[arr1 mutableCopy];
[arr1 removeObject:@"b"];
//結(jié)果arr1:a,c
//arr2:a,b,c
5.CGRectOffset 的作用
相對(duì)于源矩形原點(diǎn)(左上角的點(diǎn))沿x軸和y軸偏移 赦肃,例如:
//view沿著(0溅蛉,0)x軸向右移動(dòng)260個(gè)像素
[self.view setFrame:CGRectOffset(self.view.frame, 260, 0)];
6.convertRect 坐標(biāo)轉(zhuǎn)換
使用 convertRect: fromView: 或者 convertRect: toView:
例如一個(gè)視圖控制器的view中有一個(gè)UITableView,UITableView的某個(gè)cell中有個(gè)UITextField他宛,想要得到UITextField在view中的位置船侧,就要靠上面的兩個(gè)方法了。
用
CGRect rect = [self.view convertRect:textField.frame fromView:textField.superview];
或者
CGRect rect = [textField.superview convertRect:textField.frame toView:self.view];
進(jìn)行轉(zhuǎn)換都可以堕汞。
%m.nf
m好像是整個(gè)數(shù)據(jù)的寬度勺爱,n是小數(shù)點(diǎn)后的位數(shù)。
%2f 保留2位有效位數(shù)讯检,不足2位琐鲁,補(bǔ)0;1.234 -> %2f 為1.2
%.2f 保留到小數(shù)點(diǎn)后2位輸出 人灼;1.2 -> %.2f 為1.20
8.M_PI 180度; M_PI_4 45度;
iOS的變換函數(shù)使用弧度而不是角度作為單位围段。弧度用數(shù)學(xué)常量pi的倍數(shù)表示投放,一個(gè)pi代表180度奈泪,所以四分之一的pi就是45度。
9.modf函數(shù)計(jì)算出float或double型的整數(shù)小數(shù)部分
10.(1)User Defined Runtime Attributes 能夠配置一些在interface builder 中不能配置的屬性
(2)如果要配置CALayer的 border coloer 和 shadow color,他們都是CGColorRef類型的涝桅,并不能直接在User Defined Runtime Attributes進(jìn)行配置拜姿,但請(qǐng)看解決方案:
為了兼容CALayer 的KVC ,你得用分類重寫CALayer的borderColorFromUIColor的setter:
@implementation CALayer (Additions)
- (void)setBorderColorFromUIColor:(UIColor *)color{
self.borderColor = color.CGColor;
}
(3)當(dāng)然所選控件也可以是自定制的customView冯遂,原生控件可以用runtime定制新的屬性
11.UITableView的Group樣式下頂部底部空白處理
self.tableView.tableHeaderView = [UIView new];
self.tableView.tableFooterView = [UIView new];
12.獲取某個(gè)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.取圖片某一像素點(diǎn)的顏色 在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.禁止鎖屏蕊肥,
默認(rèn)情況下,當(dāng)設(shè)備一段時(shí)間沒(méi)有觸控動(dòng)作時(shí)蛤肌,iOS會(huì)鎖住屏幕壁却。但有一些應(yīng)用是不需要鎖屏的,比如視頻播放器裸准。
[UIApplication sharedApplication].idleTimerDisabled = YES;
或
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
16.模態(tài)推出透明界面
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調(diào)試不顯示內(nèi)存占用
editSCheme 里面有個(gè)選項(xiàng)叫叫做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 NO
19.iOS 獲取漢字的拼音
+ (NSString *)transform:(NSString *)chinese
{
//將NSString裝換成NSMutableString
NSMutableString *pinyin = [chinese mutableCopy];
//將漢字轉(zhuǎn)換為拼音(帶音標(biāo))
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
NSLog(@"%@", pinyin);
//去掉拼音的音標(biāo)
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
NSLog(@"%@", pinyin);
//返回最近結(jié)果
return pinyin;
}
20.手動(dòng)更改iOS狀態(tài)欄的顏色
- (void)setStatusBarBackgroundColor:(UIColor *)color
{
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)])
{
statusBar.backgroundColor = color;
}
}
21.判斷當(dāng)前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.獲取實(shí)際使用的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.在當(dāng)前屏幕獲取第一響應(yīng)
UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView * firstResponder = [keyWindow performSelector:@selector(firstResponder)];
24.判斷對(duì)象是否遵循了某協(xié)議
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.關(guān)于NSDateFormatter的格式
G: 公元時(shí)代展东,例如AD公元
yy: 年的后2位
yyyy: 完整年
MM: 月,顯示為1-12
MMM: 月炒俱,顯示為英文月份簡(jiǎn)寫,如 Jan
MMMM: 月盐肃,顯示為英文月份全稱,如 Janualy
dd: 日向胡,2位數(shù)表示恼蓬,如02
d: 日,1-2位顯示僵芹,如 2
EEE: 簡(jiǎn)寫星期幾处硬,如Sun
EEEE: 全寫星期幾,如Sunday
aa: 上下午拇派,AM/PM
H: 時(shí)荷辕,24小時(shí)制,0-23
K:時(shí)件豌,12小時(shí)制疮方,0-11
m: 分,1-2位
mm: 分茧彤,2位
s: 秒骡显,1-2位
ss: 秒,2位
S: 毫秒
29.runtime獲取一個(gè)類的所有子類
+ (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.阿拉伯?dāng)?shù)字轉(zhuǎn)中文格式
+(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 = @[@"個(gè)",@"十",@"百",@"千",@"萬(wàn)",@"十",@"百",@"千",@"億",@"十",@"百",@"千",@"兆"];
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;
}