Note that there are some explanatory texts on larger screens.

plurals
  1. POUIButton with two lines of text in the title (numberOfLines=2)
    primarykey
    data
    text
    <p>I'm trying to make a <code>UIButton</code> that has two lines of text in its titleLabel. This is the code I'm using:</p> <pre><code> UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)]; titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0]; [titleButton setTitle:@"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal]; titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; titleButton.titleLabel.numberOfLines = 2; [self addSubview:titleButton]; </code></pre> <p>When I try this, the text only appears on one line. It seems the only way to achieve more than one line of text in <code>UIButton.titleLabel</code> is to set <code>numberOfLines=0</code> and use <code>UILineBreakModeWordWrap</code>. But this doesn't guarantee the text to be exactly two lines.</p> <p>Using a plain <code>UILabel</code>, however, does work:</p> <pre><code> UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)]; titleLabel.font = [UIFont boldSystemFontOfSize:24.0]; titleLabel.text = @"This text is very long and should get truncated at the end of the second line"; titleLabel.numberOfLines = 2; titleLabel.lineBreakMode = UILineBreakModeTailTruncation; [self addSubview:titleLabel]; </code></pre> <p>Does anyone know how to make the <code>UIButton</code> work with two lines? Is the only solution to create a separate <code>UILabel</code> to hold the text, and add it as a subview of the button?</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.
 

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