How to implement a UIButton consisting of an image on the left side and text on the right

Possible duplicate:
UIButton header and image request

I need to implement a UIButton show in the bottom image

please help me how to implement

Thank you Prasad

enter image description here

+6
source share
3 answers

Take a look at the titleEdgeInsets UIButton property.

 @property(nonatomic) UIEdgeInsets titleEdgeInsets 

Use the approach below to get both with UIButton .

 [myButton setImage: [UIImage imageNamed:@"settingImage.png"] forState:UIControlStateNormal]; [myButton setTitleEdgeInsets:UIEdgeInsetsMake(100.0, -100.0, 10.0, 15.0)]; [myButton setTitle:@"settings" forState:UIControlStateNormal]; 
+14
source

Take the button outputs.

 UIImageView *img1=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue-thumb.jpg"]]; CGRect rect1=CGRectMake(0,0,20,30); [img1 setFrame:rect1]; [btn1 addSubview:img1]; 

You can also set a shortcut with it.

0
source

The easiest one I find is simply to create a png file that has a width (left image + text width + left image) but contains only the left settings image.

Then use this as the background of your button.

The text comes from the button itself, so you can easily change the text without changing the image. If you now center the text, it will look perfect with the image on the left and next to it.

The code will look like this:

  • [myBtn setTitle: @"settings" forState:UIControlStateNormal]
  • [myBtn setBackgroundImage: [UIImage imageNamed:@"myBtn.png"] forState:UIControlStateNormal];

It will also be compatible with localization if you ever plan to use it in another langauge.

0
source

Source: https://habr.com/ru/post/896765/


All Articles