有些方法经常在以前项目中找出来再拷贝进去,偶尔遇到u盘没带或者换台电脑,感觉自己都不会写了,这边开一篇来记录一下.一些方法网上也有,但忘记是转自哪里,只能说有雷同请勿怪.
颜色
/** 16进制颜色 */
+ (instancetype)BFColorWithHex:(uint32_t)hex;
/** 随机颜色 */
+ (instancetype)BFRandomColor;
/** rgb颜色 */
+ (instancetype)BFColorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue alpha:(uint8_t)alpha;
/** 以上方法实现 */
+ (instancetype)BFColorWithHex:(uint32_t)hex {
uint8_t red = (hex & 0xff0000) >> 16;
uint8_t green = (hex & 0x00ff00) >> 8;
uint8_t blue = hex & 0x0000ff;
return [self BFColorWithRed:red green:green blue:blue alpha:1.0];
}
+ (instancetype)BFRandomColor {
return [UIColor BFColorWithRed:arc4random_uniform(256) green:arc4random_uniform(256) blue:arc4random_uniform(256) alpha:1.0];
}
+ (instancetype)BFColorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue alpha:(uint8_t)alpha {
return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:alpha];
}
时间
/*
获取当前时间
到年:timeType = @"yyyy"
到月:timeType = @"yyyy-MM"
到天:timeType = @"yyyy-MM-dd"
到小时:timeType = @"yyyy-MM-dd hh"
到分:timeType = @"yyyy-MM-dd hh:mm"
到秒:timeType = @"yyyy-MM-dd hh:mm:ss"
*/
+(NSString *)getTimeDateWithType:(NSString *)timeType;
/**
根据日期获取礼拜几
传入时间格式要求yyyy-MM-dd
*/
+(NSString *)weekDayStringFromDate:(NSString *)date;
/** 以上方法实现 */
+(NSString *)getTimeDateWithType:(NSString *)timeType{
NSDate *date = [NSDate date];
NSTimeInterval interval = [date timeIntervalSinceNow];
NSDate *timeDate = [[NSDate alloc]initWithTimeIntervalSinceNow:interval];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:timeType];
NSString *currentTime = [dateFormat stringFromDate:timeDate];
return currentTime;
}
+(NSString *)weekDayStringFromDate:(NSString *)date{
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *inputDate = [dateFormatter dateFromString:date];
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
UIAlertController的title和message设置
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"认证失败" message:@"审核失败,可能是以下原因:\n1、个人姓名与身份证不对应\n2、手持照片不清晰\n3、身份证号不正确\n4、身份证假的,ps过,请重新提交认证信息" preferredStyle:UIAlertControllerStyleAlert];
UIView *subView1 = alert.view.subviews[0];
UIView *subView2 = subView1.subviews[0];
UIView *subView3 = subView2.subviews[0];
UIView *subView4 = subView3.subviews[0];
UIView *subView5 = subView4.subviews[0];
//取title和message:
UILabel *title = subView5.subviews[0];
UILabel *message = subView5.subviews[1];
title居中显示
title.textAlignment = NSTextAlignmentCenter;
message靠左显示
message.textAlignment = NSTextAlignmentLeft;
UIView添加手势
//手势方法
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickSearch:)];
//view添加手势
[bgSearchView addGestureRecognizer:tapGesture];
[tapGesture setNumberOfTapsRequired:1];