Search code examples
iosiphoneswiftuilabel

How to know the width of last line of label?


In my app I have a label which text came from the server so I don't Know its width, and at the end of this label directly there should be an UIImage.

My problem is: I don't know how to position the image because of the non static width of the label text.

To be more clear, this is a snapshot form the design and how should it be:

snapshot

Any suggestion to solve this problem please?


Solution

    1. Set the text of the UILabel
    2. Get the width of this UILabel with yourUILabel.frame.width
    3. Set the x coordinate of your UIImage at yourUILabel.frame.width + emptySpace like this

      var yourUIImageView:UIImageView = UIImageView(frame: CGRectMake(x:PaddingFromLeft + yourUILabel.frame.width + emptySpace, y: yourYCoordinate, width: yourImageWidth, height : yourImageHeight))