以编程方式在UIButton上设置图像和文本

Har*_*ain 41 iphone ios4 ios xcode4.2

我需要以编程方式创建按钮,用于正常和突出显示的状态以及文本.我无法使用Interface Builder构建它,因为我需要在a上创建按钮UIScrollView.这是我到目前为止的代码:

- (void)loadView {
    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
    scrollView.contentSize=CGSizeMake(320,960);

    UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpeg"]];

    UIImage * buttonImage = [UIImage imageNamed:@"contentlist_active.png"];

    self.view=scrollView;
    [scrollView addSubview:tempImageView2];

    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(22, 100, 277, 32);

    [btn setImage:buttonImage forState:UIControlStateNormal]; 

    [btn setTitle:@"hello world" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [scrollView addSubview:btn];

}
Run Code Online (Sandbox Code Playgroud)

但是按钮上的文字没有显示.如果我注释掉了setImagefor button,则文本显示完美,否则不显示.我可以同时同时拥有文字和图片吗?

iPr*_*abu 79

UIButtons setImage将图像设置在标题上方,这样您就无法看到它下面的文本.因此,尝试使用setBackgroundImage将图像设置为按钮.

Objective-C的:

[btn setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
Run Code Online (Sandbox Code Playgroud)

迅速:

myButton.setBackgroundImage(buttonImage, forState: .normal)
Run Code Online (Sandbox Code Playgroud)


小智 24

你在那里犯了一个错误,你正在做

[btn setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
Run Code Online (Sandbox Code Playgroud)

代替

[btn setImage:buttonImage forState:UIControlStateNormal]; 
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作.


Azh*_*ikh 6

雨燕4

  • 您可以将图像和文本一起设置为属性文本,并且可以通过实现以下代码来完成此操作:
  • 在这里,我有自定义的函数,您可以在其中传递标题字符串按钮图像,它将返回您一个属性文本,您可以在 UIButton/UILabel 标题上设置为属性标题/文本。

- 如果你想显示带有标题的图像前缀

func AttributedTextwithImagePrefix(AttributeImage : UIImage , AttributedText : String , buttonBound : UIButton) -> NSMutableAttributedString
{
    let fullString = NSMutableAttributedString(string: "   ")
    let image1Attachment = NSTextAttachment()
    image1Attachment.bounds = CGRect(x: 0, y: ((buttonBound.titleLabel?.font.capHeight)! - AttributeImage.size.height).rounded() / 2, width: AttributeImage.size.width, height: AttributeImage.size.height)
    image1Attachment.image = AttributeImage
    let image1String = NSAttributedString(attachment: image1Attachment)
    fullString.append(image1String)
    fullString.append(NSAttributedString(string: "  " + AttributedText))
    return fullString
}
Run Code Online (Sandbox Code Playgroud)

您可以这样使用它:

self.btnprefix.setAttributedTitle(AttributedTextwithImagePrefix(AttributeImage: #imageLiteral(resourceName: "avtar"), AttributedText: " prefix avtar", buttonBound: self.prefix), for: .normal)
Run Code Online (Sandbox Code Playgroud)

- 如果你想显示带有标题的图像后缀

func AttributedTextwithImageSuffix(AttributeImage : UIImage , AttributedText : String , buttonBound : UIButton) -> NSMutableAttributedString
{
    let fullString = NSMutableAttributedString(string: AttributedText + "  ")
    let image1Attachment = NSTextAttachment()
    image1Attachment.bounds = CGRect(x: 0, y: ((buttonBound.titleLabel?.font.capHeight)! - AttributeImage.size.height).rounded() / 2, width: AttributeImage.size.width, height: AttributeImage.size.height)
    image1Attachment.image = AttributeImage
    let image1String = NSAttributedString(attachment: image1Attachment)
    fullString.append(image1String)
    fullString.append(NSAttributedString(string: ""))
    return fullString
}
Run Code Online (Sandbox Code Playgroud)

您可以这样使用它:

self.btnsuffix.setAttributedTitle(AttributedTextwithImageSuffix(AttributeImage: #imageLiteral(resourceName: "avtar"), AttributedText: " suffix avtar", buttonBound: self.btnsuffix), for: .normal)
Run Code Online (Sandbox Code Playgroud)

输出将是

在此输入图像描述