基础类(颜色+字体+提示)

2017-09-26  本文已影响52人  Kevin_wzx

简介

封装基础类,便于自定义使用,全局使用

1.颜色

AXColor.h

#import <Foundation/Foundation.h>

@interface AXColor : NSObject

/** 主色
 *绿
 */
+ (UIColor *)greenColor;

/**
 *橙
 */
+ (UIColor *)orangeColor;

/**
 *灰
 */
+ (UIColor *)grayColor;

/**
 *红
 */
+ (UIColor *)redColor;

/**
 *黑
 */
+ (UIColor *)blackColor;

/**
 *蓝
 */
+ (UIColor *)blueColor;

/**
 *白
 */
+ (UIColor *)whiteColor;

/**
 *  黄
 *
 */
+ (UIColor *)yellowColor;

/**
 *背景灰色
 */
+ (UIColor *)backGrayColor;

/**
 *背景白色
 */
+ (UIColor *)backWhiteColor;


/**
 *分割线用色
 */
+ (UIColor *)separatelineGrayColor;


/**
 *导航栏按钮背景颜色
 */
+ (UIColor *)navigationBarBackColor;


/**
 *导航栏标题颜色
 */
+ (UIColor *)navigationBarTitleColor;

/**
 *遮罩颜色
 */
+ (UIColor *)hideViewColor;

/**
 浅橙色
 */
+ (UIColor *)shallowOrangeColor;

/**
 * 将16进制颜色制传换成color
 *
 **/
+(UIColor *)colorWithHexString:(NSString *)color;

+(UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;

/**
 * 将UIColor 变深
 *
 **/
+(UIColor *)deepColorWithColor:(UIColor *)color;

@end

AXColor.m

#import "AXColor.h"

@implementation AXColor

+ (UIColor *)greenColor {
    
    return HEXCOLOR(@"#00AE85");
}

+ (UIColor *)orangeColor {
    
    return HEXCOLOR(@"#FFC12D");
}

+ (UIColor *)grayColor {
    
    return HEXCOLOR(@"#999999");
}

+ (UIColor *)redColor {
    
    return HEXCOLOR(@"#FC4B4B");
}

+ (UIColor *)blackColor {
    
    return HEXCOLOR(@"#444444");
}

+ (UIColor *)blueColor {
    
    return HEXCOLOR(@"#00AE85");
}

+ (UIColor *)whiteColor {
    
    return HEXCOLOR(@"#FFFFFF");
}

+ (UIColor *)yellowColor {
    
    return HEXCOLOR(@"#ffd905");
}

+ (UIColor *)backGrayColor {
    
    return HEXCOLOR(@"#EFEFEF");
}

+ (UIColor *)backWhiteColor {
    
    return HEXCOLOR(@"#FFFFFF");
}

+ (UIColor *)separatelineGrayColor {
    
    return HEXCOLOR(@"#E5E5E5");
}


+ (UIColor *)navigationBarBackColor {
    
    return HEXCOLOR(@"#FFFFFF");
}

+ (UIColor *)navigationBarTitleColor {
    
    return HEXCOLOR(@"#444444");
}

+ (UIColor *)hideViewColor {
    
    UIColor * color = nil;
    color = HEXCOLOR2(@"#000000",0.3);
    return color;
}

+ (UIColor *)shallowOrangeColor {
    
    UIColor * color = nil;
    color = HEXCOLOR(@"#FFDA81");
    return color;
}


#pragma mark - 颜色转换 IOS中十六进制的颜色转换为UIColor
+(UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha{
    if (alpha>1)alpha = 1;
    if (alpha<0)alpha = 0;
    
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // 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 clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    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:alpha];
}

+ (UIColor *)colorWithHexString:(NSString *)color {
    
    return [self colorWithHexString:color alpha:1];
}


+ (UIColor *)deepColorWithColor:(UIColor *)color {
    
    CGFloat R, G, B,A;
    CGColorRef colorRef = [color CGColor];
    size_t numComponents = CGColorGetNumberOfComponents(colorRef);
    
    if (numComponents == 4) {
        
        const CGFloat *components = CGColorGetComponents(colorRef);
        R = components[0];
        G = components[1];
        B = components[2];
        A = components[3];
    }
    return [UIColor colorWithRed:R-0.1 green:G-0.1 blue:B-0.1 alpha:A+0.1];
}

@end

2. 字体

AXFont.h

#import <Foundation/Foundation.h>

@interface AXFont : NSObject

/**
 *默认字体为苹方
 */
+ (UIFont *)defaultFont;

/**
 *默认字体为苹方
 */
+ (UIFont *)defaultFontWithSize:(CGFloat)size;

/**
 适配默认字体为苹方
 
 @param size 大小
 @return
 */
+ (UIFont *)defaultAdaptationFontWithSize:(CGFloat)size;

/**
 粗体苹方字体
 @param size 大小
 @return
 */
+ (UIFont *)defaultBoldFontWithSize:(CGFloat)size;

/**
 适配粗体大小
 
 @param size 大小
 @return
 */
+ (UIFont *)defaultAdaptationBoldFontWithSize:(CGFloat)size;

/**
 *最大标题字体大小
 */
+ (UIFont *)maxTitleFont;

/**
 *导航栏标题字体大小
 */
+ (UIFont *)navigationBarTitleFont;


/**
 *导航按钮字体大小
 */
+ (UIFont *)navigationBarButtonFont;

/**
 *主要文字字体大小
 */
+ (UIFont *)mainTextFont;

/**
 *次要文字字体大小
 */
+ (UIFont *)subTextFont;

@end

AXFont.m

#import "AXFont.h"

@implementation AXFont

+ (UIFont *)defaultFont {
    
    return [self defaultFontWithSize:44/3.0];
}

+ (UIFont *)defaultFontWithSize:(CGFloat)size {
    
    return [UIFont fontWithName:@".PingFang-SC-Regular" size:size];
}

+ (UIFont *)defaultAdaptationFontWithSize:(CGFloat)size {
    
    if (IPHONE4) {
        
        return [self defaultFontWithSize:size];
    } else if (IPHONE5) {
        
        return [self defaultFontWithSize:size];
    } else if (IPHONE6) {
        
        return [self defaultFontWithSize:size];
    } else if (IPHONE6PLUS) {
        
        return [self defaultFontWithSize:size];
    }
    return [self defaultFontWithSize:size];
}

+ (UIFont *)defaultBoldFontWithSize:(CGFloat)size {
    
    return [UIFont fontWithName:@".PingFang-SC-Medium" size:size];
}

+ (UIFont *)defaultAdaptationBoldFontWithSize:(CGFloat)size {
    
    if (IPHONE4) {
        
        return [self defaultBoldFontWithSize:size];
    } else if (IPHONE5) {
        
        return [self defaultBoldFontWithSize:size];
    } else if (IPHONE6) {
        
        return [self defaultBoldFontWithSize:size];
    } else if (IPHONE6PLUS) {
        
        return [self defaultBoldFontWithSize:size];
    }
    return [self defaultBoldFontWithSize:size];
}

+ (UIFont *)maxTitleFont {
    
    if (IPHONE4) {
        
        return [self defaultFontWithSize:59/3.0];
    } else if (IPHONE5) {
        
        return [self defaultFontWithSize:59/3.0];
    } else if (IPHONE6) {
        
        return [self defaultFontWithSize:30];
    } else if (IPHONE6PLUS) {
        
        return [self defaultFontWithSize:65/3.0];
    }
    return [self defaultFontWithSize:30];
}



+ (UIFont *)navigationBarTitleFont {
    
    //54/3
    if (IPHONE4) {
        
        return [self defaultBoldFontWithSize:18];
    } else if (IPHONE5) {
        
        return[self defaultBoldFontWithSize:18];
    } else if (IPHONE6) {
        
        return [self defaultBoldFontWithSize:18];
    } else if (IPHONE6PLUS) {
        
        return [self defaultBoldFontWithSize:18];
    }
    return [self defaultBoldFontWithSize:18];
}


+ (UIFont *)navigationBarButtonFont {
    
    if (IPHONE4) {
        
        return [self defaultBoldFontWithSize:16];
    } else if (IPHONE5) {
        
        return [self defaultBoldFontWithSize:16];
    } else if (IPHONE6) {
        
        return [self defaultBoldFontWithSize:16];
    } else if (IPHONE6PLUS) {
        
        return [self defaultBoldFontWithSize:16];
    }
    return [self defaultBoldFontWithSize:16];
}

+ (UIFont *)mainTextFont {
    
    if (IPHONE4) {
        
        return [self defaultBoldFontWithSize:14];
    } else if (IPHONE5) {
        
        return [self defaultBoldFontWithSize:14];
    } else if (IPHONE6) {
        
        return [self defaultBoldFontWithSize:14];
    } else if (IPHONE6PLUS) {
        
        return [self defaultBoldFontWithSize:14];
    }
    return [self defaultBoldFontWithSize:14];
}

+ (UIFont *)subTextFont {
    
    if (IPHONE4) {
        
        return [self defaultBoldFontWithSize:12];
    } else if (IPHONE5) {
        
        return [self defaultBoldFontWithSize:12];
    } else if (IPHONE6) {
        
        return [self defaultBoldFontWithSize:12];
    } else if (IPHONE6PLUS) {
        
        return [self defaultBoldFontWithSize:12];
    }
    return [self defaultBoldFontWithSize:12];
}

@end

其中用到的宏

#define HEXCOLOR(h)  [AXColor colorWithHexString:h]
#define HEXCOLOR2(h,a) [AXColor colorWithHexString:h alpha:a]

// 设备宏定义
// 判断是否为 iPhone 4s
#define IPHONE4 ([[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 480.0f)

// 判断是否为 iPhone 5SE
#define IPHONE5 ([[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f)

// 判断是否为iPhone 6/6s
#define IPHONE6 ([[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f)

// 判断是否为iPhone 6Plus/6sPlus
#define IPHONE6PLUS ([[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f)

3. 提示

UToAlert.h

#import <Foundation/Foundation.h>

@interface UToAlert : NSObject <UIAlertViewDelegate>

typedef void (^SXAlertCompleteBlock)(BOOL isOk);

@property(nonatomic,copy)SXAlertCompleteBlock completeBlock;

+ (instancetype)AlertTitle:(NSString *)title content:(NSString *)content cancelButton:(NSString *)canceltitle okButton:(NSString *)oktitle complete:(SXAlertCompleteBlock)block;

- (void)showAlertWithController:(UIViewController *)controller;

- (BOOL)isShow;

@end

UToAlert.m

#import "UToAlert.h"

NSString * const sxalertTitleKey = @"sxalertTitleKey";
NSString * const sxalertContentKey = @"sxalertContentKey";
NSString * const sxalertcancelKey = @"sxalertcancelKey";
NSString * const sxalertokKey = @"sxalertokKey";

@interface  UToAlert()

@property(nonatomic,strong)NSDictionary * infoDic;
@property(nonatomic,assign)BOOL showing;

@end

@implementation UToAlert

+ (instancetype)AlertTitle:(NSString *)title content:(NSString *)content cancelButton:(NSString *)canceltitle okButton:(NSString *)oktitle complete:(SXAlertCompleteBlock)block{
    UToAlert * al = [[UToAlert alloc] init];
    al.completeBlock = block;
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    
    if (title) {
        [dic setValue:title forKey:sxalertTitleKey];
    }
    if (content) {
        [dic setValue:content forKey:sxalertContentKey];
    }
    if (canceltitle) {
        [dic setValue:canceltitle forKey:sxalertcancelKey];
    }
    if (oktitle) {
        [dic setValue:oktitle forKey:sxalertokKey];
    }
    if (dic.count>0) {
        al.infoDic = [dic copy];
    }
    return al;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        self.completeBlock(YES);
    }
    _showing = NO;
}

- (UIAlertView *)alertView{
    UIAlertView * av = nil;
    if (self.infoDic.count>0) {
        av = [[UIAlertView alloc]initWithTitle:_infoDic[sxalertTitleKey] message:_infoDic[sxalertContentKey] delegate:self cancelButtonTitle:_infoDic[sxalertcancelKey] otherButtonTitles:_infoDic[sxalertokKey], nil];
    }
    return av;
}

- (UIAlertController *)alertController{
    UIAlertController * ac = nil;
    if (self.infoDic.count>0) {
        ac = [UIAlertController alertControllerWithTitle:_infoDic[sxalertTitleKey] message:_infoDic[sxalertContentKey] preferredStyle:UIAlertControllerStyleAlert];
        NSString * canceltitle = _infoDic[sxalertcancelKey];
        WEAKSELF
        if (canceltitle) {
            UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:canceltitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (weakSelf.completeBlock) {
                    
                    weakSelf.completeBlock(NO);
                }
                
                weakSelf.showing = NO;
            }];
            [ac addAction:cancelAction];
        }
        NSString * oktitle = _infoDic[sxalertokKey];
        if (oktitle) {
            UIAlertAction * okAction = [UIAlertAction actionWithTitle:oktitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (weakSelf.completeBlock) {
                    
                    weakSelf.completeBlock(YES);
                }
                weakSelf.showing = NO;
            }];
            [ac addAction:okAction];
        }
    }
    return ac;
}

- (void)showAlertWithController:(UIViewController *)controller{
    // iOS8 下需要使用新的 API
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIAlertController * ac = [self alertController];
        [controller presentViewController:ac animated:YES completion:nil];
        _showing = ac != nil;
    }else {
        UIAlertView * av = [self alertView];
        [av show];
        _showing = av != nil;
        
    }
}

- (BOOL)isShow{
    return _showing;
}

@end
上一篇下一篇

猜你喜欢

热点阅读