2013-11-26 52 views
-2

我使用Xcode来制作我的第一个应用程序。我想为我的文本添加RGB颜色选择器,因此我可以使用颜色选择器更改文本的颜色。我该怎么做?如何创建颜色选择器

谢谢你的时间! :)

回答

1

看看GitHub。你会发现gazillion颜色选择器在那里。这是第一个,我发现搜索时:

https://github.com/RSully/RSColorPicker

如果你是编程新手,你应该或许与内置的UI组件坚持。您可以使用两个UIButton来让用户在“红色”和“黑色”之间选择,并直接从按钮操作中设置文本颜色。

我认为一个完整的颜色选择器是您的第一个应用程序的大项目。

一个简单的实现可以简单地创建一个循环遍历可能的颜色的颜色井。

UIView subclass with colour wells

的下面ColorPickerViewUIView子类,它示出了这一点。

#import "ColorPickerView.h" 

@implementation ColorPickerView 

- (void)drawRect:(CGRect)rect { 

    // Create a grid of n*n wells each with a seperate color -- 
    const int numberOfWells = 20; 
    const int totalWells = numberOfWells * numberOfWells; 

    // Figure out the size of each well -- 
    const CGSize size = self.bounds.size; 
    const CGFloat boxHeight = floorf(size.height/numberOfWells); 
    const CGFloat boxWidth = floorf(size.width/numberOfWells); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Loop through all the wells -- 
    for(int y = 0; y < numberOfWells; y++) { 
     for(int x = 0; x < numberOfWells; x++) { 

      int wellNumber = x + numberOfWells * y; 

      // Assign each well a color -- 
      UIColor *boxColor = [self colorForWell:wellNumber ofTotal:totalWells]; 
      [boxColor setFill]; 

      CGRect box = CGRectMake(x*boxWidth, y*boxHeight, boxWidth, boxHeight); 
      CGContextAddRect(context, box); 
      CGContextFillRect(context, box); 

     } 
    } 

} 

-(UIColor*) colorForWell:(int) well ofTotal:(int) wells { 

    CGFloat red = (CGFloat) well/wells; 
    CGFloat green = well % (wells/3)/(CGFloat) (wells/3); 
    CGFloat blue = well % (wells/9)/(CGFloat) (wells/9); 

    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; 
} 

@end 

让用户点击颜色并从触摸位置推断颜色。

+1

谢谢你,你帮了我很多! – Yarondani

-4

教你如何制作颜色选择器是不可能的。你应该学习Objective C,并且可能会看到一些现有的开源项目,这些项目是你想学习如何制作你自己的(如果你不想使用和编辑现有的)... 看看这些Color Pickers