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.


    TextView to scroll automatically when text is added?

    Pythonista
    pythonista3 pyui ui.textview
    3
    4
    1281
    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.
    • AAnkeriasniemi
      AAnkeriasniemi last edited by

      Hello

      What I’m trying to do is a console view for the ui. It wouldn’t be that nice for it to not scroll when something is printed out of the bounds, in this case the user would have to scroll down manually every time text is printed.

      An obvious solution to this would be to print so that the old text would go out of the bounds, and new text would be printed above the old. But I don’t want this because that’s not how a standard console works like and can confuse many users: textview.text = f”{newtextstring}\n{textview.text}”

      This is the current method for me to print text to the makeshift console:

      def fakeprint(newtextstring):
        consoletextview.text = f”{consoletextview.text}\n{newtextstring}”
      

      but because there will be be so many rows after the print-text height has reached the textview-height that the print-text will continue downwards and then the user will have to scroll down manually to see new prints. Basically, I want it to scroll along with new prints so that the user won’t have to, any ideas?

      Let me know if you can’t understand this.

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

        You might consider installing stash, and using that as your display. I believe some of the classes used in pythonista_ui.py can be used standalone, and give you color, scrolling, etc

        The scrolling code in ShTerminal looks like

            @on_main_thread
            def scroll_to_end(self):
                content_height = self.content_size[1]
                # rect_height is the visible rect's height
                # rect_y is the y location where the visible rect locates in the
                # coordinate of content_size
                _, rect_height, _, rect_y = self.visible_rect
                # If the space below rect_y is more than the visible rect's height,
                # or if the visible rect is over-scrolled, scroll to the last line.
                if content_height - rect_y > rect_height or \
                        (content_height > rect_height > content_height - rect_y):  # over-scroll
                    self.tvo.scrollRangeToVisible_((len(self.text), 0))
        

        Where self.tvo is the textview's .objc_instance.

        Textview's are subclasses of scroll views, so the content_inset, etc allow you to control the scroll.

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

          @AAnkeriasniemi following @JonB 's advice, test this

          import ui
          from objc_util import *
          
          class MyTextFieldDelegate():
          	def textfield_should_return(self, textfield):
          		global consoletextview
          		consoletextview.text += f"\n{textfield.text}"
          		ObjCInstance(consoletextview).scrollRangeToVisible_((len(consoletextview.text), 0))
          		textfield.text = ''
          		#textfield.end_editing()
          		return True
          
          v = ui.View()
          v.background_color = 'white'
          v.frame = (0,0,400,400)
          consoletextview = ui.TextView()
          consoletextview.frame = (0,0,400,370)
          v.add_subview(consoletextview)
          tf = ui.TextField()
          tf.frame = (0,370,400,30)
          tf.delegate = MyTextFieldDelegate()
          v.add_subview(tf)
          tf.begin_editing()
          v.present('sheet')
          
          1 Reply Last reply Reply Quote 2
          • AAnkeriasniemi
            AAnkeriasniemi last edited by

            Thank you both! It works now

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors