UIButton with both image and text possible? UIButton with both image and text possible? xcode xcode

UIButton with both image and text possible?


You can use this code,it will serve your purpose

.h file codeIBOutlet UIButton *testBtn;.m file code[testBtn setImage: [UIImage imageNamed:@"Image name.png"] forState:UIControlStateNormal];[testBtn setTitleEdgeInsets:UIEdgeInsetsMake(70.0, -150.0, 5.0, 5.0)];[testBtn setTitle:@"Your text" forState:UIControlStateNormal];

Adjust the coordinates and size of your button as your requirement.This is a sample code to guide you.

PS:Take a UIButton in the nib file>Make it a custom button>Connect the IBOutlet to your custom button in the nib file.Thats it.


The below code shows placing an image and text in a button using setImageEdgeInsets and setTitleEdgeInsets properties of UIButton class.

UIButton *butt=[UIButton buttonWithType:UIButtonTypeCustom ];    [butt setFrame:CGRectMake(0, 0, 100, 100)];    [butt setBackgroundImage:[UIImage imageNamed:@"sig.png"] forState:UIControlStateNormal];    [butt setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 50.0, 0.0)];    [butt setTitleEdgeInsets:UIEdgeInsetsMake(75.0, 0.0, 0.0, 0.0)];    [butt setTitle:@"hello" forState:UIControlStateNormal];    [butt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [self.view addSubview:butt];


In Swift 3

let button = UIButton()//make sure the image (jpg, png) you are placing in the button//is about the right size or it will push the label off the button//add imagelet image = UIImage(named:"my_button_image")button.setImage(image, for: .normal)button.imageView?.contentMode = .scaleAspectFitbutton.imageEdgeInsets = UIEdgeInsets(top:0, left:-30, bottom:0, right:0) //adjust these to have fit right//add titlebutton.setTitle("Fan", for: .normal)button.titleEdgeInsets = UIEdgeInsets(top:0, left:10, bottom:0, right:0) //adjust insets to have fit how you want//add button to your view - like in viewDidLoad or your own custom view setupaddSubview(button)

Don't forget to set anchors if doing so programmatically for it to appear/attach it to your button reference in Interface Builder if using a storyboard