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.


    Flex label size

    Pythonista
    5
    5
    1494
    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.
    • mikael
      mikael last edited by mikael

      Added a constraint that updates the label height to fit the text when label width changes:

      at(label).text_height = at(label).width
      

      Flexing label

      ... and the opposite, for completeness, flexing width when the height changes.

      Using these tools and Samer's PageControl wrapper, I took a stab at creating something like @resserone13's Protective Equipment browser, that scrolls and rotates every which way as needed.

      Info browser

      Here's the code:

      import faker
      import ui
      
      from ui3.anchor import at, attr, dock
      from ui3.pagecontrol import PageControl
      from ui3.safearea import SafeAreaView
      
      
      def create_page(scroll_view, title_text, image_name, body_text):    
          container = ui.View()
          dock(container).sides(scroll_view)
          
          title = ui.Label(text=title_text,
              text_color='white', alignment=ui.ALIGN_CENTER,
          )
          title.size_to_fit()
          dock(title).top(container)
          
          image_area = ui.ImageView(image=ui.Image(image_name),
              content_mode=ui.CONTENT_SCALE_ASPECT_FIT,
          )
          dock(image_area).below(title)
          at(image_area).height = at(scroll_view).height / 4
          body = ui.Label(text=body_text,
              text_color='white', number_of_lines=0,
          )
          dock(body).below(image_area)
          
          at(body).text_height = at(body).width
          at(container).fit_height = at(body).frame
          attr(scroll_view).content_size = at(container).size
          
          return scroll_view
         
              
      fake = faker.Faker()
      
      root = SafeAreaView(background_color='black')
      pages = PageControl(frame=root.bounds, flex='WH')
      root.add_subview(pages)
      
      for i, image_name in enumerate(['test:Boat', 'test:Lenna', 'test:Mandrill', 'test:Peppers']):
          scroll_view = ui.ScrollView(
              background_color='black',
          )
          pages.add_subview(scroll_view)
          create_page(
              scroll_view,
              f"Page {i+1}",
              image_name,
              "\n\n".join([fake.text() for _ in range(6)]),
          )
      root.present('fullscreen', hide_title_bar=True)
      
      1 Reply Last reply Reply Quote 1
      • resserone13
        resserone13 last edited by

        This is really nice. I see the difference in using ui module over the scene module for this type of thing.

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

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

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

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