先看两行代码:
1.

label2.textColor = [UIColor colorWithHexString:@"707070"];

2.

_table.header = [MJRefreshHeader headerWithRefreshingBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 刷新时的相关处理
});
}];

相信大家对上面的两行代码都不会陌生

  • 上一行:UIColor原本是没有读取十六进制颜色值的方法的
  • 下一行:UITableView原本是没有header属性的

那么,How it happened?

Because of the Category!


Category(类别)简介

  1. 利用Objective-C的动态运行时分配机制,可以为现有的类(自己的或系统的或三方库的)添加新方法,这种为现有的类添加新方法的方式称为类别category,他可以为任何类添加新的方法,包括那些没有源代码的类。
  2. 类别使得无需创建对象类的子类就能完成同样的工作。
 

Category的作用

一. 扩展类的方法
这是我们用的最多的,现在就以扩展UIColor的一个读取16进制颜色的方法为例:

在.m文件中编写扩展的方法

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
//删除字符串中的空格
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < )
{
return [UIColor clearColor];
}
// strip 0X if it appears
//如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾
if ([cString hasPrefix:@"0X"])
{
cString = [cString substringFromIndex:];
}
//如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾
if ([cString hasPrefix:@"#"])
{
cString = [cString substringFromIndex:];
}
if ([cString length] != )
{
return [UIColor clearColor];
} // Separate into r, g, b substrings
NSRange range;
range.location = ;
range.length = ;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = ;
NSString *gString = [cString substringWithRange:range];
//b
range.location = ;
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:alpha];
}

在.h文件中将方法暴露出来

/* 从十六进制字符串获取颜色 */
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;

至此,扩展系统类UIColor的方法成功,需要时,导入头文件直接使用即可:

self.view.backgroundColor=[UIColor colorWithHexString:@"f7f7f9"];

二. 扩展类的属性(结合runtime)
这个也是相当实用的,举个

Category在项目中的实际运用的更多相关文章

  1. 在Android项目中引入MuPdf

    由于公司手机App要加入一个附件查看功能,需要查看PDF文件,在网上找了许多第三方工具,最后选择了MuPDF. 更多第三方工具可以查看大神总结的:http://www.cnblogs.com/poke ...

  2. 项目中是用eCharts

    1.首先在项目中引入echart.js库. <!DOCTYPE HTML> <%@page contentType="text/html; charset=UTF-8&qu ...

  3. 项目中添加Log4J支持

    首先,在项目中的classes 中新建立一个log4j.properties文件即可: 在实际编程时,要使Log4j真正在系统中运行事先还要对配置文件进行定义.定义步骤就是对Logger.Append ...

  4. 在项目中创建单元测试时junit的配置和使用

    首先配置项目中AndroidMainfest.xml文件,加入 <instrumentation android:name="android.test.InstrumentationT ...

  5. 03_Android项目中读写文本文件的代码

    编写一下Android界面的项目 使用默认的Android清单文件 <?xml version="1.0" encoding="utf-8"?> & ...

  6. vue项目中全局配置变量

    在项目中api管理需要用到全局变量,创建全局变量的方式也有很多. 1.通过export default const BASEURL = "http://localhost:3333/&quo ...

  7. Vue+Typescript项目中使用echarts

    方案一:推荐 在typescript+Vue的项目中引用echarts,为了加强引用,引入echarts和@types/echarts两个包,一个是工程依赖,一个是声明依赖. npm install ...

  8. asp.net Web项目中使用Log4Net进行错误日志记录

      使用log4net可以很方便地为应用添加日志功能.应用Log4net,开发者可以很精确地控制日志信息的输出,减少了多余信息,提高了日志记录性能.同时,通过外部配置文件,用户可以不用重新编译程序就能 ...

  9. sessionStorage在项目中的应用

    1. 本地存储 Cookie(局限性):用户可以禁用cookie,最多只能存储4kb,cookie有过期时间的(一般我们设置的时间最长1个月,用户使用杀毒软件也可以清除我们的cookie)LocalS ...

随机推荐

  1. python3 AttributeError: module 'sklearn' has no attribute 'linear_model'

    以下导入方式报错 import sklearn lr = sklearn.linear_model.LinearRegression() # 需要导入sklearn的linear_model 修改导入 ...

  2. Laravel Composer 脚本

    composer update --no-scripts 执行静态文件 composer dump-autoload 文件映射

  3. Windows 增强版任务管理器-Process Explorer

    百度百科PROCESS EXPLORER介绍 由Sysinternals开发的Windows系统和应用程序监视工具,目前已并入微软旗下.不仅结合了Filemon(文件监视器)和Regmon(注册表监视 ...

  4. oauth2-server-php-docs 存储 学说2

    学说2 创建客户端和访问令牌存储 要把学说融入到你的项目中,首先要建立你的实体.我们先从客户端,用户和访问令牌模型开始: yaml YourNamespace\Entity\OAuthClient: ...

  5. [Git] Undo a commit that has already been pushed to the remote repository

    If we pushed our changes already to the remote repository we have to pay attention to not change the ...

  6. eclipse启动错误:java.lang.NoClassDefFoundError: org/eclipse/core/resources/IContainer

    转自:http://blog.csdn.net/niu_hao/article/details/9332521 eclipse启动时报错如下:java.lang.NoClassDefFoundErro ...

  7. Creating objects on stack or heap

    class Player {  private: int health;  int strength;  int agility; public: void move(); void attackEn ...

  8. XAML中特殊符号书写

    XAML中特殊符号书写     表示换行.      表示空格.

  9. vue循环中的v-show

    v-show如果使用循环对象的属性来时控制, 这个属性必须是加载时就存在的 <div class="list-group col-sm-12" v-for="(is ...

  10. JS-json-1

    smarty模板要处理成ajax,所以须要又一次拼接一个html来追加节点. 原先smarty的数组如今须要处理成json数据返回了,服务器端的修改比較小: header("Content- ...