Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As to Brent's idea of putting the title UILabel as sibling view, it doesn't seem to me like a very good idea. I keep thinking in interaction problems with the UILabel due to its touch events not getting through the UIButton's view. </p> <p>On the other hand, with a UILabel as subview of the UIButton, I'm pretty confortable knowing that the touch events will always be propagated to the UILabel's superview.</p> <p>I did take this approach and didn't notice any of the problems reported with backgroundImage. I added this code in the -titleRectForContentRect: of a UIButton subclass but the code can also be placed in drawing routine of the UIButton superview, which in that case you shall replace all references to self with the UIButton's variable.</p> <pre><code>#define TITLE_LABEL_TAG 1234 - (CGRect)titleRectForContentRect:(CGRect)rect { // define the desired title inset margins based on the whole rect and its padding UIEdgeInsets padding = [self titleEdgeInsets]; CGRect titleRect = CGRectMake(rect.origin.x + padding.left, rect.origin.x + padding.top, rect.size.width - (padding.right + padding.left), rect.size.height - (padding.bottom + padding].top)); // save the current title view appearance NSString *title = [self currentTitle]; UIColor *titleColor = [self currentTitleColor]; UIColor *titleShadowColor = [self currentTitleShadowColor]; // we only want to add our custom label once; only 1st pass shall return nil UILabel *titleLabel = (UILabel*)[self viewWithTag:TITLE_LABEL_TAG]; if (!titleLabel) { // no custom label found (1st pass), we will be creating &amp; adding it as subview titleLabel = [[UILabel alloc] initWithFrame:titleRect]; [titleLabel setTag:TITLE_LABEL_TAG]; // make it multi-line [titleLabel setNumberOfLines:0]; [titleLabel setLineBreakMode:UILineBreakModeWordWrap]; // title appearance setup; be at will to modify [titleLabel setBackgroundColor:[UIColor clearColor]]; [titleLabel setFont:[self font]]; [titleLabel setShadowOffset:CGSizeMake(0, 1)]; [titleLabel setTextAlignment:UITextAlignmentCenter]; [self addSubview:titleLabel]; [titleLabel release]; } // finally, put our label in original title view's state [titleLabel setText:title]; [titleLabel setTextColor:titleColor]; [titleLabel setShadowColor:titleShadowColor]; // and return empty rect so that the original title view is hidden return CGRectZero; } </code></pre> <p>I did take the time and wrote a bit more about this <a href="http://unsolicitedfeedback.com/2009/03/09/how-to-set-a-multi-line-title-in-uibutton" rel="nofollow noreferrer">here</a>. There, I also point a shorter solution, though it doesn't quite fit all the scenarios and involves some private views hacking. Also there, you can download an UIButton subclass ready to be used.</p>
 

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