2011-03-03 98 views

回答

7

如果你指的UIButton,看的UIButton的titleEdgeInsets财产。

2

所以按钮标签是UILabel的一个实例,不是吗?通过设置相对于UIButton框架的框架来设置该标签的位置。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setFrame:CGRectMake(3, 20, w, h)]; 
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, w, h)]; 
[titleLabel setText:@"TITLE"]; 
[button addSubview:titleLabel]; 
[self.view addSubview:button]; 
[titleLabel release]; 

我硬编码了按钮的x和y位置以及标签。您可以设置其他值并将标签放置在您想要的相应位置。

如果你想设置只有标签的文本位置,你可以做这样的:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button setFrame:CGRectMake(3, 20, w, h)]; 
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, h)]; 
    [titleLabel setText:@"TITLE"]; 
    [titleLabel setTextAlignment:UITextAlignmentCenter];//(UITextAlignmentRight/UITextAlignmentLeft) 
    [button addSubview:titleLabel]; 
    [self.view addSubview:button]; 
    [titleLabel release]; 

希望它会帮助你。

0
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(0,0,50,32); 
    [button setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal]; 

    [button addTarget:self action:@selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside]; 
    UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)]; 
    backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4); 
    [backButtonView addSubview:button]; 

    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView]; 
    //barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0); 

    [self.navigationItem setLeftBarButtonItem:barButtonItem];