简体   繁体   English

如何将文本和图像添加到UIButton

[英]How to add text and image to an UIButton

I want to add a UIButton . 我想添加一个UIButton However, i need the following items displayed on it. 但是,我需要在其上显示以下项目。

在此处输入图片说明

First of all i need to display some text, then a small image then again some text followed by an image as shown in the above diagram. 首先,我需要显示一些文本,然后显示一个小图像,然后再显示一些文本,然后显示一个图像,如上图所示。 How am i able to do this ? 我该怎么做?

Also, i need to know if this adheres to apples UI guidelines. 另外,我需要知道这是否符合apples UI准则。 If not please suggest how am i suppose to do this ? 如果不是,请建议我应该怎么做? (May be use a label instead) (可以改用标签)

Code

button= [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.frame = CGRectMake(210, 285, 100, 18)];
//[button setTitle:@"Show View" forState:UIControlStateNormal];
[button
 addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];

I would suggest you to create custom UIButton, and there add UILabels and UIImageViews to button. 我建议您创建自定义UIButton,并在其中添加UILabels和UIImageViews。

Please be aware that you are allowed to add as much elements to button as you want since UIButton is subclass of UIView (this is possible only through the code, not through Interface Builder). 请注意,由于UIButton是UIView的子类,因此您可以根据需要向按钮添加尽可能多的元素(这只能通过代码而不是通过Interface Builder来实现)。

Another option would be to create custom UIView and then add these elements as well as gestures to collect ones for pressing the button etc. 另一种选择是创建自定义UIView,然后添加这些元素以及手势以收集用于按下按钮等的手势。

Also, if this button is one-time-only then you can do this in your view controller. 另外,如果此按钮是一次性的,则可以在视图控制器中执行此操作。

To add any element to UIButton all you have to do is: 要将任何元素添加到UIButton,您要做的就是:

[myButton addSubview:particularSubview]

If you have three labels and two images, repeat the process five times. 如果有三个标签和两个图像,请重复此过程五次。

Keep in mind that you should init UI elements with frame, so they don't overlap: 请记住,您应该使用框架初始化UI元素,因此它们不会重叠:

UILabel *ll = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];

first parameter is x, second is y, third is width and forth is height. 第一个参数是x,第二个参数是y,第三个参数是宽度,第四个参数是高度。

Not sure if you can do it with a native UIButton but you can subclass UIView and create the elements in this view. 不知道是否可以使用本机UIButton但是可以UIView并在此视图中创建元素。 Then just override the - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event method to detect when the entire view (aka button) has been pressed 然后只需重写- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event方法即可检测何时按下了整个视图(即按钮)

as other answers suggest you can also subclass UIButton (giving you the tapping gesture oob) 其他答案表明,您也可以继承UIButton(给您敲击手势oob)

You can use the new feature introduced by Apple, IBDesignable and IBInspectacle properties (I wrote a basic tutorial here but you can find lots of sample on GitHub ). 您可以使用Apple引入的新功能,IBDesignable和IBInspectacle属性(我在这里写了一个基础教程,但是您可以在GitHub上找到很多示例)。

As a starting point, subclass UIButton with your own class and add the components you need manually. 首先,使用您自己的类将UIButton子类化,并手动添加所需的组件。

The simplest way to create buttons with image is 用图像创建按钮的最简单方法是

-(void)createButton:(NSString *)text atPos:(CGPoint)pos{
    UIImage *image=[UIImage imageNamed:@"image.png"];

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setTitle:text forState:UIControlStateNormal];

    //it is important to use image size same for the frame if you are setting image in background mode..
    [button setFrame:CGRectMake(pos.x, pos.y, image.size.width, image.size.height)];
    [self.view addSubview:button];
}

Then call it where you want to position it. 然后在要放置的位置调用它。

[self createButton:@"Text" atPos:CGPointMake(0, 0)];

However you can try set edge inset too for the solution 但是您也可以尝试为解决方案设置edge inset

See here to get some idea. 看到这里获得一些想法。

Hope it helps. 希望能帮助到你。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM