Yasser
Yasser

Reputation: 263

creating spaces in placeholder fields

my problem is I want to put 2 placeholders for one textField. one of the place holders should be on the left and the other should be on the right.

my code:

func returnPlaceHolder(first: String, second: String, textField: UITextField){

    let width: Int = Int(textField.bounds.width * 0.2)
    let spaceValue = width - (first.count + second.count)
    var temp = "\(first) "
    let tempCount = spaceValue - (first.count + second.count)
    var value = String()

    for _ in 0..<tempCount {
       temp.append(" ")


    }


    value = "\(temp) \(second)"
    textField.placeholder = value
    textField.setLeftPaddingPoints(10)
    textField.setRightPaddingPoints(10)


}

I'm currently using this function to create spaces.. but my problem is the spaces won't be the same for more than one textField, and I want them to be aligned..

just like this picture: https://i.sstatic.net/5H3Wd.jpg

and this is the result I'm getting for my current code: https://i.sstatic.net/4DisG.jpg

don't mind the textFields format & text I can fix them later.. I just want to be able to align the textFields placeholders.

Upvotes: 1

Views: 282

Answers (1)

Allan Spreys
Allan Spreys

Reputation: 5697

It would be really hard (if possible) to achieve what you are trying to do by injecting spaces into the text because each character in the font has a different width unless you use a monospaced font.

https://en.wikipedia.org/wiki/Monospaced_font

Instead, I would recommend a different approach. Override the text field, provide two UILabels and adjust their position using Autolayout.

Upvotes: 2

Related Questions