【问题标题】:UIButton Image + Text IOSUIButton图片+文字IOS
【发布时间】:2012-07-27 20:50:25
【问题描述】:

我需要一个带有图片和文字UIButton。图片应该在顶部,文字在图片下方,两者都应该是可点击的。

【问题讨论】:

标签: ios iphone ipad uibutton


【解决方案1】:

我看到非常复杂的答案,所有答案都使用代码。但是,如果您使用的是 Interface Builder,有一个非常简单的方法可以做到这一点:

  1. 选择按钮并设置标题和图像。请注意,如果您设置背景而不是图像,则如果图像小于按钮,则图像将被调整大小。
  2. 通过更改边缘和插图来设置这两个项目的位置。您甚至可以在“控制”部分控制两者的对齐方式。

您甚至可以通过代码使用相同的方法,而无需像其他建议的解决方案那样在内部创建 UILabel 和 UIImage。始终保持简单!

编辑:附上一个小示例,其中设置了 3 个内容(标题、图像和背景)并带有正确的插图

【讨论】:

  • 这是正确的。您需要使用标题和图像上的边缘插图来正确对齐两者。您可以通过设置 titleEdgeInsets 和 contentEdgeInsets 属性在代码中执行此操作。
  • 是的,它几乎是正确的。只是您必须设置Background 图像而不是Image,否则您将看不到标题,甚至没有设置插入值来重新定位标题。
  • 我也遇到了这个问题。如果您的标题未显示,请尝试为其设置负左偏移。看起来 IB 会自动将其推到图像的右侧。
  • 这只会并排显示图像和文本,对吗?有没有办法从上到下改变方向?这就是原始问题所陈述的内容,也是我正在寻找的内容。谢谢!
  • 对于使用 Xcode 8 的用户,您可能看不到 Edge 属性。添加图像后,文本也可能会消失。它将文本更改为白色,如果您在白色背景上,它看起来就像消失了。更改文本颜色,它会出现在图像附近。您可以在尺寸检查器下调整插图。
【解决方案2】:

我认为您正在为您的问题寻找此解决方案

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 中的视图中,并且可以在此处设置与好吧。几乎一样,但在一张简单的图片中也是这个版本。

还可以在Interface Builder中看到最终结果,就像屏幕截图一样。

【讨论】:

  • EdgeInsets 仅在您有“设置按钮图像”并且当您设置按钮图像时看不到标题时才有效。如果您设置背景图像,则 EdgeInsets 不能应用于图像。你可以看到标题。因此,您不能同时将 edgeinsect 设置为两者。
  • @BadalShah,是的,在某些情况下(取决于按钮的大小、图像大小、标题的长度等......)当你说的是真的并且你看不到 titleimage 同时出现,因为 image 推出了可见区域的 title;但是这种情况根本不是通用的,通用的情况是 titleimage 可以并且将同时出现。
【解决方案3】:

Xcode-9Xcode-10 Apple 现在对 Edge Inset 进行了一些更改,您可以在 size-inspector 下进行更改。

请按以下步骤操作:

第 1 步: 输入文字并选择要显示的图片:

第 2 步: 根据您的要求选择按钮控件,如下图所示:

第三步: 现在转到尺寸检查器并根据您的要求增加价值:

【讨论】:

    【解决方案4】:

    快速版本:

    var button = UIButton()
    
    newGameButton.setTitle("Новая игра", for: .normal)
    newGameButton.setImage(UIImage(named: "energi"), for: .normal)
    newGameButton.backgroundColor = .blue
    newGameButton.imageEdgeInsets.left = -50
    

    【讨论】:

      【解决方案5】:

      就我而言,我想在右侧添加UIImage,在左侧添加UILabel。也许我可以通过编写代码来实现这一点(就像上面提到的那样),但我不喜欢编写代码并尽可能使用storyboard 来完成它。原来是这样的:

      首先,在您的标签框中写下一些内容,然后选择您要显示的图像:

      这将创建一个如下所示的按钮:

      接下来,寻找Semantic并选择Force Right-to-Left(如果你不指定任何东西,那么它会在左边显示图像并像上图一样在右侧标注):

      最后,你会在右边看到UIImage,在左边看到UILabel

      要在标签和图像之间添加空格,请转到尺寸检查器并根据您的要求更改这些值:

      就是这样!

      【讨论】:

        【解决方案6】:
        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 属性,因此无需在同一控件中使用标签和按钮。

        【讨论】:

        • 我需要将图片放在顶部,图片下方的文字都应该是可点击的..
        【解决方案7】:

        使用此代码:

        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]
        

        【讨论】:

          【解决方案8】:

          使用此代码:

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

          【讨论】:

            【解决方案9】:

            我遇到了同样的问题,我通过创建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;
            
            }
            

            我认为 Angel García Olloqui 的回答是另一个很好的解决方案,如果您使用界面生成器手动放置所有这些,但我会保留我的解决方案,因为我不必修改每个按钮的内容插图。

            【讨论】:

              【解决方案10】:

              制作UIImageViewUILabel,并将图像和文本设置为这两者....然后在imageView和Label上放置一个自定义按钮....

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

              【讨论】:

              • 简单实用。如何设置高亮图片和标题?
              • 对于标签 setHighlightedTextColor,您必须使用 forControlEvents
              【解决方案11】:

              您应该为图像创建自定义图像视图并为文本创建自定义标签,然后将其作为子视图添加到按钮中。就是这样。

              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");
              
              }
              

              我认为这对你会有帮助。

              【讨论】:

                【解决方案12】:

                这真的很简单,只需将图像添加到按钮的背景,并为 uicontrolstatenormal 的按钮的标题标签添加文本。 就是这样。

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

                【讨论】:

                • ...图片下方的文字是怎样的?
                • @holex : 非常感谢您指导我,我发现了其中的缺陷,然后进行了相应的更改。
                猜你喜欢
                • 2011-06-23
                • 1970-01-01
                • 2018-01-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-10-15
                • 2011-11-29
                相关资源
                最近更新 更多