Last active
August 29, 2015 14:01
-
-
Save wangzz/de7c9d52bb4d7b29107b to your computer and use it in GitHub Desktop.
记录有用的代码片段
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
记录有用的代码片段 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it is a test file. |
系统版本判断:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
参考:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
swift查看底层:
swift -dump-ast swiftname
/Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -dump-ast a.swift
打印当前window的层次:
po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]
- iOS5及以上版本layout放在:
viewWillLayoutSubviews:
或
viewDidLayoutSubviews:
- iOS4系统layout放在:
layoutSubviews
计算文本区域:
+ (CGSize)sizeOfString:(NSString *)string font:(UIFont *)font size:(CGSize)size
{
CGSize stringSize = CGSizeZero;
if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
stringSize = [string sizeWithFont:font
constrainedToSize:size
lineBreakMode:UILineBreakModeWordWrap];
} else {
stringSize = [string boundingRectWithSize:size
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:font.fontDescriptor.fontAttributes
context:nil].size;
}
return stringSize;
}
更改UIToolBar背景色
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
toolBar.translucent = NO;
toolBar.barTintColor = [UIColor whiteColor];
}
四舍五入保留两位小数
float a = 3.456; //保留到小数点后两位
float b =(int)((a * 100) + 0.5) / 100.0;
//output b = 3.46;
不同view继承体系之间区域转换
例如一个视图控制器的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];
不同view继承体系之间点转换
- 把一个点从一个坐标系转换到接收者的坐标系
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
参数
point
一个视图中坐标系上的点
view
一个视图包含了点和他自身坐标系。如果是图是nil,那么这个方法将尝试转换基于窗口的坐标系。否则视图和那个接收者必须属于同一个UIWindow对象。
- 转换一个点从接收者坐标系到给定的视图坐标系
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
参数
point
一个在调用者坐标系中的点
view
一个包含了需要被转换的点的视图。如果视图是nil,那么这个方法将会转换成基于窗口的坐标。否则视图和接收者都要属于同一个UIWindow对象。
返回值
Block 使用方式
How Do I Declare A Block in Objective-C?
- As a local variable:
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
- As a property:
@property (nonatomic, copy) returnType (^blockName)(parameterTypes);
- As a method parameter:
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
- As an argument to a method call:
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
- As a typedef:
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
判断手机硬件型号:
参考:http://stackoverflow.com/questions/11197509/ios-iphone-get-device-model-and-make