Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I think, the good solution must be useful for any text, without hardcoded values for insets (it's about the solution provided by toytoy). I use the code similar to this:</p> <pre><code>NSString *sometitle = @"blabla button title"; NSString *someimage = @"blablaimage"; UIImage *image = [[UIImage imageNamed:someimage]; int buttonWidth = A; int buttonHeight = B; int imageWidth =image.size.width; int imageHeight = image.size.height; int titleWidth = buttonWidth - imageWidth; // create button and set image and title buttonView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)]; [buttonView setImage:image forState:UIControlStateNormal]; [buttonView setTitle:sometitle forState:UIControlStateNormal]; // make button all content to be left aligned [buttonView setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; // set title font [buttonView.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]]; // calculate font text width with selected font CGSize stringBoundingBox = [number sizeWithFont:[buttonView.titleLabel font]]; // make title inset with some value from left // it place the title right on the center of space for the title int titleLeft = (titleWidth - stringBoundingBox.width) / 2; [buttonView setTitleEdgeInsets:UIEdgeInsetsMake(0, titleLeft, 0, 0)]; </code></pre> <p>After the string with stringBoundingBox init I also add some strings like this:</p> <pre><code>if (stringBoundingBox.width &gt; titleWidth) { [_backgroundView.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:13]]; stringBoundingBox = [number sizeWithFont:[_backgroundView.titleLabel font]]; } if (stringBoundingBox.width &gt; titleWidth) { [_backgroundView.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:12]]; stringBoundingBox = [number sizeWithFont:[_backgroundView.titleLabel font]]; } </code></pre> <p>it allow to me set the titles longer than available space, by selecting some smaller fonts. I make this because of another way for this:</p> <pre><code>[buttonView.titleLabel setAdjustsFontSizeToFitWidth:YES]; </code></pre> <p>works not very good, looks like it take the font size for 3-4 points smaller at one step, so some not so long lines become too smaller, than it must to be.</p> <p>This code allow us to place any text in button title space exactly in center.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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