如何在 UIButton 下显示标题?
How to show the title under the UIButton?
我在UIButton
.
下使用了标题插入来显示文本名称
这是我的代码和输出:
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
这是我的预期输出:
我使用了 UIButton
并设置了标题和 UIImage
作为背景。那么如何在UIButton
下显示title标签呢?
请先在 xib 中 Select 按钮。然后 select 属性检查器,在此 Select 标题中 "Edge" 并根据要求设置适当的 "Inset"。
请查看以下代码并根据您的要求设置插图。
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];
您的尝试是正确的;你只需要像下面那样设置插入边缘。然后你需要增加你的按钮框架,这样它就可以同时容纳标题和图像。
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.f, button.titleLabel.frame.size.height, 0.f)];
这也可以通过故事板完成,正如@VD Patel 建议的那样,这取决于您喜欢哪种方法。
您也可以使用单独的 UIImageview
和一个更易于处理且效率更高的按钮来完成此操作。
创建 UIImageView
、UILabel
和 UIButton
。然后将 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 方法中添加此代码。
我在UIButton
.
这是我的代码和输出:
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
这是我的预期输出:
我使用了 UIButton
并设置了标题和 UIImage
作为背景。那么如何在UIButton
下显示title标签呢?
请先在 xib 中 Select 按钮。然后 select 属性检查器,在此 Select 标题中 "Edge" 并根据要求设置适当的 "Inset"。
请查看以下代码并根据您的要求设置插图。
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];
您的尝试是正确的;你只需要像下面那样设置插入边缘。然后你需要增加你的按钮框架,这样它就可以同时容纳标题和图像。
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.f, button.titleLabel.frame.size.height, 0.f)];
这也可以通过故事板完成,正如@VD Patel 建议的那样,这取决于您喜欢哪种方法。
您也可以使用单独的 UIImageview
和一个更易于处理且效率更高的按钮来完成此操作。
创建 UIImageView
、UILabel
和 UIButton
。然后将 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 方法中添加此代码。