Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Solution1</h2> <p>You can set the key path in the storyboard</p> <p>Set the text to your multiline title e.g. <code>hello</code> <kbd> ⌥ </kbd> + <kbd>↵</kbd> <code>multiline</code></p> <p>You need to press <kbd> ⌥ </kbd> + <kbd>↵</kbd> to move text to next line. </p> <p><a href="https://i.stack.imgur.com/8z0jf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/8z0jf.png" alt="enter image description here"></a></p> <p>Then add the key path</p> <p><code>titleLabel.textAlignment</code> as <code>Number</code> and value <code>1</code>, <code>1</code> means <code>NSTextAlignmentCenter</code><br> <code>titleLabel.numberOfLines</code> as <code>Number</code> and value <code>0</code>, <code>0</code> means any number of lines</p> <p><a href="https://i.stack.imgur.com/6ETfV.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6ETfV.png" alt="enter image description here"></a></p> <p><strong>This will not be reflected on IB/Xcode, but will be in centre at run time (device/simulator)</strong></p> <p>If you want to see the changes on Xcode you need to do the following: (remember you can skip these steps)</p> <ol> <li><p>Subclass the UIButton to make the button designable: <pre><code>import UIKit @IBDesignable class UIDesignableButton: UIButton {} </pre></code></p></li> <li><p>Assign this designable subclass to the buttons you're modifying:</p></li> </ol> <p><a href="https://i.stack.imgur.com/9gx2w.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9gx2w.png" alt="Showing how to change the class of the button using Interface Builder"></a></p> <ol start="3"> <li>Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):</li> </ol> <p><a href="https://i.stack.imgur.com/oB9HM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oB9HM.png" alt="Comparing the designable and default button in Interface Builder"></a></p> <hr> <p><br></p> <h2>Solution2</h2> <p><strong>If you want to write the code, then do the long process</strong></p> <p>1.Create <code>IBOutlet</code> for button<br> 2.Write code in viewDidLoad</p> <pre><code>btn.titleLabel.textAlignment = .Center btn.titleLabel.numberOfLines = 0 </code></pre> <hr> <p><br></p> <h2>Solution3</h2> <p>In newer version of xcode (mine is xcode 6.1) we have property attributed title<br> Select <code>Attributed</code> then select the text and press centre option below</p> <p><strong>P.S.</strong> The text was not coming multiline for that I have to set the </p> <p><code>btn.titleLabel.numberOfLines = 0</code> <br> <br></p> <p><img src="https://i.stack.imgur.com/JJyWM.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload