0

Scenario:

i have VC in which i have horizontally place 4 UIButton and 4 UIImageView attached to them, each button has their own individual view controllers every time they are pressed they display a respective VC.

Issue:

  • The problem is I have to change the text color and the image color that I have used on clicking the button.

  • Is it possible that i can use single button that has the image also text?

  • how to change the fonts of the button on being click.

Image :

These are the 4 buttons i made them like i place image view and a button

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
Saba Zehra
  • 63
  • 1
  • 10

5 Answers5

2
  1. You can subclass a UIButton to create your own button. Ultimately a button is inherited from UIView and thus, you can simply subclass and add any style, that you want to be added to a button. Subclassing a UIButton will give you absolute freedom in terms of design. You can also use API functions to set the image/text of the button, as shown in below

    button.setImage(UIImage(named: "<image placeholder>") for: .normal)

    Please note that the .normal is the state of the button and any changes to normal state gets applied to all other states of the button. If you wish to make changes to a specific state, you should mention that.

  2. We attach a selector to the touch events of a button. The selector gets called when an event (like touchupinside aka click) occurs. In this selector you can write code for new font of the button. You can set the font as below

    myButton.titleLabel!.font = UIFont(name: YourfontName, size: 20)

prabodhprakash
  • 3,825
  • 24
  • 48
  • i have button actions and outlet. what next should i do? i am new to ios and swift has been 2 months to me. so can you explain little simple terms. i have been stucked here since 7 days. – Saba Zehra Aug 22 '17 at 06:27
  • In the function that gets called "on-click" of the button - you can simply mention the changes that you want to see in the button. – prabodhprakash Aug 22 '17 at 06:29
  • 1
    @IBAction func btnClicked(_ sender: UIButton) { btn.setImage(UIImage(named: "img1.jpg") for: .normal) btn.titleLabel?.text = "Text" } – Nishant Bhindi Aug 22 '17 at 06:31
1

You can use both text and image in UIButton like below

button.setImage(UIImage(named: "imageName") for: .normal)
button.setTitle("Title", for: .normal)

Use contentHorizontalAlignment of UIButton to set button title to the left or right of the image.

Use imageEdgeInsets and contentEdgeInsets of UIButton to set margin

Use IBAction of UIButton to change the image, title and font of the button on click

Use .highlighted state of UIButton to set image and title in highlighted state of UIButton

Himanshu
  • 287
  • 3
  • 9
0

of course, you can use a button, because we can set the image and text for a button.

button.setImage(UIImage(named: "imageName") for: .normal)
button.setTitle("the title string", for: .normal)

And button text font:

button.titleLabel?.font = UIFont(name: "font string you want", size: 18)

Highlighted status color:

button.setTitleColor(UIColor.red, for: .highlighted)

with Objective-C you can exchange the image and the text position in the button :

_titleViewBtn.size=CGSizeMake(100, 20);

CGFloat imgX=_titleViewBtn.imageView.frame.origin.x;
CGFloat titleX=_titleViewBtn.titleLabel.frame.origin.x;
if (imgX<titleX) {
    CGRect titleFrame=_titleViewBtn.titleLabel.frame;
    titleFrame.origin.x=imgX;
    _titleViewBtn.titleLabel.frame=titleFrame;

    CGRect imgFrame=_titleViewBtn.imageView.frame;
    imgFrame.origin.x=CGRectGetMaxX(_titleViewBtn.titleLabel.frame);
    _titleViewBtn.imageView.frame=imgFrame;

}
Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
0

You can set button.setImage(image: UIImage(named: "abc"), for: .normal) button.setTitle(image: "abc", for: .normal)

You change state action button ".normal" is state other.

0

so guys here is the best solution :

Q1) The problem is I have to change the text color and the image color that I have used on clicking the button. A) a. drag and drop a button to the VC b. in ATTRIBUTES INSPECTOR by default the state is default choose the color you want i choose grey and now click on the arrow in the side of default and choose SELECTED state and choose the color and image for that in the image put the image.png. c. go to the SIZE INSPECTOR and adjust the size of the image and text of the button . d. go to you respected swift file and create each buttons iboutlet and ibaction , in the action of each button use the following lines:

   button1.isselected = true
   button2.isselected = false

it worked for me.

Q2)Is it possible that i can use single button that has the image also text?

A) yes steps are above

Q3)how to change the fonts of the button on being click. A) still working

Saba Zehra
  • 63
  • 1
  • 10