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


您甚至可以通过代码使用相同的方法,而不像其他解决方案那样在内部创建UILabel和UIImage.始终保持简单!
编辑:附上一个小例子,其中3个东西设置(标题,图像和背景)与正确的插图

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-9和Xcode-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)
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属性,因此无需在同一控件中使用标签和按钮.
就我而言,我想添加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)
| 归档时间: |
|
| 查看次数: |
124876 次 |
| 最近记录: |