Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton with both image and text possible?

I have a button of size width 200 and height 270. I want to have text and image on the same button. Not as a background image on that text. Instead of this, I want to display the text with height 120 and image of height 150 on the same button. How to do it?

like image 301
Viral Narshana Avatar asked Dec 03 '10 10:12

Viral Narshana


People also ask

How do I put the image on the right side of the text in a UIButton?

To make an UIButton's image align to the right side of the text, we force content flipping behavior to the one use it right-to-left language. 1 By adding this semanticContentAttribute = . forceRightToLeft , we force UIKit to mirrored our content which push our image to the right side of the text. Here is the result.

How do I use imageEdgeInsets?

The rule when it comes to titleEdgeInsets or imageEdgeInsets is to add equal and opposite offsets to the left and right insets. So if you add for example 8 points to the left title inset, you need to apply -8 points to the right inset.

What is a UIButton?

A control that executes your custom code in response to user interactions.


1 Answers

You can use this code,it will serve your purpose

.h file code IBOutlet 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.

like image 176
Aditya Avatar answered Oct 12 '22 18:10

Aditya