如何在UIButton下显示标题?

问题描述 投票:1回答:3

我已使用标题插图在UIButton下显示文本名称。

这是我的代码和输出:

 [button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9nV05yOS5wbmcifQ==” alt =“在此处输入图像描述”>

这是我的预期输出:

“在此处输入图像描述”

我已经使用UIButton并将标题和UIImage设置为背景。那么如何在UIButton下显示标题标签?

ios objective-c uibutton uiimage
3个回答
1
投票

请先选择xib中的按钮。然后选择“属性检查器”,在“边缘”的“选择标题”中,并根据需求设置适当的“插图”。

请看下面的代码,并根据您的要求设置插图。

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
[myButton setImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
[myButton setTitle:@"Rate us on app store" forState:UIControlStateNormal];
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 50, 2, 20)];//set ur title insects myButton
[myButton setImageEdgeInsets:UIEdgeInsetsMake(2, -200, 2, 2)];//make negative edge for left side
[myButton setBackgroundColor:[UIColor greenColor]];
[self.view myButton];

3
投票

您试图做的是正确的;您只需要按照以下步骤设置插图边缘即可。然后,您需要增加按钮框,以便它可以同时容纳标题和图像。

[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];

[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.f, button.titleLabel.frame.size.height, 0.f)];

这也可以通过情节提要来完成,因为@VD Patel建议您根据自己的喜好选择。

您还可以使用单独的UIImageview和一个更易于操作且效率更高的按钮来执行此操作。


0
投票

创建UIImageViewUILabelUIButton。然后将ImageView和Label作为子视图添加到Button。

    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 200, 100)];
    lbl.textAlignment = NSTextAlignmentCenter;
    [lbl setText:@"Mono"];

    UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    imgView.image = [UIImage imageNamed:@"your_image.png"];
    [imgView setUserInteractionEnabled:NO];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addSubview:imgView];
    [btn addSubview:lbl];
    [btn setFrame:CGRectMake(0, 20, 200, 300)];
    [btn addTarget:self action:@selector(OnButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

尝试在viewDidLoad方法中添加此代码。

© www.soinside.com 2019 - 2024. All rights reserved.