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.


    How to add a new line in a button title

    Pythonista
    2
    4
    2691
    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.
    • sskaye
      sskaye last edited by

      I'd like to have my button title wrap over two lines. Normally, I'd use the newline character in a text string (e.g. 'Line1 \n Line2'), but that doesn't work here.

      Is there any way to insert a newline or have the text wrap?

      Thanks.

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

        Sorry, forgot to add that I made the button and am editing the title in the UI editor.

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

          By default, iOS buttons only allow a single line of text, any extra lines are silently hidden. Pythonista's ui.Button uses this default behavior and doesn't provide a way to change that. You can use objc_util to change the behavior on the underlying label:

          import objc_util
          import ui
          
          NSLineBreakByWordWrapping = 0
          NSLineBreakByCharWrapping = 1
          NSLineBreakByClipping = 2
          NSLineBreakByTruncatingHead = 3
          NSLineBreakByTruncatingTail = 4
          NSLineBreakByTruncatingMiddle = 5 # Default for button labels.
          
          b = ui.Button() # Your button (doesn't need to be created here, can come from somewhere else, like a UI file).
          objc_util.ObjCInstance(b).button().titleLabel().setLineBreakMode(NSLineBreakByWordWrapping) # You can use any of the line break modes listed above.
          

          Also, I'm not sure if the UI designer recognizes escape sequences like \n in button titles and such. If you see a literal \n appearing in the button title instead of a newline, try setting the button title in code (b.title = "line 1\nline 2") instead of in the UI designer.

          1 Reply Last reply Reply Quote 1
          • sskaye
            sskaye last edited by

            Worked perfectly. Thanks!

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