语言
// 当前语言
#define CURRENTLANGUAGE ([[NSLocale preferredLanguages] objectAtIndex:0])
## ****本地化
// 本地化字符串
/** NSLocalizedString宏做的其实就是在当前bundle中查找资源文件名“Localizable.strings”(参数:键+注释) */
#define LocalString(x, ...) NSLocalizedString(x, nil)
/** NSLocalizedStringFromTable宏做的其实就是在当前bundle中查找资源文件名“xxx.strings”(参数:键+文件名+注释) */
#define AppLocalString(x, ...) NSLocalizedStringFromTable(x, @"zjj", nil)
## ****打印
#ifdef DEBUG
#define DLog(fmt, ...) {NSLog((@"%s [Line %d]" fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);}
#else
#define DLog(...)
#endif
## ****设备及系统型号
// 判断是否 Retina屏、设备是否%fhone 5、是否是iPad
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// 判断设备的操做系统是不是ios7
#define IOS7 ([[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0]
// 判断设备的操做系统是不是ios8
#define IOS8 ([[[UIDevice currentDevice].systemVersion doubleValue] >= 8.0]
// 判断设备的操做系统是不是ios9
#define IOS9 ([[[UIDevice currentDevice].systemVersion doubleValue] >= 9.0]
// 判断当前设备是不是iPhone4或者4s
#define IPHONE4S (([[UIScreen mainScreen] bounds].size.height)==480)
// 判断当前设备是不是iPhone5
#define IPHONE5 (([[UIScreen mainScreen] bounds].size.height)==568)
// 判断当前设备是不是iPhone6
#define IPHONE6 (([[UIScreen mainScreen] bounds].size.height)==667)
// 判断当前设备是不是iPhone6Plus
#define IPHONE6_PLUS (([[UIScreen mainScreen] bounds].size.height)>=736)
// 获取当前屏幕的高度
#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
// 获取当前屏幕的宽度
#define kMainScreenWidth ([UIScreen mainScreen].applicationFrame.size.width)
// 判断是真机还是模拟器
#if TARGET_OS_IPHONE
// iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
// iPhone Simulator
#endif
## ****获取系统版本
// 获取系统版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
## ****获取****RGB****颜色
// 获取RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
## G - C - D****定义
// 延时GCD
#define dispatch_after(time, block) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
// 使用
dispatch_after(2, ^{
// 延时代码
});
// 主线程
#define dispatch_main_queue(block) dispatch_async(dispatch_get_main_queue(), block)
// 子线程
#define dispatch_global_queue(block) dispatch_async(dispatch_get_global_queue(0, 0), block)
## ****常量定义
static CGFloat const kLogoImageWidth = 100; //logo宽度
static CGFloat const kLogoImageHeight = 100; //logo宽度static CGFloat const kLogoImageY = 110;
static CGFloat const kBtnHeight = 40;
static CGFloat const kPadding = 30;
static CGFloat const kWeixinTopPadding = 15;
static CGFloat const kWeiboLoginBottom = 230;
#define kScaleSpace(designSpace) ((designSpace)(SCREEN_HEIGHT/667.0)) //根据iphone6 的设计稿计算缩放高度
static CGFloat const kBottomHeight = 50.0; //底部视图高度
static NSString *const CELL_TITLE_KEY = @"CELL_TITLE_KEY";
static NSString *const CELL_CONTENT_KEY = @"CELL_CONTENT_KEY";
补充说明:以上的类型常量替换宏的情况,只是适用于单个文件的情况。如果是多个文件共享的常量,苹果推荐的这样的方式:
* UserInfoModelConstants.h
extern NSString *const USER_AGE_KEY ;
extern NSString *const USER_TELPHONE_KEY ;
extern NSString *const USER_ADDRESS_KEY ;
extern NSString *const USER_BRIEF_KEY ;
* UserInfoModelConstants.m
NSString *const BKUSER_AGE_KEY = @"XXXXX.userAge";
NSString *const BKUSER_TELPHONE_KEY = @"XXXXX.telphoneNO";
NSString *const BKUSER_ADDRESS_KEY = @"XXXXX.address";
NSString *const BKUSER_BRIEF_KEY = @"XXXXX.brief";
## Block
// 无返回值block
//@property (nonatomic, copy) void (^editBlock)();
@property (nonatomic, copy) dispatch_block_t editBlock;
## ****打印****View****所有子视图
po [[self view]recursiveDescription]
## layoutSubviews****调用的调用时机
* 当视图第一次显示的时候会被调用
* 当这个视图显示到屏幕上了,点击按钮
* 添加子视图也会调用这个方法
* 当本视图的大小发生改变的时候是会调用的
* 当子视图的frame发生改变的时候是会调用的
* 当删除子视图的时候是会调用的
## NSString****过滤特殊字符
// 定义一个特殊字符的集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:
@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""];
// 过滤字符串的特殊字符
NSString *newString = [trimString stringByTrimmingCharactersInSet:set];
## TransForm****属性
//平移按钮
CGAffineTransform transForm = self.buttonView.transform;
self.buttonView.transform = CGAffineTransformTranslate(transForm, 10, 0);
//旋转按钮
CGAffineTransform transForm = self.buttonView.transform;
self.buttonView.transform = CGAffineTransformRotate(transForm, M_PI_4);
//缩放按钮
self.buttonView.transform = CGAffineTransformScale(transForm, 1.2, 1.2);
//初始化复位
self.buttonView.transform = CGAffineTransformIdentity;
## ****去掉分割线多余****15****像素
// 首先在viewDidLoad方法加入以下代码:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
// 然后在重写willDisplayCell方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
## ****计算方法耗时时间间隔
// 获取时间间隔
#define TICK CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define TOCK NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
## Alert****提示宏定义
#define Alert(_S_, ...) [[[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:(_S_), ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]
## ****让**** iOS ****应用直接退出
- (void)exitApplication {
AppDelegate *app = [UIApplication sharedApplication].delegate;
UIWindow *window = app.window;
[UIView animateWithDuration:1.0f animations:^{
window.alpha = 0;
} completion:^(BOOL finished) {
exit(0);
}];
}
## ****修改****Label****中不同文字颜色
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self editStringColor:self.label.text editStr:@"好" color:[UIColor blueColor]];
}
- (void)editStringColor:(NSString *)string editStr:(NSString *)editStr color:(UIColor *)color {
// string为整体字符串, editStr为需要修改的字符串
NSRange range = [string rangeOfString:editStr];
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:string];
// 设置属性修改字体颜色UIColor与大小UIFont
[attribute addAttributes:@{NSForegroundColorAttributeName:color} range:range];
self.label.attributedText = attribute;
}
## ****播放声音
#import<AVFoundation/AVFoundation.h>
// 1.获取音效资源的路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"pour_milk" ofType:@"wav"];
// 2.将路劲转化为url
NSURL *tempUrl = [NSURL fileURLWithPath:path];
// 3.用转化成的url创建一个播放器
NSError *error = nil;
AVAudioPlayer *play = [[AVAudioPlayer alloc]initWithContentsOfURL:tempUrl error:&error];
self.player = play;
// 4.播放
[play play];
## ****修改****Tabbar Item****的属性
// 修改标题位置
self.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -10);
// 修改图片位置
self.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0);
// 批量修改属性
for (UIBarItem *item in self.tabBarController.tabBar.items) {
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:19.0], NSFontAttributeName, nil]
forState:UIControlStateNormal];
}
// 设置选中和未选中字体颜色
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
//未选中字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateNormal];
//选中字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor cyanColor]} forState:UIControlStateSelected];
## ****去掉****BackBarButtonItem****的文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
## ****视图的生命周期
1、 alloc 创建对象,分配空间
2、 init (initWithNibName) 初始化对象,初始化数据
3、 loadView 从nib载入视图 ,除非你没有使用xib文件创建视图
4、 viewDidLoad 载入完成,可以进行自定义数据以及动态创建其他控件
5、 viewWillAppear视图将出现在屏幕之前,马上这个视图就会被展现在屏幕上了
6、 viewDidAppear 视图已在屏幕上渲染完成
1、viewWillDisappear 视图将被从屏幕上移除之前执行
2、viewDidDisappear 视图已经被从屏幕上移除,用户看不到这个视图了
3、dealloc 视图被销毁,此处需要对你在init和viewDidLoad中创建的对象进行释放.
viewVillUnload- 当内存过低,即将释放时调用;
viewDidUnload-当内存过低,释放一些不需要的视图时调用。
## ****应用程序的生命周期
1,启动但还没进入状态保存 :
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2,基本完成程序准备开始运行:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
3,当应用程序将要入非活动状态执行,应用程序不接收消息或事件,比如来电话了:
- (void)applicationWillResignActive:(UIApplication *)application
4,当应用程序入活动状态执行,这个刚好跟上面那个方法相反:
- (void)applicationDidBecomeActive:(UIApplication *)application
5,当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可:
- (void)applicationDidEnterBackground:(UIApplication *)application
6,当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反:
- (void)applicationWillEnterForeground:(UIApplication *)application
7,当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作:
- (void)applicationWillTerminate:(UIApplication *)application
## ****判断****view****是不是指定视图的子视图
BOOL isView = [textView isDescendantOfView:self.view];
## ****页面强制横屏
#pragma mark - 强制横屏代码
- (BOOL)shouldAutorotate{
//是否支持转屏
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
//支持哪些转屏方向
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)prefersStatusBarHidden{
return NO;
}
## ****系统键盘通知消息
1、UIKeyboardWillShowNotification - 将要弹出键盘
2、UIKeyboardDidShowNotification - 显示键盘
3、UIKeyboardWillHideNotification - 将要隐藏键盘
4、UIKeyboardDidHideNotification - 键盘已经隐藏
5、UIKeyboardWillChangeFrameNotification - 键盘将要改变frame
6、UIKeyboardDidChangeFrameNotification - 键盘已经改变frame
## ****关闭****navigationController****的滑动返回手势
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
## ****设置状态栏为任意的颜色
- (void)setStatusColor
{
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
statusBarView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:statusBarView];
}
## ****让****Xcode****的控制台支持****LLDB****类型的打印
打开终端输入三条命令:
touch ~/.lldbinit
echo display @import UIKit >> ~/.lldbinit
echo target stop-hook add -o \"target stop-hook disable\" >> ~/.lldbinit
下次重新运行项目,然后就不报错了。
## Label****行间距
-(void)test{
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:3];
//调整行间距
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [self.contentLabel.text length])];
self.contentLabel.attributedText = attributedString;
}
## UIImageView****填充模式
@"UIViewContentModeScaleToFill", // 拉伸自适应填满整个视图
@"UIViewContentModeScaleAspectFit", // 自适应比例大小显示
@"UIViewContentModeScaleAspectFill", // 原始大小显示
@"UIViewContentModeRedraw", // 尺寸改变时重绘
@"UIViewContentModeCenter", // 中间
@"UIViewContentModeTop", // 顶部
@"UIViewContentModeBottom", // 底部
@"UIViewContentModeLeft", // 中间贴左
@"UIViewContentModeRight", // 中间贴右
@"UIViewContentModeTopLeft", // 贴左上
@"UIViewContentModeTopRight", // 贴右上
@"UIViewContentModeBottomLeft", // 贴左下
@"UIViewContentModeBottomRight", // 贴右下
网友评论