Is it possible to adjust x,y position for titleLabel of UIButton?

IosIphoneUibuttonTextlabel

Ios Problem Overview


Is it possible to adjust the x,y position for the titleLabel of a UIButton?

Here is my code:

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
    [btn setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
    [btn setTitle:[NSString stringWithFormat:@"Button %d", i+1] forState:UIControlStateNormal];		
    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.frame = ???

Ios Solutions


Solution 1 - Ios

//make the buttons content appear in the top-left
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];

//move text 10 pixels down and right
[button setTitleEdgeInsets:UIEdgeInsetsMake(10.0f, 10.0f, 0.0f, 0.0f)];

And in Swift

//make the buttons content appear in the top-left
button.contentHorizontalAlignment = .Left
button.contentVerticalAlignment = .Top

//move text 10 pixels down and right
button.titleEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 0.0, 0.0)

Swift 5

button.contentHorizontalAlignment = .left
button.contentVerticalAlignment = .top
button.titleEdgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 0.0, right: 0.0)

Solution 2 - Ios

The easiest way to do it visually is to use the attribute inspector** (appears when editing a xib/storyboard), setting the "edge" property to title, adjusting it's insets, then setting "edge" property to image, and adjusting accordingly. It's usually better than coding it , since it's easier to maintain and highly visual.

Attribute inspector setting

Solution 3 - Ios

Derive from UIButton and implement the following method:

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

Edit:

@interface PositionTitleButton : UIButton
@property (nonatomic) CGPoint titleOrigin;
@end

@implementation PositionTextButton
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
  contentRect.origin = titleOrigin;
  return contentRect;
}
@end

Solution 4 - Ios

For my projects I've this extension utility:

extension UIButton {
    
    func moveTitle(horizontal hOffset: CGFloat, vertical vOffset: CGFloat) {
        self.titleEdgeInsets.left += hOffset
        self.titleEdgeInsets.top += vOffset
    }
    
}

Solution 5 - Ios

This can be done in xib. Select your button, go to "Show the Size Inspector" tab and setup insets.

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRAGOpoRView Question on Stackoverflow
Solution 1 - IoscannyboyView Answer on Stackoverflow
Solution 2 - IoseladlebView Answer on Stackoverflow
Solution 3 - IosdrawnonwardView Answer on Stackoverflow
Solution 4 - IosLuca DavanzoView Answer on Stackoverflow
Solution 5 - IoslandonandreyView Answer on Stackoverflow