Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
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?
 
Last edited:
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.
 
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.
 
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.
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
 
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.
 
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.
 
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.
 
  • Like
Reactions: grandM
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.