cmii
cmii

Reputation: 3636

Wrap a text around uibutton SWIFT

I need to display a text around a button.

In my view controller I have:

class myViewController: UIViewController {

    @IBOutlet var backButton: UIButton!
    @IBOutlet var myTitle: UITextView!

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let exclusionPath:UIBezierPath = UIBezierPath(rect: backButton.frame)
        myText.textContainer.exclusionPaths=[exclusionPath]
    }

     override func viewDidLoad() {
         super.viewDidLoad()
         myTitle.text="Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu"
     }

}

I get in the simulator

in the simulator

The expected result:

expected result

What's wrong?

Upvotes: 5

Views: 745

Answers (1)

Dima
Dima

Reputation: 23634

The documentation for exclusionPaths describes it as: An array of UIBezierPath objects representing the exclusion paths inside the receiver's bounding rectangle. Default value: nil.

You are using the frame of another view to set this property, which uses a different coordinate system. Log the values of each of these properties (frame and bounds of your label, and frame of your button) to see how the values relate and how to translate from one to the other.

Relevant documentation: Converting Between View Coordinate Systems

Upvotes: 2

Related Questions