Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I don't want to use a subview if I can avoid it. I want a UIButton with a background image, text, and an image in it. Right now, when I do that, the image is on the left side of the text. The background image, text, and image all have different highlight states.

like image 317
jasongregori Avatar asked Aug 18 '11 00:08

jasongregori


People also ask

How do I add text to UIButton?

Programmatically. To make a multi-line text in UIButton, you insert a new line character ( \n ) wherever you want in button title and set lineBreakMode to byWordWrapping . You can adjust text alignment with . textAlignment .

What is a UIButton?

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

How do I make a button in Swift?

SwiftUI's button is similar to UIButton , except it's more flexible in terms of what content it shows and it uses a closure for its action rather than the old target/action system. To create a button with a string title you would start with code like this: Button("Button title") { print("Button tapped!") }


1 Answers

Simplest solution:

iOS 10 & up, Swift:

button.transform = CGAffineTransform(scaleX: -1.0, y: 1.0) button.titleLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0) button.imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0) 

Before iOS 10, Swift/Obj-C:

button.transform = CGAffineTransformMakeScale(-1.0, 1.0); button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0); button.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0); 

iOS 9 & up, Swift: (Recommended)

button.semanticContentAttribute = .forceRightToLeft 
like image 151
Liau Jian Jie Avatar answered Oct 19 '22 23:10

Liau Jian Jie