开发中常用的一些工具
程序员文章站
2022-07-03 23:22:36
...
计算一个字符串的在一定宽度范围内 的 size
/* 计算文字的宽高
* param1:theString 需要计算的文字
* param2:aSize 文字在相应载体(label、textView等)的字体
* param3:aWidth 展示文字的最大宽度
*/
+(CGSize)calculateText:(NSString *)theString font:(CGFloat)aSize maxWidth:(CGFloat)aWidth;
#pragma mark - 自动计算文字的高度
+(CGSize)calculateText:(NSString *)theString font:(CGFloat)aSize maxWidth:(CGFloat)aWidth{
NSDictionary *attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:aSize]};
CGRect rect = [theString boundingRectWithSize:CGSizeMake(aWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];
return rect.size;
}
/* 通过一个颜色值 得到一张图片
* param1: 颜色
* param2: 图片的尺寸
*/
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;
#pragma mark - 通过颜色值生产一张图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
UIImage* theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
#pragma mark - 获取设备型号
+(NSString *)getDeviceType{
struct utsname systemInfo;
uname(&systemInfo);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
NSString *path = [[NSBundle mainBundle]pathForResource:@"DeviceType" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
if ([[dict allKeys]containsObject:platform]) {
return [dict objectForKey:platform];
}else{
return platform;
}
}
#pragma mark - 检测设备是否是iPad
+(BOOL)isiPad{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
return TRUE;
}
return FALSE;
}
#pragma mark -检测设备 是否越狱
+(BOOL)isJailBreak{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]) {
return YES;
}
if ([[NSFileManager defaultManager] fileExistsAtPath:@"/User/Applications/"]) {
return YES;
}
const char* jailbreak_tool_pathes[] = {
"/Applications/Cydia.app",
"/Library/MobileSubstrate/MobileSubstrate.dylib",
"/bin/bash",
"/usr/sbin/sshd",
"/etc/apt"
};
for (int i=0; i<5; i++) {
if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:jailbreak_tool_pathes[i]]]) {
return YES;
}
}
return NO;
}
#pragma mark -获取当前显示的在屏幕上的控制器
+(UIViewController *)getTopController{
UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
}
#pragma mark - 检测手机号码
+(BOOL)isPhoneNumber:(NSString *)num{
if (num.length < 11)
return NO;
NSString* regularExpression = @"^1[3|4|5|7|8][0-9]\\d{8}$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regularExpression];
return [pre evaluateWithObject:num];
}
#pragma mark - 获取设备的IDFA
+(NSString *)getDevice_IDFA{
NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
if (adId) {
return adId;
}
return @"";
}
#pragma mark - 获取设备的UUID
+(NSString *)getDevice_UUID{
if(NSClassFromString(@"NSUUID")) { // only available in iOS >= 6.0
return [[NSUUID UUID] UUIDString];
}
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef cfuuid = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
CFRelease(uuidRef);
NSString *uuid = [((__bridge NSString *) cfuuid) copy];
CFRelease(cfuuid);
return uuid;
}
#pragma mark - 获取设备的物理地址
+(NSString *)getDevice_MacAddress{
int mib[6];
size_t len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if ((mib[5] = if_nametoindex("en0")) == 0) {
printf("Error: if_nametoindex error/n");
return NULL;
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 1/n");
return NULL;
}
if ((buf = malloc(len)) == NULL) {
printf("Could not allocate memory. error!/n");
return NULL;
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 2");
return NULL;
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free(buf);
return [outstring uppercaseString];
}
#pragma mark - 获取设备系统版本号
+ (NSString*)getDevice_iOSVersion{
return [[UIDevice currentDevice] systemVersion];
}
#pragma mark - 获取设备系统电量
+(CGFloat )getDevice_batteryLevel{
return [[UIDevice currentDevice]batteryLevel];
}
#pragma mark - 获取设备当前网络状态
+(NSString *)getNetWorkStates{
NSArray *arr = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
int type = 0;
NSString *state = @"";
for (id item in arr) {
if ([item isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
type = [[item valueForKeyPath:@"dataNetworkType"] intValue];
}
}
switch (type) {
case 1:{
state = @"2G";
}break;
case 2:{
state = @"3G";
}break;
case 3:{
state = @"4G";
}break;
case 5:{
state = @"WIFI";
}
default:{
state = @"未知网络";
}break;
}
return state;
}
#pragma mark - 获取设备当前时间
+ (NSString *)getDevice_time{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
return [dateFormatter stringFromDate:[NSDate date]];
}
#pragma mark - 获取应用的包名
+(NSString *)getApplication_BundleIdentify{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
return [infoDictionary objectForKey:@"CFBundleIdentifier"];
}
#pragma mark - 获取项目名称
+ (NSString*)getApplication_projectName{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
return [infoDictionary objectForKey:@"CFBundleName"];
}
#pragma mark - 获取项目版本号
+ (CGFloat)getApplication_version{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
return [[infoDictionary objectForKey:@"CFBundleVersion"] floatValue];
}
//将 数值 按千位分隔符的方式分隔开
+(NSString *)PriceFormateWithPrice:(NSString *)price
{
NSNumberFormatter *matter = [[NSNumberFormatter alloc]init];
[matter setPositiveFormat:@"###,###,##0.00"];
NSString *result = [matter stringFromNumber:[NSNumber numberWithDouble:[price doubleValue]]];
result = [@"¥" stringByAppendingString:result];
return result;
}
欢迎广大 程序开发爱好者来补充让以后的开发多一些工具 少一些羁绊!
上一篇: Redhat7修改默认运行级别方法
下一篇: 有种爆笑叫酒喝多了。。。