iOS工具类

作者: Smallwolf_JS | 来源:发表于2015-11-04 11:09 被阅读3849次

前言

现在好多Dev都开始创建并使用工具类,因为把一些很多地方用到得方法写到工具类里面会显得很简单明了,最主要的是使用起来方便。这里想用了直接包含以下头文件调用以下就可以,比如iToast类,只是显示一个类似安卓提示的一个小黑框而已,每次只需要调用一下传进去要显示的字符串就可以了。
---哎呀废话太多了。
现在我跟大家分享下自己创建的工具类

1.创建工具类

很显然是继承自NSObject的,自己打开Xcode创建一个继承自NSObject的类,我的起名字叫做KK_Utils,在.h中写类名,全部是类方法,调用起来方便,在.m中实现。

2.实现具体方法
* 1在iOS编程中必不可少的就是十六进制的颜色值,所以十六进制颜色转换必不可少
/*
 * 十六进制颜色值转换成UIColor对象
 */
+ (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反过来颜色转换成十六进制的字符串,这个基本上没有什么人会用到,我基本上不用
/*
 *  UIColor对象转换成十六进制颜色值字符串
 */
+ (NSString *)changeUIColorToRGB:(UIColor *)color;
//颜色转换为字符串
+ (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这个方法是我比较喜欢的,利用颜色来创建图片,修改导航颜色的时候很好用
/*
 *  使用UIColor创建UIImage
 */
+ (UIImage *) createImageWithColor: (UIColor *)color;
// 使用UIColor创建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弹出矩形黑色提示窗(类似安卓的提示窗)需要下载一个类iToast,文件减小易用,下载下来添加到工程里面就可以了,但是我在使用过程中发现了一个bug,就是传入要显示的字符串为nil的时候被导致崩溃,因为这个类里面用了属性字符串,遇到nil会崩溃,自己可以在崩溃的地方自行加一个判断。

在.m中包含头文件

#import "iToast.h"

显示位置可以设置多个,这里提供两个位置一个是中间,一个是中偏下的位置

+ (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 同上显示菊花也是一样,网络请求或者加载数据时候难免会用到菊花我使用的是

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;//设置显示框的高度和宽度一样
    hud.removeFromSuperViewOnHide = YES;
    [hud show:YES];
}
//使用示例
 MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
 [KK_Utils showHUD:@"正在加载" andView:self.view andHUD:hud];

//关闭时执行代码:
hud.hidden = YES;

3.总结

这个工具类会持续更新,比如MD5加密字符串等等,希望大神们多多指教,欢迎评论Star。
欢迎关注我的微博博客

相关文章

  • iOS工具类

    前言 现在好多Dev都开始创建并使用工具类,因为把一些很多地方用到得方法写到工具类里面会显得很简单明了,最主要的是...

  • iOS 内存泄漏检测

    【YFMemoryLeakDetector】人人都能理解的 iOS 内存泄露检测工具类 背景 即使到今天,iOS ...

  • IOS DB 封装类使用

    @[TOC](IOS DB 封装类使用) IOS DB 工具类使用 1. 定义模型 如下定义一个Student模型...

  • iOS开发常用的工具类和宏定义

    iOS开发常用的工具类和宏定义 开发总结的工具类和宏 https://github.com/xiaoChen66...

  • 1.3 iOS应用逆向工程的工具

    iOS逆向工程的工具分为四大类:监测工具、反汇编工具(disassembler)、调试工具(debugger)、开...

  • iOS逆向

    iOS逆向工程的工具可以分为四大类:监测工具、反汇编工具(disassembler)、调试工具(debugger)...

  • iOS逆向(class-dump)

    1,iOS逆向工程的工具可以分为四大类:监测工具,反汇编工具(disassembler),调试工具(debugge...

  • iOS工具类(1)

    MARK: - 对字符串进行加密 MD5 加密: 对字符串进行分类处理 【16位与32位】 MARK: - 对字...

  • iOS 工具类 -- 定位

    开发中经常会遇到重复性的需求,自然会有重复性的代码。小白就会 command + c, command + v;大...

  • iOS Config工具类

    一.字符串分断显示不同大小、颜色字体 1.对array参数的介绍: 其中point字段中的数组,两个值是按照NSM...

网友评论

本文标题:iOS工具类

本文链接:https://www.haomeiwen.com/subject/mugqhttx.html