2012-07-30 80 views
93

我需要一个UIButton图像&文本。图片应该位于顶部&下的图片应该可点击。UIButton图像+文本IOS

+0

希望这有助于 参见[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

+15

听起来更像一个需求规格而不是问题。 – Abizern 2012-08-10 10:35:57

+0

查看接受的答案和http://commandshift.co.uk/blog/2013/03/12/uibutton-edge-insets/关于UIButton Edges – onmyway133 2014-12-19 03:17:12

回答

250

我看到非常复杂的答案,他们都使用代码。然而,如果你正在使用Interface Builder,有一个很简单的方法来做到这一点:

  1. 选择按钮,设置标题和图像。请注意,如果您设置的是背景而不是图像,则图像的大小将小于按钮的大小。 IB basic image and title
  2. 通过更改边和插图设置两个项目的位置。你甚至可以在控制部分控制两者的对齐。

IB position set IB Control set

你甚至可以使用通过代码相同的方法,无需内部创建UILabels和UIImages作为提出其他解决方案。始终保持简单!

EDIT:附带具有正确插图 Button preview

+2

这是正确的。您需要在标题和图像上使用边缘插页来正确对齐。您可以通过设置titleEdgeInsets和contentEdgeInsets属性在代码中执行此操作。 – 2012-08-08 22:41:08

+3

是的,它几乎是正确的。只需要设置“背景”图像而不是“图像”,否则就不会看到标题甚至没有设置插入值来重新定位标题。 – holex 2012-08-09 09:42:37

+0

不正确?其实,你可以一起看到标题,图像和背景。我为我的按钮使用了这种技术,它可以正常工作。 – 2012-08-09 10:00:38

3

使用此代码:

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

UIImageViewUILabel,并设置图像和文字都这样....然后,将自定义按钮上的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]; 
+0

简单而有用。如何设置突出显示的图像和标题? – 2017-04-15 10:22:32

+0

为标签setHighlightedTextColor和你必须玩forControlEvents – Rajneesh071 2017-05-25 10:13:31

7
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属性,所以不需要在同一控件中使用标签和按钮。

+1

我需要将图像放置在顶部和图像下方的文本都应该是可点击的。 – Codesen 2012-07-30 08:28:22

+0

你可以使用我的第一部分代码 – Vinodh 2012-07-30 08:29:36

59

我觉得您正在寻找这种解决方案您的问题:

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中添加按钮,你的看法,你可以设置在那里也有相同的值。它是相同的,但这是这个版本,以及在一个简单的图片。

也可以在界面生成器中看到最终结果,因为它在屏幕截图上。

enter image description here

+0

EdgeInsets只有当你有“设置按钮图像”,当你设置按钮图像,你不能看到标题。如果你设置了背景图像,那么EdgeInsets不能应用于图像。你可以看到标题。所以,你不能同时设置edgeinsect。 – 2015-12-03 12:23:38

+0

@BadalShah,是的,当你说的是真实的,并且你不能同时看到_title_和_image_时,有一些场景(取决于按钮的大小,图像大小,标题的长度等等),因为_image_推出可见区域的_title_;但是这种情况根本不是通用的,一般的情况是_title_和_image_可以同时出现。 – holex 2015-12-03 13:13:41

3

你应该创建为文本图像和自定义标签定制的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"); 

} 

我认为这将是对你有所帮助。

3

使用此代码:

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

它非常简单,只需添加图片到你的按键背景,给文本按钮titlelabel为uicontrolstatenormal。 就是这样。

[btn setBackgroundImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal]; 
[btn setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom]; 
[btn setTitle:@"Click Me" forState:UIControlStateNormal]; 
+4

...以及文字如何在图像下移动? – holex 2012-08-09 08:45:14

+0

@holex:非常感谢你指导我,我发现它的缺陷,然后相应地改变。 – Dhruv 2012-08-09 09:36:40

3

我遇到同样的问题,我通过创建的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为我的每个按钮。

+0

但它工作;)检查我的意见和编辑 – 2012-08-10 13:29:24

1

检查这真棒LIB ImageCenterButton 它解决了所有的问题,其他库的脸和我确认它的工作原理与iOS 9

enter image description here

4

的Xcode-8的Xcode-9苹果做了几现在更改Edge Inset,您可以在尺寸检查器下更改它。

请按照下列步骤操作:

第1步: 要显示输入文字,然后选择图片:

enter image description here

步骤2: 选择按钮控制根据您的要求如下图所示:

enter image description here

步骤3: 现在去到尺寸检查,并添加值按您的要求:

enter image description here