1、获取window的三种方法
UIWindow *window = self.view.window ;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
2、Block的使用
(1)方法调用:
1.在.h里面(建立吧bock)和设置block的方法
typedef void(^BLOCK)(NSString *name);
-(void)setBlock:(BLOCK)block;(在.m属性需要实现)
2.在.m里面进行实现block方法和属性的转换
先利用BLOCK设置一个属性 BLOCK valueBlock;
-(void)setBlock:(BLOCK)block
{
valueBlock = block;
}
3.在点击的调用的方法里面进行调用
valueBlock(这里放一个字符串传到外面);
4.在外面调用
[类的对象 setBlock:^(NSString *name) {
这里进行执行block这个方法
}];
(2)属性调用
1.在.h里面设置2个字符串
@property(nonatomic,strong) void(^guwen)(NSString *paGe,NSString *name);
2.在.m里面点击调用
if (self.guwen) {
self.guwen(pageNumber,self.nameLabel.text);
}
3.在外面把值取出来
利用这个类的对象进行调用
累的对象.guwen = ^(NSString *paGe,NSString *name){
在此进行调用传值
};
3、Profile文件的设置
platform :ios,’iOS的系统版本号(自己看着写现在一般是8.0了)’
target “项目名称” do
pod 'AFNetworking', '~> 3.1.0'
pod 'SDWebImage', '~> 3.8.1'
end
4、尺寸的宏定义
宽高的设置
#define Screen_W [UIScreen mainScreen].bounds.size.width
#define Screen_H [UIScreen mainScreen].bounds.size.height
尺寸的设置
#define iPhone5AndEarlyDevice (([[UIScreen mainScreen] bounds].size.height*[[UIScreen mainScreen] bounds].size.width <= 320*568)?YES:NO)
#define Iphone6 (([[UIScreen mainScreen] bounds].size.height*[[UIScreen mainScreen] bounds].size.width <= 375*667)?YES:NO)
#define Size_ratio Screen_W/414 //(iPhone 6s plus基准尺寸) // 这个是按比例布局的
5、颜色的宏定义
//RGB颜色
#define JHColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
//随机色(和上面的保持 一致)
#define JHRandomColor JHColor(arc4random_uniform(256),arc4random_uniform(256),arc4random_uniform(256))
自定义随机色
#define JHRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
6、开发模式(名字自己起)
#ifdef DEBUG //处于开发阶段才打印(调试)
#define JHLog(...) NSLog(__VA_ARGS__)
#else //处于发布阶段(没有输出)(发布)
#define JHLog(...)
#endif
7、NSUserDefaults取值,存值
//取值
#define UserDefaultObject(A) [[NSUserDefaults standardUserDefaults]objectForKey:A]
//存值(可变的值不可以存)
#define UserDefaultSetValue(B,C) [[NSUserDefaults standardUserDefaults]setObject:B forKey:C]
//存BOOL值
#define UserDefaultBool(D,E) [[NSUserDefaults standardUserDefaults]setBool:D forKey:E]
8、弱引用
__weak typeof(self) weakSelf = self;
/*
弱引用
*/
#define STWeakSelf __weak typeof(self) weakSelf = self;
9、判断版本是ios几以后版本
// 这里以iOS7.0为例了
#define IOS_CURRENT_VERSION_IOS7 ([[UIDevice currentDevice].systemVersion floatValue] < 7.0 ? NO : YES)
10、打印函数,可以打印所在的函数,行数,以及你要打印的值
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
NSLog(@"==1==%s 2==[Line %d] 3==%@", __PRETTY_FUNCTION__, __LINE__,string);
//下边是我的示例
#define DLog(__LINE__,Content) NSLog(@"==1==%s 2==[Line %d] 3==%@", __PRETTY_FUNCTION__, __LINE__,Content)
11、设置导航栏文字的颜色以及文字大小(如导航的背景颜色,以及导航栏中间字体的颜色)
/*
设置导航栏文字的颜色以及文字大小(如导航的背景颜色,以及导航栏中间字体的颜色)
[UINavigationBar appearance];
设置Item的样式
[UIBarButtonItem appearance];
*/
//在第一次使用调用一次
+(void)initialize
{
//设置整个项目所有item的主题样式()
UIBarButtonItem *item = [UIBarButtonItem appearance];
//设置普通状态的字体颜色和大小
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:CWColor(123, 123, 123),NSFontAttributeName:[UIFont systemFontOfSize:20]} forState:UIControlStateNormal];
//设置点击的时候颜色和大小
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:CWColor(252, 108, 8),NSFontAttributeName:[UIFont systemFontOfSize:20]} forState:UIControlStateSelected];
}
12、导航栏上button图片防止被渲染
UIImage *img = [[UIImage imageNamed:@"icon_cog"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//宽度为负数的固定间距的系统item
UIBarButtonItem *rightNegativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[rightNegativeSpacer setWidth:-15];
UIBarButtonItem *rightBtnItem1 = [[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonItemClicked:)];
UIBarButtonItem *rightBtnItem2 = [[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonItemClicked:)];
self.navigationItem.rightBarButtonItems = @[rightNegativeSpacer,rightBtnItem1,rightBtnItem2];
13、NSData 转化我 string
/*
1.把 NSData 转化我 string
*/
NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
/*
2.把string 转化为 NSData
*/
NSData *data = [respondString dataUsingEncoding:NSUTF8StringEncoding]
//NSString转化为NSNumber
NSString *string = @"9000";
NSNumber *number = @([string integerValue]);
NSLog(@"number=%@",number);
//NSNumber转化为NSString
NSString *string1 = [NSString stringWithFormat:@"%@",number];
NSLog(@"string1=%@",string1);
14、导航栏的颜色
可以设置全局,也可以单独设置
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont systemFontOfSize:20]};
/颜色自己随便设置
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.101 green:0.705 blue:0.652 alpha:1.000];
15、自定义导航控制器上面的按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//设置图片的两种状态
//正常
[button setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
//选中
[button setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, 30, 30);
[button addTarget:self action:@selector(back1) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
16、对控件边框的处理(如宽度,颜色),自定义导航栏标题
对象(button,label...).layer.borderColor = [[UIColor blackColor] CGColor];
对象(button,label...).layer.borderWidth = 2.0f;
// 自定义导航栏标题
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
titleLabel.text = @"登陆";
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont systemFontOfSize:15];
self.navigationItem.titleView = titleLabel;
17、判断手机号码格式是否正确,利用正则表达式验证
+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
if (mobileNum.length != 11)
{
return NO;
}
/** * 手机号码:
* 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
* 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
* 联通号段: 130,131,132,155,156,185,186,145,176,1709
* 电信号段: 133,153,180,181,189,177,1700
*/
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$";
/** * 中国移动:China Mobile
* 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
*/
NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)";
/** * 中国联通:China Unicom
* 130,131,132,155,156,185,186,145,176,1709
*/
NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)";
/** * 中国电信:China Telecom
* 133,153,180,181,189,177,1700
*/
NSString *CT = @"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:mobileNum] == YES) || ([regextestcm evaluateWithObject:mobileNum] == YES) || ([regextestct evaluateWithObject:mobileNum] == YES) || ([regextestcu evaluateWithObject:mobileNum] == YES))
{
return YES;
}
else
{
return NO;
}
}
18、判断邮箱格式是否正确,利用正则表达式验证
+ (BOOL)isAvailableEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
19、 判断字符串中是否含有空格
+ (BOOL)isHaveSpaceInString:(NSString *)string
{
NSRange _range = [string rangeOfString:@" "];
if (_range.location != NSNotFound)
{
return YES;
}else
{
return NO;
}
}
20、判断字符串中是否含有中文
+ (BOOL)isHaveChineseInString:(NSString *)string
{
for(NSInteger i = 0; i < [string length]; i++)
{
int a = [string characterAtIndex:i];
if (a > 0x4e00 && a < 0x9fff)
{
return YES;
}
}
return NO;
}
21、判断字符串是否全部为数字
+ (BOOL)isAllNum:(NSString *)string
{
unichar c;
for (int i=0; i判断是否是纯数字
+ (BOOL)isPureInteger:(NSString *)str {
NSScanner *scanner = [NSScanner scannerWithString:str];
NSInteger val;
return [scanner scanInteger:&val] && [scanner isAtEnd];
}
22、 让iOS应用直接退出
+ (void)backOutApp
{
UIWindow *window = [[UIApplication sharedApplication].delegate window];
[UIView animateWithDuration:1.0f animations:^{
window.alpha = 0;
} completion:^(BOOL finished)
{
exit(0);
}];
}
23、NSArray 快速求总和、最大值、最小值、平均值
+ (NSString *)caculateArray:(NSArray *)array
{
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(@"%fn%fn%fn%f",sum,avg,max,min);
return [NSString stringWithFormat:@"%f",sum];
}
24、生成八位随机字符串(一般是RSA加密或者DES加密使用)
- (NSString *)shuffledAlphabet {
NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Get the characters into a C array for efficient shuffling
NSUInteger numberOfCharacters = [alphabet length];
unichar *characters = calloc(numberOfCharacters, sizeof(unichar));
[alphabet getCharacters:characters range:NSMakeRange(0, numberOfCharacters)];
// Perform a Fisher-Yates shuffle
for (NSUInteger i = 0; i < numberOfCharacters; ++i) {
NSUInteger j = (arc4random_uniform((float)numberOfCharacters - i) + i);
unichar c = characters[i];
characters[i] = characters[j];
characters[j] = c;
}
// Turn the result back into a string
NSString *result = [NSString stringWithCharacters:characters length:8];
free(characters);
return result;
}