IOS - 常用宏定义和功能方法
可能不定期添加新的东西
github地址:https://github.com/yuqingzhude/CommonUseDemo /************************Tools************************/ /** 存网络请求参数或者其他类似key value对应的字典*/
@property (nonatomic,strong) NSDictionary *keyValueDic; + (instancetype)sharedCommonUse; /** 获取当前时间*/
+ (NSString *)getCurrentTime; /** 将日期格式转化成字符串*/
+(NSString*)getFormatedTime:(NSDate*)date; /** 改变图片大小*/
+(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize; /** 拨打电话*/
+ (void)callSomebody:(NSString *)tel; /** 字符串是否为数字*/
+ (BOOL)ifThisStringIsAnNumber:(NSString *)str; /** 字符串是否为整形*/
+ (BOOL)isPureInt:(NSString*)string; /** 判断是否为浮点形*/
+ (BOOL)isPureFloat:(NSString*)string; /** 从栈中将要移动vc向前移动index个位置,中间的vc将被释放*/
//ex:pay success pop start vc
+ (void)moveForwardTopVC:(UIViewController *)vc ForwardIndex:(NSInteger)index; /** pop到指定名字的VC,没有index可能变化的引起的问题*/
+ (void)popToViewControllerWithVC:(UIViewController *)vc toClass:(Class)toClass animated:(BOOL)animated; /** 传入数据字典&Model名打印出Model Body*/
+(void)createModelWithDictionary:(NSDictionary *)dict modelName:(NSString *)modelName; /** 计算字符串高度*/
- (float) calculateStrheightWithStr:(NSString *)str Font: (UIFont *)font Width: (float) width; /** 计算字符串宽度*/
- (float) calculateStrwidthWithStr:(NSString *)str Font: (UIFont *) font; /** 将一个UIView切成圆形 宽高得相等*/
+ (void)changeViewToCircle:(UIView *)view; /** collectionView 重新加载数据后计算出高度并更新*/
+ (void)reloadCollectionViewHeight:(UICollectionView *)collectionView; // Factory /** 创建btn 基本属性: frame title titleColor backgroundColor*/
+ (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title TitleColor:(UIColor *)titleColor BackgroundColor:(UIColor *)backgroundColor; /** 创建btn 基本属性 + 边框 + 圆角 + 背景图*/
+ (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title TitleColor:(UIColor *)titleColor BackgroundColor:(UIColor *)backgroundColor CornerRadius:(CGFloat)cornerRadius BorderWidth:(CGFloat)borderWidth BorderColor:(UIColor *)borderColor ImageName:(NSString *)imageName; /** 创建label 基本属性 frame text font textColor*/
+ (UILabel *)createLabelWithFrame:(CGRect)frame Text:(NSString *)text Font:(UIFont *) font TextColor:(UIColor *)textColor; /** 创建label 基本属性 + backgroundColor + textAlinment */
+ (UILabel *)createLabelWithFrame:(CGRect)frame Text:(NSString *)text Font:(UIFont *) font TextColor:(UIColor *)textColor BackgroundColor:(UIColor *)backgroundColor TextAlignment:(NSTextAlignment)textAlinment; /** 富文本单个keyword*/
+(NSAttributedString *)attributeTextStr:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont keywordStr:(NSString *)keywordStr keywordColor:(UIColor *)keywordColor keywordFont:(UIFont *)keywordFont; /** 富文本多个keyword*/
+(NSAttributedString *)attributeTextStr:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont keywordStrArray:(NSArray *)keywordStrArray keywordColor:(UIColor *)keywordColor keywordFont:(UIFont *)keywordFont; @end /************************Define************************/ #ifdef DEBUG
#define QZLog(fmt, ...) NSLog((@"owner:QZ %s [Line %d] \n " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define QZLog(...)
#endif //判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device
#endif #if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif #define SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define IS_IOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0] /** RGB颜色*/
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A] #define DEVICE_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define DEVICE_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT DEVICE_HEIGHT
#define SCREEN_WIDTH DEVICE_WIDTH
#define NavigationBar_Height 44 /************************适配************************/
#define IS_IPHONEUI (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPADUI (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE4 ((int)DEVICE_HEIGHT%480==0)
#define IS_IPHONE5 ((int)DEVICE_HEIGHT%568==0)
#define IS_IPAD ((int)DEVICE_HEIGHT%1024==0)
#define IS_IPHONE6 ((int)DEVICE_HEIGHT%667==0)
#define IS_IPHONE6P ((int)DEVICE_HEIGHT%736==0)
/*
4 inch 320*568
4.7 inch 375*667
5.5 inch 414*736
*/
#define CONVERT_SCALE(x) (x)/2 #define CT_SCALE_X DEVICE_WIDTH/320.0
#define CT_SCALE_Y DEVICE_HEIGHT/568.0
// 5 6 比例一致,6p 放大
#define CT6P_SCALE_X (IS_IPHONE6P?CT_SCALE_X:1)
#define CT6P_SCALE_Y (IS_IPHONE6P?CT_SCALE_Y:1)
//转化为最低兼容设备尺寸
#define ConvertTo5_W(x) (CONVERT_SCALE(x)*320)/375
#define ConvertTo5_H(x) (CONVERT_SCALE(x)*568)/667 #define CW(x) ConvertTo5_W(x)*CT_SCALE_X
#define CH(x) ConvertTo5_H(x)*CT_SCALE_Y
#define CWFor6P(x) ConvertTo5_W(x)*CT6P_SCALE_X
#define CHFor6P(x) ConvertTo5_H(x)*CT6P_SCALE_Y
/************************适配************************/ 工具方法的实现 - (NSDictionary*)keyValueDic
{
if (!_keyValueDic) {
NSDictionary *keyValueDic=[[NSDictionary alloc] init];
keyValueDic = @{
@"key1":@"value1",
@"key2":@"value2",
@"key3":@"value3",
};
_keyValueDic = keyValueDic;
}
return _keyValueDic;
} +(NSString*)getCurrentTime{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateTime=[formatter stringFromDate:[NSDate date]];
return dateTime;
} +(NSString*)getFormatedTime:(NSDate*)date{
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateTime=[formatter stringFromDate:date];
return dateTime; } +(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize{
UIGraphicsBeginImageContext(reSize);
[image drawInRect:CGRectMake(, , reSize.width, reSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
} +(void)callSomebody:(NSString *)tel
{
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",tel];
UIWebView * callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[[UIApplication sharedApplication].keyWindow addSubview:callWebview];
} + (BOOL)ifThisStringIsAnNumber:(NSString *)str
{
if ([self isPureInt:str] || [self isPureFloat:str]) {
return YES;
}
return NO;
} + (BOOL)isPureInt:(NSString*)string
{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return[scan scanInt:&val] && [scan isAtEnd];
} + (BOOL)isPureFloat:(NSString*)string
{
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return[scan scanFloat:&val] && [scan isAtEnd];
} +(void)moveForwardTopVC:(UIViewController *)vc ForwardIndex:(NSInteger)index
{
// 直接操作nav的viewControllers栈
NSMutableArray *stackAry=[NSMutableArray arrayWithArray:vc.navigationController.viewControllers]; for (NSInteger i=vc.navigationController.viewControllers.count-; i>=vc.navigationController.viewControllers.count--index; i--) {
if(i<) return;
[stackAry removeObjectAtIndex:i];
}
[stackAry addObject:vc];
vc.navigationController.viewControllers=[stackAry copy];
} +(void)popToViewControllerWithVC:(UIViewController *)vc toClass:(Class)toClass animated:(BOOL)animated
{
for (UIViewController *controller in vc.navigationController.viewControllers) {
if ([controller isKindOfClass:toClass]) {
[vc.navigationController popToViewController:controller animated:animated];
}
}
} +(void)createModelWithDictionary:(NSDictionary *)dict modelName:(NSString *)modelName
{
printf("\n@interface %s :NSObject\n",modelName.UTF8String);
for (NSString *key in dict) {
NSString *type = ([dict[key] isKindOfClass:[NSNumber class]])?@"NSNumber":@"NSString";
printf("@property (nonatomic,copy) %s *%s;\n",type.UTF8String,key.UTF8String);
}
printf("@end\n"); } - (float) calculateStrheightWithStr:(NSString *)str Font: (UIFont *)font Width: (float) width
{
CGRect textRect = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:font}
context:nil]; return ceil(textRect.size.height);
} - (float) calculateStrwidthWithStr:(NSString *)str Font: (UIFont *) font
{
CGRect textRect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, font.pointSize)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:font}
context:nil]; return ceil(textRect.size.width);
} + (void)changeViewToCircle:(UIView *)view
{
view.layer.cornerRadius = view.bounds.size.width/2.0;
view.clipsToBounds = YES;
} + (void)reloadCollectionViewHeight:(UICollectionView *)collectionView
{
CGFloat x = collectionView.frame.origin.x;
CGFloat y = collectionView.frame.origin.y;
CGFloat width = collectionView.bounds.size.width;
CGFloat height = collectionView.collectionViewLayout.collectionViewContentSize.height;
collectionView.frame = CGRectMake(x, y, width, height);
} +(NSAttributedString *)attributeTextStr:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont keywordStr:(NSString *)keywordStr keywordColor:(UIColor *)keywordColor keywordFont:(UIFont *)keywordFont
{
return [CommonUse attributeTextStr:text textColor:textColor textFont:textFont keywordStrArray:keywordStr?@[keywordStr]:nil keywordColor:keywordColor keywordFont:keywordFont];
} +(NSAttributedString *)attributeTextStr:(NSString *)text textColor:(UIColor *)textColor textFont:(UIFont *)textFont keywordStrArray:(NSArray *)keywordStrArray keywordColor:(UIColor *)keywordColor keywordFont:(UIFont *)keywordFont
{
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text];
[attr addAttributes:@{NSFontAttributeName:textFont?:[UIFont systemFontOfSize:],NSForegroundColorAttributeName:textColor?:[UIColor blackColor]} range:NSMakeRange(, text.length)];
for (NSString *tempstr in keywordStrArray) {
[attr addAttributes:@{NSFontAttributeName:keywordFont?:[UIFont systemFontOfSize:],NSForegroundColorAttributeName:keywordColor?:[UIColor blackColor]} range:[text rangeOfString:tempstr]];
}
return attr;
} /** Button Factory*/
+ (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title TitleColor:(UIColor *)titleColor BackgroundColor:(UIColor *)backgroundColor
{
UIButton *btn = [[UIButton alloc] init];
btn.frame = frame;
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:titleColor ? : [UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor = backgroundColor ? :[UIColor whiteColor];
return btn;
} + (UIButton *)createButtonWithFrame:(CGRect)frame Title:(NSString *)title TitleColor:(UIColor *)titleColor BackgroundColor:(UIColor *)backgroundColor CornerRadius:(CGFloat)cornerRadius BorderWidth:(CGFloat)borderWidth BorderColor:(UIColor *)borderColor ImageName:(NSString *)imageName
{
UIButton *btn = [CommonUse createButtonWithFrame:frame Title:title TitleColor:titleColor BackgroundColor:backgroundColor];
btn.layer.cornerRadius = cornerRadius;
if (borderWidth > ) {
btn.layer.borderWidth=borderWidth;
}
if (borderColor) {
btn.layer.borderColor=[borderColor CGColor];
}
btn.layer.masksToBounds=YES;
[btn setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
return btn;
} + (UILabel *)createLabelWithFrame:(CGRect)frame Text:(NSString *)text Font:(UIFont *) font TextColor:(UIColor *)textColor
{
UILabel *label = [[UILabel alloc] init];
label.frame = frame;
label.text = text ? :@"";
if (font) {
label.font = font;
}
if (textColor) {
label.textColor = textColor;
}
return label;
} + (UILabel *)createLabelWithFrame:(CGRect)frame Text:(NSString *)text Font:(UIFont *) font TextColor:(UIColor *)textColor BackgroundColor:(UIColor *)backgroundColor TextAlignment:(NSTextAlignment)textAlinment
{
UILabel *label = [CommonUse createLabelWithFrame:frame Text:text Font:font TextColor:textColor];
if (backgroundColor) {
label.backgroundColor = backgroundColor;
}
label.textAlignment = textAlinment;
return label;
}
IOS - 常用宏定义和功能方法的更多相关文章
- iOS - 常用宏定义和PCH文件知识点整理
(一)PCH文件操作步骤演示: 第一步:图文所示: 第二步:图文所示: (二)常用宏定义整理: (1)常用Log日志宏(输出日志详细可定位某个类.某个函数.某一行) //=============== ...
- iOS常用宏 定义
总结了iOS开发过程中的一些常用宏,以后会陆陆续续添加进来. 字符串是否为空 1 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull c ...
- iOS常用宏定义--实用
在这里给大家分享一些常用的宏定义,喜欢的小伙伴可以直接在项目中使用(持续更新)!为了大家使用方便,请点击GitHub - 宏定义头文件下载 ! 1.获取屏幕宽度与高度 #define SCREEN_W ...
- iOS常用宏定义大全
宏定义与常量的区别 宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时间相对较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间. 所以在使用 ...
- iOS常用宏定义
转发:https://www.douban.com/note/486674206/ #ifndef MacroDefinition_h#define MacroDefinition_h //----- ...
- (转)iOS 常用宏定义
#ifndef MacroDefinition_h #define MacroDefinition_h //-------------------获取设备大小------------------- ...
- IOS 程序员开发最常用宏定义
网上对IOS的宏定义比较多,我总结了一些最常用的宏,后续还会继续补上. 1.首次启动判断: #define First_Launched @"firstLaunch" 2.ios7 ...
- ios 常用宏(copy)
分享一下我现在用的 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...
- iOS 使用宏定义函数和代码块
iOS使用宏定义函数和代码块 今天在开发过程中碰到一个问题:就是父类中要向外发送通知,然后子类中或者其他类中来接收它.当然一般是把它写到类方法中去,但是有个问题,就是如果调用的类不是它的子类,就不能直 ...
随机推荐
- POJ 3436 ACM Computer Factory (拆点+输出解)
[题意]每台计算机由P个零件组成,工厂里有n台机器,每台机器针对P个零件有不同的输入输出规格,现在给出每台机器每小时的产量,问如何建立流水线(连接各机器)使得每小时生产的计算机最多. 网络流的建图真的 ...
- SSH信任
配置SSH的目的就是使得两个节点的主机之间的相同用户可以无障碍的通信,SSH主要包括两条命令,即scp和ssh.当用户在一个节点上安装和配置RAC软件时,SSH将通过scp命令,以对等用户的身份,将软 ...
- Spring AOP前置通知和后置通知
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...
- Protobuf语言指南
Protobuf语言指南 l 定义一个消息(message)类型 l 标量值类型 l Optional 的字段及默认值 l 枚举 l 使用其他消息类型 l 嵌套类型 l 更新一个消息类型 ...
- 刑事案件的构成要素 zt
论刑事案件的构成要素 马忠红 2013-03-22 14:05:33 来源:<中国人民公安大学学报:社会科学版>(京)2012年5期 [内容提要]刑事案件是由诸多要素构成的一个系 统. ...
- HDU 1251-统计难题(Trie)
题意: 给一组单词 开始提问每次给一个串求该串是上面几个单词的前缀 分析: 没给数据规模,但用链表写ME好几次,又用数组写开小RE了,试了几次才过了,真是醉了... #include <map& ...
- Doing Homework(HDU 1074状压dp)
题意:给你n个要做的作业,它们的名字.期限.可完成所需天数(必须连续)在规定期限不能完成要扣分(每天一分)求做作业顺序使扣分最少. 分析:作业数量较少,用状态压缩,做到第i种作业花费的天数dp[i]. ...
- javscript面试题(一)
你如何理解HTML结构的语意化? 1.去掉或样式丢失的时候能让页面呈现清晰的结构:2.屏幕阅读器(如果访客有视障)会完全根据你的标记来“读”你的网页:3.PDA.手机等设备可能无法像普通电脑的浏览器一 ...
- Android Socket 相关
http://www.imooc.com/learn/223 http://www.epubit.com.cn/book/onlinechapter/12093
- ffmpeg 的tutorial
可能是新的: https://github.com/chelyaev/ffmpeg-tutorial https://github.com/chelyaev/ffmpeg-tutorial.git 老 ...