UIButton图像+文本IOS

Cod*_*sen 120 iphone uibutton ipad ios

我需要UIButton图像和文字.图像应位于顶部,文本位于图像下方,均应可点击.

Ang*_*qui 299

我看到非常复杂的答案,他们都使用代码.但是,如果您使用的是Interface Builder,则有一种非常简单的方法:

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

IB位置设置 IB控制集

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

编辑:附上一个小例子,其中3个东西设置(标题,图像和背景)与正确的插图 按钮预览

  • 对于使用Xcode 8的用户,您可能看不到Edge属性.添加图像后,文本可能会消失.它将文本更改为白色,如果您在白色背景上,它看起来就像消失了.更改文本颜色,它将显示在图像附近.您可以在"大小"检查器下调整插图. (11认同)
  • 这只会并排显示图像和文字,对吧?有没有办法从上到下改变方向?这就是原始问题所说的以及我正在寻找的内容.谢谢! (7认同)
  • 我也遇到了麻烦.如果您的标题没有显示,请尝试为其设置负左偏移.看起来IB会自动将其推到图像的右侧. (5认同)
  • 是的,这几乎是正确的.只需要设置`Background`图像而不是`Image`,否则你不会看到标题甚至没有设置插入值来重新定位标题. (4认同)
  • 这是对的.您将需要使用标题和图像上的边缘插入来正确对齐两者.您可以通过设置titleEdgeInsets和contentEdgeInsets属性在代码中执行此操作. (2认同)
  • 我遇到了“不可见标签”的问题,并通过减小图像大小(在 Preview.app 中)解决了这个问题。我使用 500*500 的图标作为 100*30 的按钮...... (2认同)

hol*_*lex 65

我想你正在为你的问题寻找这个解决方案:

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
Run Code Online (Sandbox Code Playgroud)

...按钮的其他定制现在是您的职责,不要忘记将按钮添加到您的视图中.

更新#1更新#2

或者,如果您不需要动态按钮,可以在Interface Builder中将按钮添加到视图中,也可以在此处设置相同的值.它是相同的,但这个版本也是一个简单的图片.

还可以界面生成器中查看最终结果,因为它位于屏幕截图中.

在此输入图像描述


Alo*_*lok 25

Xcode-9Xcode-10 Apple现在对Edge Inset做了一些改动,你可以在尺寸检查器下进行更改.

请按照以下步骤操作:

步骤1: 输入文本并选择要显示的图像:

在此输入图像描述

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

在此输入图像描述

第3步: 现在进入规模检查员并根据您的要求增加价值:

在此输入图像描述


atu*_*n23 14

快速版本:

var button = UIButton()

newGameButton.setTitle("????? ????", for: .normal)
newGameButton.setImage(UIImage(named: "energi"), for: .normal)
newGameButton.backgroundColor = .blue
newGameButton.imageEdgeInsets.left = -50
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Vin*_*odh 8

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.image = [UIImage imageNamed:@"your image name here"];
button.titleLabel.text = @"your text here";
Run Code Online (Sandbox Code Playgroud)

但是下面的代码会显示上面的标签和背景中的图像

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.background.image = [UIImage imageNamed:@"your image name here"];
button.titleLabel.text = @"your text here";
Run Code Online (Sandbox Code Playgroud)

由于UIButton具有UILabel和UIimageview属性,因此无需在同一控件中使用标签和按钮.

  • 我需要将图像放置在顶部,图像下方的文本都应该是可点击的。 (2认同)

Har*_*tel 8

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

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

在此处输入图片说明

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

在此处输入图片说明

接下来,查找Semantic并选择Force Right-to-Left(如果您不指定任何内容,那么它将像上图一样向左显示图像并向右显示标签):

在此处输入图片说明

最后,您将UIImage在右侧和UILabel左侧看到:

在此处输入图片说明

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

在此处输入图片说明

就是这样!


小智 5

使用此代码:

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]
Run Code Online (Sandbox Code Playgroud)