how to use complex labels in a button

Discussion in 'iOS Programming' started by theprizerevealed, Nov 11, 2016.

  1. theprizerevealed, Nov 11, 2016
    Last edited: Nov 11, 2016

    theprizerevealed macrumors member

    Joined:
    Feb 26, 2016
    #1
    I wonder how to add complex labels to a button. I know that you can do it in the default label in the Attributes Inspector but I wish to do it programmatically also.

    By a complex label I mean newspaces, tabspaces, newlines, et cetera; along with the text of the label. I presume this is stored somewhere by the Attributes Inspector but I don't know where to find it.

    In any case, I wish to store in a string text that would be on several lines, with tabspaces and newspaces. Can strings store information in that manner in Swift 3? thanks for your time and patience

    postscript: is there any free program for mac that shows all carriage return, newlines, tablines, etc?
     
  2. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #2
    I would suggest using a normal UILabel, then add an invisible UIButton on top of it and use Auto Layout to make sure it's always on top.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Pretty sure the standard UIButton only allows one line of text. You can add styled text to labels and other classes using NSAttributedString and setAttributedText. Maybe that would work for a button, never tried it. It gives a tremendous amount of control of the text appearance for labels and textViews.

    I think when typing into the textboxes in IB you need to type tabs and newlines as escape codes: \t, \n, \r. But tabs don't have any meaning with setting the tab stops, which I think you can only do using NSAttributedString and a paragraph style.

    You can view invisible characters in TextWrangler or BBEdit, which are free Mac text editors.
     
  4. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #4
    i also think attributed strings are the way to go
    in the attributes inspector the op could select the attributed string option and try it out
     
  5. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #5
    Pretty sure that UIButtons don't allow for attributed strings.

    You could create a custom UIButton subclass that automatically creates an invisible button in front of a normal UILabel. Then you can just set text attributes on the label. You can animate button presses too to make it look normal.
     
  6. KALLT macrumors 601

    Joined:
    Sep 23, 2008
    #6
    You can set an attributed string as the title with the instance method setAttributedTitle(NSAttributedString?, for: UIControlState). I also don’t think UIButton supports multiple lines of text, however.
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    The simplest solution may be a clear button on top of a label.

    I wrote a UIButton subclass that adds a label as a subview so I can have two lines of text on the button. That would be another way to go.
     

Share This Page