PS:
如果有多次属性添加,只能先分割成多个部分再添加属性了。
<#code#>
这个代码在xcode上只会显示成code,但会有提示code,需要把code替换成真实的代码块常用宏
//屏幕尺寸
#define is3_5Inch ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960),[[UIScreen mainScreen] currentMode].size) : NO)
#define is4_0Inch ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136),[[UIScreen mainScreen] currentMode].size) : NO)
#define is4_7Inch ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334),[[UIScreen mainScreen] currentMode].size) : NO)
#define is5_5Inch ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208),[[UIScreen mainScreen] currentMode].size) : NO)
//oc语言
#ifdef __OBJC__
#endif
//模拟器和真机判断
#if TARGET_IPHONE_SIMULATOR//是模拟器
#elif TARGET_OS_IPHONE//是真机
#endif
#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#define debugMethod() NSLog(@"%s", __func__)
#else
#define NSLog(...)
#define debugMethod()
#endif
定时器
1.CADisplayLink
//基本使用
//初始化
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
//添加到运行循环
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
//停止,从运行循环移除
[link invalidate];
2.NSTimer
//基本使用
//初始化定时器
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
//启动定时器
[timer fire];
//停止定时器
[timer invalidate];
单例singletion
+(__Class__ *)shareInstance
{
static __Class__ *__singletion;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__singletion=[[self alloc] init];
});
return __singletion;
}
绘图
<#view#>.layer.masksToBounds = YES; //没这句话它圆不起来
<#view#>.layer.cornerRadius = 4.0; //设置图片圆角的大小
键盘
//寻找第一响应者
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
//退出键盘
//在不知道第一响应者的情况下取消键盘方法
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
app 跳转 与 URL Scheme
//检测APP是否安装 只要调用下面这个方法就可以
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"appurlscheme://"]
AVFoundation
//设置扩音器
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);
NSDictionary
//如何判断NSDictionary是否包含某个键
//方法一:
if ([[dictionary allKeys] containsObject:key) {
// contains key
}
//方法二:
if ([dictionary objectForKey:key]) { //objectForKey will return nil if a key doesn't exists.
// contains key
}
NSLog中文输出
//NSLog中文输出
//备注:__FILE__为当前的编译文件名(假设这里target中间有中文),它是一个字符串
//代码段1
NSLog(@"%@",[[NSString alloc] initWithUTF8String:__FILE__]);
//代码段2
NSLog(@"%@",[[NSString alloc] initWithCString:__FILE__ encoding:NSUTF8StringEncoding]);
//代码段3
NSLog(@"%@",[[NSString alloc] initWithBytes:&__FILE__ length: sizeof(__FILE__) encoding:NSUTF8StringEncoding]);
UINavigation
//隐藏导航栏
//viewWillAppear中添加
self.navigationController.navigationBarHidden = YES;
UITableView
//取消分割线
<#tableView#>.separatorStyle = UITableViewCellSeparatorStyleNone;
UITableViewCell
//取消选中状态
<#UITableViewCell#>.selectionStyle = UITableViewCellSelectionStyleNone;
//多选
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//当前选中的cell
Custom *cell = (Custom*)[tableView cellForRowAtIndexPath:indexPath];
}
//右边箭头
<#UITableViewCell#>.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
UITextField
//密码模式
<#uitextfield#>.secureTextEntry = YES;
UIWebView
/**
* webview的403、404错误处理
*
* @param webView 当前webview
* @param request 当前request
* @param navigationType 当前navigationType(正如其名称,表示导航类型向前,向后还是刷新等等)
*
* @return 是否加载网页
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
static BOOL isRequestWeb = YES;
if (isRequestWeb) {
NSHTTPURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
if (response.statusCode == 404) {
// code for 404
return NO;
} else if (response.statusCode == 403) {
// code for 403
return NO;
}
[webView loadData:data MIMEType:@"text/html" textEncodingName:nil baseURL:[request URL]];
isRequestWeb = NO;
return NO;
}
return YES;
}
数据类型转换
NSDate与NSString
a、NSDate转NSString/格式化字符串
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置日期格式
formatter.dateFormat = @"HH:mm";
// yyyy:年 MM:月 dd:日 hh:时 mm:分 ss:秒 Z代表时区
// 将日期转换为指定格式的字符串
NSString *dateStr = [formatter stringFromDate:datePicker.date];
NSLog(@"%@",dateStr);
/*
字母 日期或时间元素 表示 示例
G Era 标志符 Text AD
y 年 Year 1996 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区(General time zone,Pacific Standard Time) PST; GMT-08:00
Z 时区 RFC 822 time zone -0800
*/
b、NSString转NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// 设置日期格式
dateFormatter.dateFormat = @"YYYY-MM-dd";
NSDate *date = [dateFormatter dateFromString:@"1970-01-14"];
NSString与基本C语言数据类型
a.NSString转基本数据类型
[@"2" intValue];
b.基本数据类型转NSString
[NSString stringWithFormat:@"%d",8]
NSMutableAttributedString 只能添加两次属性问题
如果有多次属性添加,只能先分割成多个部分再添加属性了。
评论
发表评论