iOS颜色选择器

1.导入ANImageBitmapRep库(或者文件下的ColorPickerClasses)

2.用法

FColorPickerView.h

//
//  FColorPickerView.h
//  Foowwphone
//
//  Created by Jacky-MAC on 15/7/7.
//  Copyright (c) 2015年 Fooww. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "RSColorPickerView.h"
#import "RSBrightnessSlider.h"

@interface FColorPickerView : UIViewController <RSColorPickerViewDelegate,UITextFieldDelegate>

@property (nonatomic,copy) NSString * colorCode;
@property (nonatomic,strong) UIColor * selectColor;
@property (nonatomic,copy) void (^sendColorInfo)(UIColor * sc,NSString * cc);

@end

FColorPickerView.m

//
//  FColorPickerView.m
//  Foowwphone
//
//  Created by Jacky-MAC on 15/7/7.
//  Copyright (c) 2015年 Fooww. All rights reserved.
//

#import "FColorPickerView.h"
#import "ApiConnectionDelegate.h"
#import "ProgressHUD.h"

@interface FColorPickerView ()

{
    NSInteger _upH;
    BOOL _upView;
    BOOL _shouldU;
    NSInteger _screenH;
    NSInteger _screenW;
    UIView * _oldColorV;
    UIView * _newColorV;
}
@property (nonatomic,strong) UIColor * oldColor;
@property (nonatomic,strong) RSColorPickerView * colorPicker;
@property (nonatomic,strong) RSBrightnessSlider * brightnessSlider;
@property (nonatomic,strong) UITextField * colorCodeText;

@end

@implementation FColorPickerView

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"选择背景色";
    self.view.backgroundColor=grayLogColor;

    _shouldU=NO;
    _screenW=ScreenWidth;
    _screenH=ScreenHeight;
    if (isBeforeIOS7)
    {
        _upH=0;
    }else
    {
        _upH=64;
    }

    _upView=NO;

    [self createInterface];

    if (isBeforeIOS7)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(15,5,22,22);
        [btn setBackgroundImage:[UIImage imageNamed:@"icon_back_left"] forState:UIControlStateNormal];
        [btn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
        UIBarButtonItem*back=[[UIBarButtonItem alloc]initWithCustomView:btn];
        self.navigationItem.leftBarButtonItem=back;
    }
}

-(void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
}

-(void)createInterface
{
    self.colorPicker = [[RSColorPickerView alloc] initWithFrame:CGRectMake(10.0, 10.0, 250.0, 250.0)];
    [self.colorPicker setDelegate:self];
    [self.colorPicker setBrightness:1.0];
    [self.colorPicker setCropToCircle:NO];
    [self.colorPicker setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.colorPicker];

    self.brightnessSlider = [[RSBrightnessSlider alloc] initWithFrame:CGRectMake(155.0, 120.0, 250.0, 30.0)];
    [self.brightnessSlider setColorPicker:self.colorPicker];
    [self.brightnessSlider setUseCustomSlider:NO];
    [self.view addSubview:self.brightnessSlider];

    self.colorCodeText=[[UITextField alloc]initWithFrame:CGRectMake(10, 270, 250, 30)];
    self.colorCodeText.returnKeyType = UIReturnKeyDone;
    self.colorCodeText.text=[NSString stringWithFormat:@"#%@",self.colorCode];
    self.colorCodeText.layer.borderColor=[[UIColor grayColor] CGColor];
    self.colorCodeText.layer.borderWidth=1;
    self.colorCodeText.layer.cornerRadius=4.0;
    self.colorCodeText.delegate=self;
    self.colorCodeText.tag=101;
    [self.view addSubview:self.colorCodeText];

    UIColor * oldColor=[self colorWithColorString:self.colorCode];
    _oldColorV=[[UIView alloc]initWithFrame:CGRectMake(10, 305, 120, 30)];
    _oldColorV.backgroundColor=oldColor;
    [self.view addSubview:_oldColorV];

    _newColorV=[[UIView alloc]initWithFrame:CGRectMake(140, 305, 120, 30)];
    _newColorV.backgroundColor=oldColor;
    [self.view addSubview:_newColorV];

    if (isBeforeIOS7)
    {
        UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        changeBtn.frame = CGRectMake(ScreenWidth-50,2,40,40);
        [changeBtn setTitle:@"确定" forState:UIControlStateNormal];
        changeBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [changeBtn addTarget: self action: @selector(saveSelectColor) forControlEvents: UIControlEventTouchUpInside];
        UIBarButtonItem*changeBarBtn=[[UIBarButtonItem alloc]initWithCustomView:changeBtn];
        self.navigationItem.rightBarButtonItem=changeBarBtn;
    }else
    {
        UIBarButtonItem * changeBtn=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(saveSelectColor)];
        self.navigationItem.rightBarButtonItem = changeBtn;
    }
}
-(void)saveSelectColor
{

    self.sendColorInfo(self.selectColor,self.colorCode);
    [self goBackAction];
}

-(void)colorPickerDidChangeSelection:(RSColorPickerView *)cp
{
    if (_shouldU)
    {
        self.selectColor=[cp selectionColor];
        self.colorCode=[self getColorCodeWithColor:self.selectColor];
        self.colorCodeText.text=[NSString stringWithFormat:@"#%@",self.colorCode];
        _newColorV.backgroundColor = self.selectColor;
    }else
    {
        _shouldU=YES;
    }
}

#pragma mark-TextFieldDelegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self textResignFirstResponder];
}
-(void)textResignFirstResponder
{
    [self.colorCodeText resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSInteger t,h,b;
    t=textField.tag-100;
    h=270+t*35;
    b=_screenH-64-h-216;

    if (b < 0)
    {
        _upView=YES;
        self.view.frame=CGRectMake(0, b, _screenW, _screenH);
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (_upView)
    {
        self.view.frame=CGRectMake(0, _upH, _screenW, _screenH);
    }

    if ([self checkTextStringLength])
    {
        [self inputCodeChangeToColor];
    }

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
    return YES;
}

#pragma mark-ColorOperation
-(UIColor *)colorWithColorString:(NSString *)colorStrig //16进制转UIColor
{
    NSString *cString = [[colorStrig stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    if ([cString length] < 6)
    {
        return [UIColor whiteColor];
    }

    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }

    if ([cString length] != 6)
    {
        return [UIColor whiteColor];
    }

    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];

    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    if ([self checkTextNumberWithRed:r green:g blue:b])
    {
        return [UIColor colorWithRed:((float) r / 255.0f)
                               green:((float) g / 255.0f)
                                blue:((float) b / 255.0f)
                               alpha:1.0f];
    }else
    {
        [ProgressHUD showError:@"颜色代码错误"];
        return [UIColor whiteColor];
    }
}
-(NSString *)getColorCodeWithColor:(UIColor *)color
{
    CGFloat r, g, b, a;
    [color getRed:&r green:&g blue:&b alpha:&a];
    int rgb = (int) (r * 255.0f)<<16 | (int) (g * 255.0f)<<8 | (int) (b * 255.0f)<<0;
    NSString * code = [NSString stringWithFormat:@"%06x", rgb];
    return code;
}
-(BOOL)checkTextStringLength
{
    BOOL c;
    NSString * r=self.colorCodeText.text;

    if (r.length == 7)
    {
        c=YES;
    }else
    {
        c=NO;
    }

    return c;
}
-(BOOL)checkTextNumberWithRed:(NSInteger)r green:(NSInteger)g blue:(NSInteger)b
{
    BOOL c;
    if (r>=0 && r<=255 && g>=0 && g<=255 &&b>=0 && b<=255)
    {
        c=YES;

    }else
    {
        c=NO;
    }
    return c;
}
-(void)inputCodeChangeToColor
{
    _shouldU=NO;
    self.colorCode = [self.colorCodeText.text substringFromIndex:1];
//    self.colorCode = self.colorCodeText.text;
    NSLog(@"----%@",self.colorCode);
    self.selectColor=[self colorWithColorString:self.colorCode];
    [self.colorPicker setSelectionColor:self.selectColor];
    [_newColorV setBackgroundColor:self.selectColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

时间: 2024-11-13 06:57:45

iOS颜色选择器的相关文章

jquery颜色选择器

本站下载 第二种:纯JAVASCRIPT: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2311"> <title>DW调色板</title> <script> var ColorHex=new Array('00','33','66','99','CC','FF') var SpColo

在线颜色选择器

在做web开发的时候,有时需要自己解决配色问题,但又不知道某一种颜色对应的代码,这时在线颜色选择器就有了用武之地. 这里推荐的是我经常用的一个网站:ATOOL在线工具

Android 颜色渲染(一) 颜色选择器 ColorPickerDialog剖析

版权声明:本文为博主原创文章,未经博主允许不得转载. Android 颜色选择器之ColorPickerDialog剖析 有这样一个需求,可以让用户自定义背景颜色,这就需要提供一个颜色选择器给用户. 在Android 中,如何实现这样的功能呢,遇到这种需求是,先查看一下ApiDemos,是否已经有相关的实例,果然,找到了一个可以参考的demo:ColorPickerDialog 我已经把apidemos导入到eclipse中,请看截图: 我们要找的就是这个ColorPickerDialog,在c

Android圆环形颜色选择器:HoloColorPicker

HoloColorPicker实现圆环形颜色选择器,可以改变颜色饱和度来选择颜色.选择颜色时,可以用手指沿着圆环滑动一个滑块,从而选择颜色. 添加以下XML至你的布局中: ? 1 2 3 4 <com.larswerkman.holocolorpicker.ColorPicker     android:id="@+id/picker"     android:layout_width="wrap_content"     android:layout_hei

UWP 颜色选择器(ColorPicker) 和 自定义的Flyout(AdvancedFlyout)

ColorPicker 故事背景 项目里面需要一个像Winfrom里面那样的颜色选择器,如下图所示: 在网上看了一下.没有现成的东东可以拿来使用.大概查看了一下关于颜色的一些知识,想着没人种树,那就由我自己来种树,大家来乘凉好了. 设计过程 由于要考虑到手机上的效果,所以说这种向右展开的方式,不是太合适手机,所以最外层我考虑使用Pivot来存放基本颜色和自定义颜色这2页. 第一页是基本颜色,第二页是自定义的颜色,如下图. ColorPicker这个控件,主要是由一个Button以及FlyoutB

一步步教你实现跨游览器的颜色选择器

原文:http://www.cnblogs.com/rubylouvre/archive/2009/07/24/1530264.html#2580430 <!doctype html> <html dir="ltr" lang="zh-CN"> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible"

Swift调用Objective-C编写的代码(颜色选择器KKColorListPicker调用)

在Swift项目中,我们可以导入任意用Objective-C写的框架,代码库等.下面以Swift调用Objective-C编写的颜色选择器KKColorListPicker为例. 效果图如下:      实现步骤: 1,首先在项目里导入KKColorListPicker的源码(整个文件夹,在项目上有键“Add Files To XXX”). 2,手工创建桥接头文件bridge.h来包含需要引用的Objective-C头文件,内容如下: 1 2 3 //KKColorsSchemeType.h已经

java swing颜色选择器代码例子下载

原文:java swing颜色选择器代码例子下载 源代码下载地址:http://www.zuidaima.com/share/1550463730420736.htm 矿泉水2013-09-13 17:17:13 java swing颜色选择器代码例子下载

基于vue的颜色选择器vue-color-picker

项目中有用到颜色选择器的童鞋们可以看过来了 关于color-picker的jquery的插件是有蛮多,不过vue组件没有吧,反正我没有找到, 虽然element-ui里面有这个,但是你愿意为了一个小功能去引入这么大的依赖库吗? 适用于:想写基于vue的单个color-picker功能的童鞋 甩github地址啦:https://github.com/alexzaolin/vue-color-picker (麻烦觉得有用的童鞋帮我点个star,谢谢谢谢谢~~) 凑个字数贴一下代码(部分算法来自网络