iOS工具類(lèi)

前言

現(xiàn)在好多Dev都開(kāi)始創(chuàng)建并使用工具類(lèi),因?yàn)榘岩恍┖芏嗟胤接玫降梅椒▽?xiě)到工具類(lèi)里面會(huì)顯得很簡(jiǎn)單明了,最主要的是使用起來(lái)方便。這里想用了直接包含以下頭文件調(diào)用以下就可以,比如iToast類(lèi),只是顯示一個(gè)類(lèi)似安卓提示的一個(gè)小黑框而已,每次只需要調(diào)用一下傳進(jìn)去要顯示的字符串就可以了。
---哎呀廢話太多了。
現(xiàn)在我跟大家分享下自己創(chuàng)建的工具類(lèi)

1.創(chuàng)建工具類(lèi)

很顯然是繼承自NSObject的,自己打開(kāi)Xcode創(chuàng)建一個(gè)繼承自NSObject的類(lèi),我的起名字叫做KK_Utils,在.h中寫(xiě)類(lèi)名,全部是類(lèi)方法,調(diào)用起來(lái)方便,在.m中實(shí)現(xiàn)。

2.實(shí)現(xiàn)具體方法
* 1在iOS編程中必不可少的就是十六進(jìn)制的顏色值,所以十六進(jìn)制顏色轉(zhuǎn)換必不可少
/*
 * 十六進(jìn)制顏色值轉(zhuǎn)換成UIColor對(duì)象
 */
+ (UIColor *) hexStringToColor: (NSString *) stringToConvert;
+ (UIColor *) hexStringToColor: (NSString *) stringToConvert{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor blackColor];
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor blackColor];
    // Separate into r, g, b substrings
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}
* 2反過(guò)來(lái)顏色轉(zhuǎn)換成十六進(jìn)制的字符串,這個(gè)基本上沒(méi)有什么人會(huì)用到,我基本上不用
/*
 *  UIColor對(duì)象轉(zhuǎn)換成十六進(jìn)制顏色值字符串
 */
+ (NSString *)changeUIColorToRGB:(UIColor *)color;
//顏色轉(zhuǎn)換為字符串
+ (NSString *) changeUIColorToRGB:(UIColor *)color{
    const CGFloat *cs=CGColorGetComponents(color.CGColor);
    NSString *r = [NSString stringWithFormat:@"%@",[self  ToHex:cs[0]*255]];
    NSString *g = [NSString stringWithFormat:@"%@",[self  ToHex:cs[1]*255]];
    NSString *b = [NSString stringWithFormat:@"%@",[self  ToHex:cs[2]*255]];
    return [NSString stringWithFormat:@"#%@%@%@",r,g,b];  
}
* 3這個(gè)方法是我比較喜歡的,利用顏色來(lái)創(chuàng)建圖片,修改導(dǎo)航顏色的時(shí)候很好用
/*
 *  使用UIColor創(chuàng)建UIImage
 */
+ (UIImage *) createImageWithColor: (UIColor *)color;
// 使用UIColor創(chuàng)建UIImage
+ (UIImage *)createImageWithColor: (UIColor *)color;{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
* 4彈出矩形黑色提示窗(類(lèi)似安卓的提示窗)需要下載一個(gè)類(lèi)iToast,文件減小易用,下載下來(lái)添加到工程里面就可以了,但是我在使用過(guò)程中發(fā)現(xiàn)了一個(gè)bug,就是傳入要顯示的字符串為nil的時(shí)候被導(dǎo)致崩潰,因?yàn)檫@個(gè)類(lèi)里面用了屬性字符串,遇到nil會(huì)崩潰,自己可以在崩潰的地方自行加一個(gè)判斷。

在.m中包含頭文件

#import "iToast.h"

顯示位置可以設(shè)置多個(gè),這里提供兩個(gè)位置一個(gè)是中間,一個(gè)是中偏下的位置

+ (void)showItoast:(NSString *)str;
+ (void)showItoastInCenter:(NSString *)str;
+ (void)showItoast:(NSString *)str{
    dispatch_async(dispatch_get_main_queue(), ^{
        iToast *itost = [[iToast makeText:str] setGravity:iToastGravityBottom];
        [itost setDuration:iToastDurationNormal];
    
        [itost show:iToastTypeWarning];
    });
    
}
+ (void)showItoastInCenter:(NSString *)str{
    dispatch_async(dispatch_get_main_queue(), ^{
        iToast *itost = [[iToast makeText:str] setGravity:iToastGravityCenter];
        [itost setDuration:iToastDurationShort];
        [itost show:iToastTypeNotice];
    });
}
* 5 同上顯示菊花也是一樣,網(wǎng)絡(luò)請(qǐng)求或者加載數(shù)據(jù)時(shí)候難免會(huì)用到菊花我使用的是

MBProgressHUD。

+(void)showHUD:(NSString *)text andView:(UIView *)view andHUD:(MBProgressHUD *)hud;
+(void)showHUD:(NSString *)text andView:(UIView *)view andHUD:(MBProgressHUD *)hud{
    [view addSubview:hud];
    hud.labelText = text;//顯示提示
    hud.dimBackground = YES;//使背景成黑灰色,讓MBProgressHUD成高亮顯示
    hud.square = YES;//設(shè)置顯示框的高度和寬度一樣
    hud.removeFromSuperViewOnHide = YES;
    [hud show:YES];
}
//使用示例
 MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
 [KK_Utils showHUD:@"正在加載" andView:self.view andHUD:hud];

//關(guān)閉時(shí)執(zhí)行代碼:
hud.hidden = YES;

3.總結(jié)

這個(gè)工具類(lèi)會(huì)持續(xù)更新,比如MD5加密字符串等等,希望大神們多多指教,歡迎評(píng)論Star。
歡迎關(guān)注我的微博博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容