版本记录
版本号 | 修改时间 |
---|---|
2017.04.29 | V1.0 |
前言
工程中我们总需要为各种各样的小功能进行封装,这样团队使用的时候,可以调用这些封装好的小模块或者小功能,就不用从底层开始一个个的写了,这样可以加快开发速度,缩短开发周期,下面我就写几个封装好的小功能,想起来几个就写几个,以后我会更新。
详情
一、十六进制字符串转颜色色值
我们知道oc中,只有r g b a转化颜色色值,如下:
// Convenience methods for creating colors
+ (UIColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha;
+ (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
+ (UIColor *)colorWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha NS_AVAILABLE_IOS(10_0);
+ (UIColor *)colorWithCGColor:(CGColorRef)cgColor;
+ (UIColor *)colorWithPatternImage:(UIImage *)image;
#if __has_include(<CoreImage/CoreImage.h>)
+ (UIColor *)colorWithCIColor:(CIColor *)ciColor NS_AVAILABLE_IOS(5_0);
#endif
// Initializers for creating colors
- (UIColor *)initWithWhite:(CGFloat)white alpha:(CGFloat)alpha;
- (UIColor *)initWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;
- (UIColor *)initWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
- (UIColor *)initWithDisplayP3Red:(CGFloat)displayP3Red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha NS_AVAILABLE_IOS(10_0);
- (UIColor *)initWithCGColor:(CGColorRef)cgColor;
- (UIColor *)initWithPatternImage:(UIImage*)image;
#if __has_include(<CoreImage/CoreImage.h>)
- (UIColor *)initWithCIColor:(CIColor *)ciColor NS_AVAILABLE_IOS(5_0);
#endif
可以发现并没有十六进制转化为颜色的接口,这里提供了一个,不多说了直接看代码,先看一下代码组织结构。
直接看代码吧。
1. AppDelegate.m
#import "AppDelegate.h"
#import "JJMainVC.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
JJMainVC *mainVC = [[JJMainVC alloc] init];
self.window.rootViewController = mainVC;
[self.window makeKeyAndVisible];
return YES;
}
@ end
2. JJMainVC.m
#import "JJMainVC.h"
#import "UIColor+JJUIColor.h"
#import "Masonry.h"
@interface JJMainVC ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *avatarButton;
@end
@implementation JJMainVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
//用户描述
[self.titleLabel sizeToFit];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(100.0);
make.left.equalTo(self.view).offset(100.0);
}];
}
#pragma mark - Object Private Function
- (void)setupUI
{
self.view.backgroundColor = [UIColor lightGrayColor];
//用户描述
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"看我是什么颜色";
titleLabel.textColor = [UIColor colorWithHexString:@"FF00FF"];
[self.view addSubview:titleLabel];
self.titleLabel = titleLabel;
}
@end
3. UIColor+JJUIColor.h
#import <UIKit/UIKit.h>
@interface UIColor (JJUIColor)
/**
Creates and returns a color object from hex string.
@discussion:
Valid format: #RGB #RGBA #RRGGBB #RRGGBBAA 0xRGB ...
The `#` or "0x" sign is not required.
The alpha will be set to 1.0 if there is no alpha component.
It will return nil when an error occurs in parsing.
Example: @"0xF0F", @"66ccff", @"#66CCFF88"
@param hexStr The hex string value for the new color.
@return An UIColor object from string, or nil if an error occurs.
*/
+ (instancetype)colorWithHexString:(NSString *)hexStr;
@end
4. UIColor+JJUIColor.m
#import "UIColor+JJUIColor.h"
#import "NSString+JJNSString.h"
@implementation UIColor (JJUIColor)
#pragma mark - Object Base Function
+ (instancetype)colorWithHexString:(NSString *)hexStr {
CGFloat r, g, b, a;
if (hexStrToRGBA(hexStr, &r, &g, &b, &a)) {
return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
return nil;
}
static BOOL hexStrToRGBA(NSString *str,
CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) {
str = [[str stringByTrim] uppercaseString];
if ([str hasPrefix:@"#"]) {
str = [str substringFromIndex:1];
} else if ([str hasPrefix:@"0X"]) {
str = [str substringFromIndex:2];
}
NSUInteger length = [str length];
// RGB RGBA RRGGBB RRGGBBAA
if (length != 3 && length != 4 && length != 6 && length != 8) {
return NO;
}
//RGB,RGBA,RRGGBB,RRGGBBAA
if (length < 5) {
*r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
*g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
*b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
if (length == 4) *a = hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f;
else *a = 1;
}
else {
*r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
*g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
*b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
if (length == 8) *a = hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f;
else *a = 1;
}
return YES;
}
static inline NSUInteger hexStrToInt(NSString *str)
{
uint32_t result = 0;
sscanf([str UTF8String], "%X", &result);
return result;
}
@end
5. NSString+JJNSString.h
#import <Foundation/Foundation.h>
@interface NSString (JJNSString)
- (NSString *)stringByTrim;
@end
6. NSString+JJNSString.m
#import "NSString+JJNSString.h"
@implementation NSString (JJNSString)
- (NSString *)stringByTrim
{
NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
return [self stringByTrimmingCharactersInSet:set];
}
@end
我们直接看结果。
二、图片切圆角
图片切圆角,其实就是切layer,就是两句话。
self.avatarButton.layer.cornerRadius = 15.0;
self.avatarButton.layer.masksToBounds = YES;
但是这里有一个问题,图片少的话还可以,但是如果一个tableview上有很多图片的话,滑动的时候这么切圆角就很耗cpu性能,就会出现卡顿,这里有个方法就是画上去。
先看代码结构。
我这里红色的部分就是对上面增加的部分,我只列出改变的部分吧。看代码。
1. JJMainVC.m
#import "JJMainVC.h"
#import "UIColor+JJUIColor.h"
#import "Masonry.h"
#import "UIImage+JJImage.h"
@interface JJMainVC ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *avatarButton;
@end
@implementation JJMainVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
//用户描述
[self.titleLabel sizeToFit];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(100.0);
make.left.equalTo(self.view).offset(100.0);
}];
//头像
[self.avatarButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0);
make.width.height.equalTo(@100);
}];
}
#pragma mark - Object Private Function
- (void)setupUI
{
self.view.backgroundColor = [UIColor lightGrayColor];
//用户描述
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"看我是什么颜色";
titleLabel.textColor = [UIColor colorWithHexString:@"FF00FF"];
[self.view addSubview:titleLabel];
self.titleLabel = titleLabel;
//头像
UIButton *avatarButton = [UIButton buttonWithType:UIButtonTypeCustom];
[avatarButton setImage:[[UIImage imageNamed:@"71r58PICmDM_1024.jpg"] circleImage] forState:UIControlStateNormal];
[self.view addSubview:avatarButton];
self.avatarButton = avatarButton;
}
@end
2. UIImage+JJImage.h
#import <UIKit/UIKit.h>
@interface UIImage (JJImage)
- (UIImage *)circleImage;
@end
3. UIImage+JJImage.m
#import "UIImage+JJImage.h"
@implementation UIImage (JJImage)
- (UIImage *)circleImage
{
// 开始图形上下文
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
// 获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置一个范围
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
// 根据一个rect创建一个椭圆
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 将原照片画到图形上下文
[self drawInRect:rect];
// 从上下文上获取剪裁后的照片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
return newImage;
}
@end
然后我们看结果。
三、根据UIColor色值返回Image
这个功能就是根据传入的UIColor色值,返回UIImage图像,特别当我们希望我们这个按钮的背景的图片时,这个时候就需要我们给一个UIColor色值,返回的是UIImage图像。在原代码中继续加东西。
1. JJMainVC.m
//属性增加
@property (nonatomic, strong) UIImageView *contentImageView;
//控件内容增加
//内容图像按钮
UIImageView *contentImageView = [[UIImageView alloc] init];
contentImageView.image = [UIImage zbImageWithColor:[UIColor colorWithHexString:@"FF00FF"]];
[self.view addSubview:contentImageView];
self.contentImageView = contentImageView;
//约束增加
//内容
[self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(self.avatarButton.mas_bottom).offset(15.0);
make.width.height.equalTo(@200);
}];
2. UIImage+JJImage.h
+ (UIImage *)zbImageWithColor:(UIColor *)color;
3. UIImage+JJImage.m
+ (UIImage *)zbImageWithColor:(UIColor *)color;
{
CGRect rect = CGRectMake(0.0, 0.0, 1.0, 1.0);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
我们可以看一下结果。
四、电话号有效性判断
我们在做注册登录的时候,常需要对电话号的有效性进行判断,我们用的就是正则表达式进行判断。不多说了,我自己写了一个,用着还可以。先看代码。
1. JJMainVC.m
```
```
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL isPhoneRight1 = [self isPhoneNumberAvailable:@"18614005678"];
BOOL isPhoneRight2 = [self isPhoneNumberAvailable:@"12345678900"];
NSLog(@"isPhoneRight1----%d",isPhoneRight1);
NSLog(@"isPhoneRight2----%d",isPhoneRight2);
}
```
```
- (BOOL)isPhoneNumberAvailable:(NSString *)phoneNumberStr
{
NSString *phoneNumStr;
phoneNumStr = [[NSString alloc] initWithString:[phoneNumberStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
phoneNumStr = [phoneNumStr stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *phoneRegex = @"\\b(1)[34578][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\\b|\\b(1)(7)(7)[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\\b|\\b(1)(7)(6)[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\\b";
NSPredicate *phoneCheck = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
if (![phoneCheck evaluateWithObject:phoneNumStr]) {
return NO;
}
return YES;
}
```
然后我们查看结果。
```
2017-04-30 19:43:13.547 几种简单功能的封装[5502:232905] isPhoneRight1----1
2017-04-30 19:43:13.547 几种简单功能的封装[5502:232905] isPhoneRight2----0
```
# 后记
>   先写这么多吧,待续~~~,谢谢大家。
![美图](http://upload-images.jianshu.io/upload_images/3691932-a9b82761f9ffb74a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)