更改 Xcode 中按钮标签的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5178354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:22:22  来源:igfitidea点击:

Changing position of button label in Xcode

iphonexcodebuttonios-4.2

提问by edhog

I'm just wondering if anyone could help me change the position of a button label for a button that I've made with code.

我只是想知道是否有人可以帮助我更改我用代码制作的按钮的按钮标签的位置。

Thanks a lot,

非常感谢,

Chris

克里斯

回答by zrzka

If you mean UIButton, look at UIButton's titleEdgeInsets property.

如果您指的是 UIButton,请查看 UIButton 的 titleEdgeInsets 属性。

回答by Dee

So the button labelis an instance of UILabel, isn't it ?? Set the position of that label by setting the frame to it with respect to the UIButton frame.

所以按钮标签是 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];

I hard coded the x and y positions of the button as well as for the label. You set some other value's and place the label in the respective position where ever you want.

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

If you want to set only the text position of that label you can do it like this:

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

    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];

Hope it will help you.

希望它会帮助你。

回答by iphone_Kanta

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];