omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Change Color of selected segmentcontrol

    Editorial
    5
    8
    2795
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Killerburns
      Killerburns last edited by

      Hey guys,
      I’am quite new to pythonista and i already love it. Everything is well documented and there is a thread for everything you need in this Forum. Except for this special requirement. For my own App í´m trying to figure out how i can set the color of the selected segment.

      During my search i found this topic https://forum.omz-software.com/topic/5672/custom-font-for-ui-segmentedcontrol and i was able to get this to work in my code. But unfortunately its only the font type, color and size. But if we can access theese attributes wouldnt we be able to change the color of the selection as well.?

      I tried with the attribute nsforegroundcolorattributename, but without succes. And i have to admit, I have no experience with object C.

      So has anyone a clue how i can get this to work? I would really appreciate it :)

      Best wishes

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @Killerburns last edited by cvp

        @Killerburns read this topic

        1 Reply Last reply Reply Quote 0
        • Killerburns
          Killerburns last edited by

          Hi cvp,
          Thanks for your reply. I have already seen this thread, and as i mentioned was able to change color of the font as you describe it in the the thread you have posted. If its about the „get back ios 12 appearance“ i really have no idea how to use that :( I‘am really a beginner.

          I thought about to build a own segmented control with buttons and ui animate if this is too complicated to get it work with the build in segmented control.

          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @Killerburns last edited by cvp

            @Killerburns Try this, almost nothing in Objective C 😅

            import ui
            from objc_util import *
            
            v = ui.View()
            v.frame = (0,0,400,200)
            v.background_color = 'white'
            
            d = 64
            items = ['aaa','bbb']
            sc = ui.SegmentedControl()
            sc.frame = (10,10,d*len(items),d)
            
            sc.segments = items
            def sc_action(sender):
            	idx = sender.selected_index
            	o = ObjCInstance(sender).segmentedControl()
            	#print(dir(o))
            	idx = o.selectedSegmentIndex()
            	for i in range(len(items)):
            		if i == idx:
            			with ui.ImageContext(d,d) as ctx:
            				path = ui.Path.rounded_rect(0,0,d,d,5)
            				ui.set_color('red')
            				path.fill()
            				s = 12
            				ui.draw_string(items[idx], rect=(0,(d-s)/2,d,s), font=('Menlo', s), color='black', alignment=ui.ALIGN_CENTER)
            				ui_image = ctx.get_image().with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
            			o.setImage_forSegment_(ui_image.objc_instance,idx)
            		else:
            			o.setTitle_forSegmentAtIndex_(items[i],i)
            sc.action = sc_action
            v.add_subview(sc)
            
            v.present('sheet')
            

            1 Reply Last reply Reply Quote 0
            • Killerburns
              Killerburns last edited by Killerburns

              Thanks @cvp for spending your time to help me out here. Youre brilliant :). There is only one thing that disturbs a bit. You still can see the indicator moving behind the red path and its still white which doesnt look nice on a dark theme. Maybe there is a solution to animate this rectangular shape to the position of the selected index, so that the indicator is always covered?

              I think for now i will build my own segmented control with buttons, labels and a colored custom view with a background color that animates x to the button that has been pressed. Thats a dirty solution but this way i know what i‘am doing :D

              But if you or someone else wants find a perfect and effectiv solution for this, feel free to do so and share it with us :)

              Thanks again for your support
              Best wishes and
              Stay safe

              ///

              Almost the same now :P but dirty solution

              https://imgur.com/Qs8p9ta

              import ui
              
              
              def segment_tapped(sender):
              	x = sender.x
              	
              	if sender.name == "FireTv":
              		v['segmentedCtrl_input']['idx'].width = 56
              		x = x+2
              		
              	elif sender.name == "Musik":
              		v['segmentedCtrl_input']['idx'].width = 57  
              		x = x-1
              				
              	else: 	
              		v['segmentedCtrl_input']['idx'].width = 59
              		
              	animate_movex(v['segmentedCtrl_input']['idx'],x)
              	
              	for a in buttons:
              		if a.name == sender.name:
              			a.font = ('Avenir-Heavy', 13)
              		else:
              			a.font = ('Avenir-Book', 13)
              
              		
              def animate_movex(v, x):
              	def a():
              		v.x = x
              	ui.animate(a, 0.3)
              		
              v = ui.load_view()
              buttons = v['segmentedCtrl_input'].subviews
              
              v['segmentedCtrl_input']['idx'].background_color = '#92b8ff'
              
              for a in buttons:
              	a.font = ('Avenir-Book', 13)
              	
              v.present('sheet') ```
              1 Reply Last reply Reply Quote 0
              • Gagnon_265
                Gagnon_265 last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • myagkayaanna
                  myagkayaanna last edited by

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • marknany
                    marknany last edited by

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors