Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 735
cmii Avatar asked Nov 01 '22 12:11

cmii


1 Answers

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

like image 56
Dima Avatar answered Nov 12 '22 21:11

Dima