iphoneuibuttonuikitmarginpadding

UIButton Text Margin / Padding


I have the following layout, and I'm trying to add a padding to the left and right..

The controls are a disabled UIButton.

enter image description here

My code for creating a button is this:

UIButton *buttonTime = [[UIButton alloc] initWithFrame:CGRectMake(90, 10, 50, 20)]; 
[buttonTime setBackgroundImage:[[UIImage imageNamed:@"bubble.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:13] forState:UIControlStateDisabled];

[buttonTime setTitle:@"27 feb, 2011 11:10 PM" forState:UIControlStateDisabled];             
[buttonTime setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
buttonTime.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:8.0]; 
buttonTime.titleLabel.lineBreakMode= UILineBreakModeWordWrap;
[buttonTime setEnabled:FALSE];
[scrollView addSubview:buttonTime];
[buttonTime release];

Solution

  • // Swift
    var titleEdgeInsets: UIEdgeInsets!
    
    // Objective-C
    @property(nonatomic) UIEdgeInsets titleEdgeInsets;
    

    Use this property to resize and reposition the effective drawing rectangle for the button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.

    https://developer.apple.com/documentation/uikit/uibutton/1624010-titleedgeinsets

    NOTE: (@2023/01)

    titleEdgeInsets is deprecated in iOS 15.0.