krikor Herlopian
krikor Herlopian

Reputation: 731

Not able to get my placeholder onto the right of my textview

placeholderLabel  = UILabel()
        placeholderLabel.text = localizedMsgComment
        placeholderLabel.font = UIFont.italicSystemFont(ofSize: (body.font?.pointSize)!)
        placeholderLabel.sizeToFit()
        placeholderLabel.textAlignment = .right
        body.addSubview(placeholderLabel)
        placeholderLabel.frame.origin = CGPoint(x: 5, y: (body.font?.pointSize)! / 2)
        placeholderLabel.textColor = UIColor.lightGray
        placeholderLabel.isHidden = !body.text.isEmpty

Basically i have that, and it adds a placeholder to my textview ( body) . But this placeholderlabel is seen on the left side of textview, i want it to be on the right side. I tried to play with text alignment and x: 5 things, but couldnt get it done.So am asking here for help

Upvotes: 0

Views: 36

Answers (1)

Stefan Stefanov
Stefan Stefanov

Reputation: 829

It's because you are setting placeholderLabel.sizeToFit() and in this way you are making the size of the frame to be just as big so that it can fit the text in it, thus it does not matter if you set your textAlignment to right, that would not change anything.

In order for this to work you would need to delete the following lines:

placeholderLabel.sizeToFit()
placeholderLabel.frame.origin = CGPoint(x: 5, y: (body.font?.pointSize)! / 2)

and instead use:

placeholder.frame = body.frame
placeholder.textAlignment = .right

Upvotes: 1

Related Questions