我需要一个UIButton图像&文本。图片应该位于顶部&下的图片应该可点击。UIButton图像+文本IOS
回答
我看到非常复杂的答案,他们都使用代码。然而,如果你正在使用Interface Builder,有一个很简单的方法来做到这一点:
- 选择按钮,设置标题和图像。请注意,如果您设置的是背景而不是图像,则图像的大小将小于按钮的大小。
- 通过更改边和插图设置两个项目的位置。你甚至可以在控制部分控制两者的对齐。
你甚至可以使用通过代码相同的方法,无需内部创建UILabels和UIImages作为提出其他解决方案。始终保持简单!
EDIT:附带具有正确插图
这是正确的。您需要在标题和图像上使用边缘插页来正确对齐。您可以通过设置titleEdgeInsets和contentEdgeInsets属性在代码中执行此操作。 – 2012-08-08 22:41:08
是的,它几乎是正确的。只需要设置“背景”图像而不是“图像”,否则就不会看到标题甚至没有设置插入值来重新定位标题。 – holex 2012-08-09 09:42:37
不正确?其实,你可以一起看到标题,图像和背景。我为我的按钮使用了这种技术,它可以正常工作。 – 2012-08-09 10:00:38
使用此代码:
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.imageView.frame=CGRectMake(0.0f, 0.0f, 50.0f, 44.0f);///You can replace it with your own dimensions.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 35.0f, 50.0f, 44.0f)];///You can replace it with your own dimensions.
[button addSubview:label];
让UIImageView
和UILabel
,并设置图像和文字都这样....然后,将自定义按钮上的ImageView和标签....
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search.png"]];
imageView.frame = CGRectMake(x, y, imageView.frame.size.width, imageView.frame.size.height);
[self.view addSubview:imageView];
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y,a,b)];
yourLabel.text = @"raj";
[self.view addSubview:yourLabel];
UIButton * yourBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setFrame:CGRectMake(x, y,c,d)];
[yourBtn addTarget:self action:@selector(@"Your Action") forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourBtn];
简单而有用。如何设置突出显示的图像和标题? – 2017-04-15 10:22:32
为标签setHighlightedTextColor和你必须玩forControlEvents – Rajneesh071 2017-05-25 10:13:31
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.image = [UIImage imageNamed:@"your image name here"];
button.titleLabel.text = @"your text here";
但下面的代码将显示标签上方和图像中的背景
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.background.image = [UIImage imageNamed:@"your image name here"];
button.titleLabel.text = @"your text here";
因为UIButton具有UILabel和UIimageview属性,所以不需要在同一控件中使用标签和按钮。
我觉得您正在寻找这种解决方案您的问题:
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setFrame:CGRectMake(0.f, 0.f, 128.f, 128.f)]; // SET the values for your wishes
[_button setCenter:CGPointMake(128.f, 128.f)]; // SET the values for your wishes
[_button setClipsToBounds:false];
[_button setBackgroundImage:[UIImage imageNamed:@"jquery-mobile-icon.png"] forState:UIControlStateNormal]; // SET the image name for your wishes
[_button setTitle:@"Button" forState:UIControlStateNormal];
[_button.titleLabel setFont:[UIFont systemFontOfSize:24.f]];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // SET the colour for your wishes
[_button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; // SET the colour for your wishes
[_button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, -110.f, 0.f)]; // SET the values for your wishes
[_button addTarget:self action:@selector(buttonTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside]; // you can ADD the action to the button as well like
...按钮的定制的其余部分是你的责任,现在,不要忘记添加按钮以你的看法。
更新#1和更新#2
,或者如果你并不需要一个动态按钮你可以在Interface Builder中添加按钮,你的看法,你可以设置在那里也有相同的值。它是相同的,但这是这个版本,以及在一个简单的图片。
你也可以在界面生成器中看到最终结果,因为它在屏幕截图上。
EdgeInsets只有当你有“设置按钮图像”,当你设置按钮图像,你不能看到标题。如果你设置了背景图像,那么EdgeInsets不能应用于图像。你可以看到标题。所以,你不能同时设置edgeinsect。 – 2015-12-03 12:23:38
@BadalShah,是的,当你说的是真实的,并且你不能同时看到_title_和_image_时,有一些场景(取决于按钮的大小,图像大小,标题的长度等等),因为_image_推出可见区域的_title_;但是这种情况根本不是通用的,一般的情况是_title_和_image_可以同时出现。 – holex 2015-12-03 13:13:41
你应该创建为文本图像和自定义标签定制的ImageView并将您添加到您的按钮子视图。而已。
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.backgroundColor = [UIColor greenColor];
yourButton.frame = CGRectMake(140, 40, 175, 30);
[yourButton addTarget:self action:@selector(yourButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, yourButton.frame.size.width, yourButton.frame.size.height/2)];
imageView1.image =[UIImage imageNamed:@"images.jpg"];
[yourButton addSubview:imageView1];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, yourButton.frame.size.height/2, yourButton.frame.size.width, yourButton.frame.size.height/2)];
label.backgroundColor = [UIColor greenColor];
label.textAlignment= UITextAlignmentCenter;
label.text = @"ButtonTitle";
[yourButton addSubview:label];
出于测试目的,使用yourButtonSelected:
方法
-(void)yourButtonSelected:(id)sender{
NSLog(@"Your Button Selected");
}
我认为这将是对你有所帮助。
使用此代码:
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(0, 10, 200, 52)];
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton]
它非常简单,只需添加图片到你的按键背景,给文本按钮titlelabel为uicontrolstatenormal。 就是这样。
[btn setBackgroundImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];
[btn setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[btn setTitle:@"Click Me" forState:UIControlStateNormal];
我遇到同样的问题,我通过创建的UIButton
和压倒一切的一个新的子类修复它的三样东西设置(标题,图像和背景)的一个小例子该layoutSubviews:
方法如下:
-(void)layoutSubviews {
[super layoutSubviews];
// Center image
CGPoint center = self.imageView.center;
center.x = self.frame.size.width/2;
center.y = self.imageView.frame.size.height/2;
self.imageView.center = center;
//Center text
CGRect newFrame = [self titleLabel].frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.imageView.frame.size.height + 5;
newFrame.size.width = self.frame.size.width;
self.titleLabel.frame = newFrame;
self.titleLabel.textAlignment = UITextAlignmentCenter;
}
我认为天使加西亚Olloqui的答案是另一种很好的解决方案,如果你把所有的人都与界面生成器手动,但我会继续我的解决方案,因为我没有修改内容嵌入s为我的每个按钮。
但它工作;)检查我的意见和编辑 – 2012-08-10 13:29:24
检查这真棒LIB ImageCenterButton 它解决了所有的问题,其他库的脸和我确认它的工作原理与iOS 9
- 1. UIButton文本和图像
- 2. 自定义UIButton图像iOS
- 3. ios - 左对齐图像和在UIButton中心放置文本
- 4. 带图像和文本的UIButton
- 5. iOS - UIButton系统选定的图像
- 6. iOS - UIButton图像不符合预期
- 7. 如何更改UIButton中的UIButton背景图像点击ios
- 8. iOS中的自定义UIButton与文本顶部的居中图像
- 9. UITextView:使用UITextView文本内联UIButton iOS
- 10. 带图像的uibutton
- 11. UIButton图像动画
- 12. 如何在UIButton中放置图像和底部文本?
- 13. UIButton左侧文本和右侧快捷图像
- 14. 自定义的UIButton具有图像和文本
- 15. 以编程方式在UIButton上设置图像和文本
- 16. 如何使图像和文本自定义UIButton?
- 17. 图像和背景图像的UIButton
- 18. UIButton图像/背景图像古怪
- 19. 标题在UIButton与图像
- 20. UIButton修改图像的UIImageView
- 21. UIButton不显示图像
- 22. iPhone的UIButton - 图像位置
- 23. 动画UIButton背景图像
- 24. 如何设置UIButton图像?
- 25. 单击更改UIbutton图像
- 26. 使UIButton大于其图像
- 27. NavigationItem中的UIButton图像
- 28. UIButton不会改变图像
- 29. UIButton:多行文本
- 30. UIButton文本缩写?
希望这有助于 参见[UIButton的用文字和图像] [1 ] http://stackoverflow.com/questions/4926581/uibutton-with-picture-and-text [1]:http:// stackoverflow。com/questions/4926581/uibutton-with-picture-and-text 然后,您可以在视图中添加UITapGestureRecognizer以处理自定义按钮点击。 – 2012-08-10 03:33:24
听起来更像一个需求规格而不是问题。 – Abizern 2012-08-10 10:35:57
查看接受的答案和http://commandshift.co.uk/blog/2013/03/12/uibutton-edge-insets/关于UIButton Edges – onmyway133 2014-12-19 03:17:12