views:

487

answers:

3

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 = ???
+2  A: 

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
drawnonward
can you show me some example please?
RAGOpoR
A: 

Can you do button.titlelabel.center = cgpoint(x,y)

nickthedude
thanks nickthedude btn.titleLabel.center = CGPointMake(x,y);it work for me, is it private API ? or apple will reject this code?
RAGOpoR
`button.titleLabel.center = CGPointMake(button.titleLabel.center.x, 60.0f);` didn't work for me.
MattDiPasquale
+4  A: 
// 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.0, 10.0, 0.0, 0.0)]
cannyboy