Forum Archive

UIColorPickerViewController In iOS 14

cvp

If you want the new iOS 14 Color Picker, please try

# coding: utf-8
from objc_util import *
import ui

def colorPickerViewControllerDidFinish_(_self, _cmd, _controller):
    #print('colorPickerViewControllerDidFinish')
    UIColorPickerViewController = ObjCInstance(_controller)

def colorPickerViewControllerDidSelectColor_(_self, _cmd, _controller):
    #print('colorPickerViewControllerDidSelectColor')
    UIColorPickerViewController = ObjCInstance(_controller)
    cl = UIColorPickerViewController.selectedColor()
    rgb = (cl.red(),cl.green(),cl.blue())
    coloredButtonItem(UIColorPickerViewController.buttonItem, rgb)

methods = [colorPickerViewControllerDidFinish_, colorPickerViewControllerDidSelectColor_]
protocols = ['UIColorPickerViewControllerDelegate']
try:
        MyUIColorPickerViewControllerDelegate = ObjCClass('MyUIColorPickerViewControllerDelegate')
except Exception as e:
    MyUIColorPickerViewControllerDelegate = create_objc_class('MyUIColorPickerViewControllerDelegate', methods=methods, protocols=protocols)

def coloredButtonItem(btn,rgb):
    import ui
    if rgb:
        with ui.ImageContext(32,32) as ctx: 
            path = ui.Path.rect(0,0,32,32)
            ui.set_color(rgb)
            path.fill()     
            btn.image = ctx.get_image().with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)

def UIColorPickerViewController(w, h, alpha_slider=True, title='', rgb=None, popover=None):
    v = ui.View()
    v.name = title
    v.rgb = rgb
    vc = ObjCInstance(v)
    colorpicker = ObjCClass('UIColorPickerViewController').new().autorelease()
    #print(dir(colorpicker))
    delegate = MyUIColorPickerViewControllerDelegate.alloc().init()
    colorpicker.delegate = delegate 

    colorpicker.setSupportsAlpha_(alpha_slider) 
    if rgb:
        color = ObjCClass('UIColor').colorWithRed_green_blue_alpha_(rgb[0], rgb[1], rgb[2], 1.0)
        colorpicker.setSelectedColor_(color)

    clview = colorpicker.view()
    v.frame = (0,0,w,h)
    vc.addSubview_(clview)
    done_button = ui.ButtonItem(title='ok')
    def tapped(sender):
        cl = colorpicker.selectedColor()
        v.rgb = (cl.red(),cl.green(),cl.blue())
        v.close()
    done_button.action = tapped
    color_button = ui.ButtonItem()
    coloredButtonItem(color_button,rgb)
    v.right_button_items = [done_button,color_button]

    colorpicker.buttonItem = color_button

    if popover:
        x,y = popover
        v.present('popover', popover_location=(x,y))
    else:
        v.present('sheet')

    v.wait_modal()
    return v.rgb    

# Protect against import    
if __name__ == '__main__':
    mv = ui.ImageView()
    mv.name = 'Test UIColorPickerViewController'
    mv.image = ui.Image.named('test:Peppers')
    b1 = ui.ButtonItem()
    b1.title = '๐ŸŽจ'
    def b1_action(sender):
        w,h = ui.get_screen_size()
        rgb = UIColorPickerViewController(w,h,alpha_slider=True,title='Select a color')
    b1.action = b1_action
    b2 = ui.ButtonItem()
    b2.title = '๐Ÿ’‰'
    def b2_action(sender):
        # compute x,y of an ui.ButtonItem
        vi = ObjCInstance(sender).view()
        r = vi.convertRect_toView_(vi.bounds(),ObjCInstance(mv))
        # x,y in screen coordinates, not in view
        x,y,w,h = r.origin.x,r.origin.y,r.size.width,r.size.height
        x,y = x+w/2, y+h
        w,h = 200,50
        rgb = UIColorPickerViewController(w,h, alpha_slider=False, popover=(x,y))
    b2.action = b2_action
    mv.right_button_items =(b1,b2)
    mv.present('fullscreen')

mikael

@cvp, well done! I had some problems with it, I think the wait_modal is the trick.

cvp

@mikael no problem for me but I use sheet and popover and you are on iPhone, aren't you?

cvp

And I don't know why I have two topics and the 2nd one is not editable

mikael

@cvp, by the way, the one I was really excited about in iOS 14 was the inline date/time picker style, but could not get it to be anything except the good old big rollers.

ingiestein

@cvp Amazing! I had been looking for a color picker solution like this. Thank you!

cvp

@mikael said:

inline date/time picker style, but could not get it to be anything except the good old big rollers.

Sorry, I didn't understand. You did not succeed to get it?

ร‰dit: Perhpas, does Pythonista need to be upgraded?