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.


    Is it possible to present an alert with multicolored text in a Pythonista script?

    Pythonista
    4
    9
    1557
    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.
    • jaalburquerque
      jaalburquerque last edited by jaalburquerque

      I’m wondering if it is possible to use something like console.alert() to show the user some text rendered in different colors. Would someone have an idea as to how to achieve this as simply and easily as possible? I would really appreciate it. Thanks so much for taking the time to answer.

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

        I believe the dialogs module (or easy mod to dialogs) would allow changing tint color. It would also be possible to show formatted text, though that is a little more complex.

        cvp jaalburquerque Johndavidson9 3 Replies Last reply Reply Quote 0
        • cvp
          cvp @JonB last edited by

          @JonB yes, but dialogs presents a modal view and if you tap outside, you close it.
          Console.alert is not closed if you tap outside, why?

          1 Reply Last reply Reply Quote 1
          • jaalburquerque
            jaalburquerque @JonB last edited by

            @JonB Thanks for the reply. Would changing the tint color make it possible to display multicolored text? I would prefer something like an alert, but I would use a dialog if it allows displaying the text in a mixture of various colors.

            cvp 3 Replies Last reply Reply Quote 0
            • cvp
              cvp @jaalburquerque last edited by cvp

              @jaalburquerque tint_color seems to work only for switch and check fields types. Sorry for you

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

                @jaalburquerque a Quick and dirty script as starting point for you.

                import ui
                
                def myalert(title, message, button1, button2=None, button3=None, hide_cancel_button=False):
                
                	v = ui.View()
                	v.background_color = 'white'
                	w = 270
                	y = 10
                	lt = ui.Label()
                	lt.font = ('Menlo',16)
                	lt.text_color = 'red'
                	lt.text = title
                	lt.alignment = ui.ALIGN_CENTER
                	lt.number_of_lines = 0
                	y += 10
                	lt.frame = (10,y,w-2*10,lt.font[1])
                	lt.size_to_fit()
                	ht = lt.height
                	lt.frame = (10,y,w-2*10,ht)
                	v.add_subview(lt)	
                	y += ht
                	
                	lm = ui.Label()
                	lm.font = ('Menlo',16)
                	lm.text_color = 'green'
                	lm.text = message
                	lm.alignment = ui.ALIGN_CENTER
                	lm.number_of_lines = 0
                	y += 10
                	lm.frame = (10,y,w-2*10,lt.font[1])
                	lm.size_to_fit()
                	hm = lm.height
                	lm.frame = (10,y,w-2*10,hm)
                	v.add_subview(lm)	
                	y += hm
                	
                	li = ui.Label()
                	y += 10
                	li.frame = (0,y,w,1)
                	li.border_width = 1
                	li.border_color = 'lightgray'
                	v.add_subview(li)
                	
                	b = ui.SegmentedControl()
                	segments = []
                	if not hide_cancel_button:
                		segments.append('cancel')  
                	if button1:
                		segments.append(button1)  
                	if button2:
                		segments.append(button2)  
                	if button3:
                		segments.append(button3)  
                	x = 10 
                	y += 10
                	wb = (w - 10*(len(segments)+1))/len(segments)
                	v.ret = None
                	def b_action(sender):
                		v.ret = sender.ret
                		v.close()
                	for i in range(len(segments)):
                		b = ui.Button()
                		b.font = ('<System-Bold>', 20)
                		b.title = segments[i]
                		b.ret = i
                		b.action = b_action
                		b.frame = (x,y,wb,24)
                		v.add_subview(b)
                		if i < (len(segments)-1):
                			lv = ui.Label()
                			lv.border_width = 1
                			lv.border_color = 'lightgray'
                			lv.frame = (x+wb+5,li.y,1,y+b.height+10-li.y)
                			v.add_subview(lv)
                		x += wb + 10
                			
                	y += b.height + 10
                	v.frame = (0,0,w,y)
                	
                	v.present('sheet', hide_title_bar=True)
                	v.wait_modal()
                	return v.ret
                
                b = myalert('title, up to you to add color and even font parameters','message, supports long texts', button1='yes', button2='no')
                print(b)
                

                1 Reply Last reply Reply Quote 1
                • cvp
                  cvp @jaalburquerque last edited by

                  @jaalburquerque as adviced by @JonB , you can use ObjectiveC text attributes

                  import random
                  from objc_util import *
                  .
                  .
                  .
                  	lt.text = title	
                  	
                  	# set color attributes
                  	attrtext = ObjCClass('NSMutableAttributedString').alloc().initWithString_(title)
                  	for i in range(len(title)):
                  		color = ObjCClass('UIColor').colorWithRed_green_blue_alpha_(random.random(), random.random(), random.random(), 1.0)
                  		attrtext.addAttribute_value_range_('NSColor',color,NSRange(i, 1))
                  	ObjCInstance(lt).setAttributedText_(attrtext)
                  
                  

                  jaalburquerque 1 Reply Last reply Reply Quote 1
                  • jaalburquerque
                    jaalburquerque @cvp last edited by

                    @cvp Okay. I believe that you have both given me enough information and exemplification to achieve what I am trying to do. I believe that the code particularly will be very useful to me. Thank you both very much.

                    1 Reply Last reply Reply Quote 1
                    • Johndavidson9
                      Johndavidson9 @JonB last edited by Johndavidson9

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