How to add space between UITextField Placeholder characters?












0















I want to add Space between UITextField placeHolder Text.



func setTextFieldCharacterSpacing(txtField: TextFieldCustomize) {
let attributedString = NSMutableAttributedString(string: txtField.text!)
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
txtField.attributedText = attributedString
}


Edit 1



I have done this by using textField delegate method as below.



extension LoginVC: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
CommonHelper.sharedInstance.setTextFieldCharacterSpacing(txtField: textField as! TextFieldCustomize)
return true
}
}


Edit 2



I have 1 textField with secure entry. when I type anything in that textField, txtField.attributedTextspace between characters not applied.



Without Secure Field
enter image description here



With Secure Field



enter image description here



This works for Username but not for password secure field.



how to add space between characters in secure entry textField?



Thanks in advance.










share|improve this question

























  • Why aren't you using attributedPlaceholder?

    – paulvs
    Apr 19 '18 at 12:57











  • because I also want to add space between character in placeholder.

    – Kuldeep
    Apr 19 '18 at 12:58











  • And what is the output of your code?

    – TheTiger
    Apr 19 '18 at 13:10













  • code added in my question is working fine for space between character. but facing error while using this code for attributedPlaceholder.

    – Kuldeep
    Apr 19 '18 at 13:16











  • The secure entry field having the text and others filed having placeholder.

    – TheTiger
    Apr 20 '18 at 5:44
















0















I want to add Space between UITextField placeHolder Text.



func setTextFieldCharacterSpacing(txtField: TextFieldCustomize) {
let attributedString = NSMutableAttributedString(string: txtField.text!)
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
txtField.attributedText = attributedString
}


Edit 1



I have done this by using textField delegate method as below.



extension LoginVC: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
CommonHelper.sharedInstance.setTextFieldCharacterSpacing(txtField: textField as! TextFieldCustomize)
return true
}
}


Edit 2



I have 1 textField with secure entry. when I type anything in that textField, txtField.attributedTextspace between characters not applied.



Without Secure Field
enter image description here



With Secure Field



enter image description here



This works for Username but not for password secure field.



how to add space between characters in secure entry textField?



Thanks in advance.










share|improve this question

























  • Why aren't you using attributedPlaceholder?

    – paulvs
    Apr 19 '18 at 12:57











  • because I also want to add space between character in placeholder.

    – Kuldeep
    Apr 19 '18 at 12:58











  • And what is the output of your code?

    – TheTiger
    Apr 19 '18 at 13:10













  • code added in my question is working fine for space between character. but facing error while using this code for attributedPlaceholder.

    – Kuldeep
    Apr 19 '18 at 13:16











  • The secure entry field having the text and others filed having placeholder.

    – TheTiger
    Apr 20 '18 at 5:44














0












0








0








I want to add Space between UITextField placeHolder Text.



func setTextFieldCharacterSpacing(txtField: TextFieldCustomize) {
let attributedString = NSMutableAttributedString(string: txtField.text!)
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
txtField.attributedText = attributedString
}


Edit 1



I have done this by using textField delegate method as below.



extension LoginVC: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
CommonHelper.sharedInstance.setTextFieldCharacterSpacing(txtField: textField as! TextFieldCustomize)
return true
}
}


Edit 2



I have 1 textField with secure entry. when I type anything in that textField, txtField.attributedTextspace between characters not applied.



Without Secure Field
enter image description here



With Secure Field



enter image description here



This works for Username but not for password secure field.



how to add space between characters in secure entry textField?



Thanks in advance.










share|improve this question
















I want to add Space between UITextField placeHolder Text.



func setTextFieldCharacterSpacing(txtField: TextFieldCustomize) {
let attributedString = NSMutableAttributedString(string: txtField.text!)
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
txtField.attributedText = attributedString
}


Edit 1



I have done this by using textField delegate method as below.



extension LoginVC: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
CommonHelper.sharedInstance.setTextFieldCharacterSpacing(txtField: textField as! TextFieldCustomize)
return true
}
}


Edit 2



I have 1 textField with secure entry. when I type anything in that textField, txtField.attributedTextspace between characters not applied.



Without Secure Field
enter image description here



With Secure Field



enter image description here



This works for Username but not for password secure field.



how to add space between characters in secure entry textField?



Thanks in advance.







swift uitextfield nsattributedstring placeholder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 20 '18 at 6:17







Kuldeep

















asked Apr 19 '18 at 12:54









KuldeepKuldeep

2,49441534




2,49441534













  • Why aren't you using attributedPlaceholder?

    – paulvs
    Apr 19 '18 at 12:57











  • because I also want to add space between character in placeholder.

    – Kuldeep
    Apr 19 '18 at 12:58











  • And what is the output of your code?

    – TheTiger
    Apr 19 '18 at 13:10













  • code added in my question is working fine for space between character. but facing error while using this code for attributedPlaceholder.

    – Kuldeep
    Apr 19 '18 at 13:16











  • The secure entry field having the text and others filed having placeholder.

    – TheTiger
    Apr 20 '18 at 5:44



















  • Why aren't you using attributedPlaceholder?

    – paulvs
    Apr 19 '18 at 12:57











  • because I also want to add space between character in placeholder.

    – Kuldeep
    Apr 19 '18 at 12:58











  • And what is the output of your code?

    – TheTiger
    Apr 19 '18 at 13:10













  • code added in my question is working fine for space between character. but facing error while using this code for attributedPlaceholder.

    – Kuldeep
    Apr 19 '18 at 13:16











  • The secure entry field having the text and others filed having placeholder.

    – TheTiger
    Apr 20 '18 at 5:44

















Why aren't you using attributedPlaceholder?

– paulvs
Apr 19 '18 at 12:57





Why aren't you using attributedPlaceholder?

– paulvs
Apr 19 '18 at 12:57













because I also want to add space between character in placeholder.

– Kuldeep
Apr 19 '18 at 12:58





because I also want to add space between character in placeholder.

– Kuldeep
Apr 19 '18 at 12:58













And what is the output of your code?

– TheTiger
Apr 19 '18 at 13:10







And what is the output of your code?

– TheTiger
Apr 19 '18 at 13:10















code added in my question is working fine for space between character. but facing error while using this code for attributedPlaceholder.

– Kuldeep
Apr 19 '18 at 13:16





code added in my question is working fine for space between character. but facing error while using this code for attributedPlaceholder.

– Kuldeep
Apr 19 '18 at 13:16













The secure entry field having the text and others filed having placeholder.

– TheTiger
Apr 20 '18 at 5:44





The secure entry field having the text and others filed having placeholder.

– TheTiger
Apr 20 '18 at 5:44












3 Answers
3






active

oldest

votes


















1














Your attirbutedString code is okay you're just putting it in wrong property of textField. attributedPlaceholder is the right one. attributedText is used to set the text value not placeholder.



So change



txtField.attributedText = attributedString


to



textField.attributedPlaceholder = attributedString


Below is the output:



enter image description here



EDIT: While user is typing



Add target on textField for editingDidChange event



textField.addTarget(self, action: #selector(textFieldEdidtingDidChange(_ :)), for: UIControlEvents.editingChanged)


And here is how can you give space while user is typing.



@objc func textFieldEdidtingDidChange(_ textField :UITextField) {
let attributedString = NSMutableAttributedString(string: textField.text!)
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
textField.attributedText = attributedString
}


Output:



enter image description here



EDIT: 2




After analyzing the demo project provided by @kuldeep it seems secureTextEntry is not working if we use customFont (other than the system). I tried with systemFont and it works and its also working for all fonts if we uncheck the secureTextEntry.




I don't know why it spacing is not working in that case but I come up with below solution:



I'm not gonna using secureTextEntry instead I will replace all the characters on editingChange event. So I created a custom class:



class PasswordField: TextFieldCustomize {
var originalText: String? /// Your original text
var customChar = "•" /// Custom character to replace with original character while typing
}
/// TextFieldCustomize is a custom class I found in demo project.


And here is the updated method for editingChange:



@objc func textFieldEdidtingDidChange(_ textField :UITextField) {

var string = textField.text;
if let passwordField = textField as? PasswordField {
/// Replace all the characters by custom character •
var newString = ""
let count = string?.characters.count ?? 0
for i in 0..<count {
if i == count - 1 {
let lastChar = string!.characters.last!
newString.append(lastChar)
}
else {
newString.append(passwordField.customChar)
}
}
string = newString

/// This will be your original text, So use passwordField.originalText instead of passwordField.text when needed
passwordField.originalText = textField.text
}

let attributedString = NSMutableAttributedString(string: string!)
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
textField.attributedText = attributedString
}





share|improve this answer


























  • Thanks, it's working, but there's 1 password filed with secure entry, in that field txtField.attributedText doesn't work.

    – Kuldeep
    Apr 20 '18 at 5:02











  • @Kuldeep Please update this in your question too so that we can answer accordingly.

    – TheTiger
    Apr 20 '18 at 5:05











  • I updated my question.

    – Kuldeep
    Apr 20 '18 at 5:10











  • Its also working for secured text entry.

    – TheTiger
    Apr 20 '18 at 5:35











  • I updated my question with screenshot.

    – Kuldeep
    Apr 20 '18 at 5:42



















0














If you want left padding in TextField just use below code, it'll add padding to the left for placeholder as well as text



txtField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);


The amount of left padding you can change by changing the value of at first argument .



Edit



If you want space between characters, why don't you use attributedPlaceholder property? I tried your code with attributedPlaceholder, and it works perfectly.



let attributedString = NSMutableAttributedString(string: "Developer")
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
txtField.attributedPlaceholder = attributedString





share|improve this answer


























  • This code only add Leading space between placeholder but I want to add space between textField every character. suppose textField text is Developer than I want D e v e l o p e r.

    – Kuldeep
    Apr 19 '18 at 13:09











  • Why don't you use attributedPlaceholder?

    – Arnab Hore
    Apr 19 '18 at 13:29











  • @ArnabHore Updated your answer after reading mine :|

    – TheTiger
    Apr 20 '18 at 4:49



















0














For placeholder you should do it like this,



let attributedString = NSMutableAttributedString(string: txtField.placeholder!)
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
txtField.attributedPlaceholder = attributedString


If you have not set placeholder text in storyboard then you should also set its value before calling the above snippet,



txtField.placeholder = "Developer"





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49921687%2fhow-to-add-space-between-uitextfield-placeholder-characters%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Your attirbutedString code is okay you're just putting it in wrong property of textField. attributedPlaceholder is the right one. attributedText is used to set the text value not placeholder.



    So change



    txtField.attributedText = attributedString


    to



    textField.attributedPlaceholder = attributedString


    Below is the output:



    enter image description here



    EDIT: While user is typing



    Add target on textField for editingDidChange event



    textField.addTarget(self, action: #selector(textFieldEdidtingDidChange(_ :)), for: UIControlEvents.editingChanged)


    And here is how can you give space while user is typing.



    @objc func textFieldEdidtingDidChange(_ textField :UITextField) {
    let attributedString = NSMutableAttributedString(string: textField.text!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    textField.attributedText = attributedString
    }


    Output:



    enter image description here



    EDIT: 2




    After analyzing the demo project provided by @kuldeep it seems secureTextEntry is not working if we use customFont (other than the system). I tried with systemFont and it works and its also working for all fonts if we uncheck the secureTextEntry.




    I don't know why it spacing is not working in that case but I come up with below solution:



    I'm not gonna using secureTextEntry instead I will replace all the characters on editingChange event. So I created a custom class:



    class PasswordField: TextFieldCustomize {
    var originalText: String? /// Your original text
    var customChar = "•" /// Custom character to replace with original character while typing
    }
    /// TextFieldCustomize is a custom class I found in demo project.


    And here is the updated method for editingChange:



    @objc func textFieldEdidtingDidChange(_ textField :UITextField) {

    var string = textField.text;
    if let passwordField = textField as? PasswordField {
    /// Replace all the characters by custom character •
    var newString = ""
    let count = string?.characters.count ?? 0
    for i in 0..<count {
    if i == count - 1 {
    let lastChar = string!.characters.last!
    newString.append(lastChar)
    }
    else {
    newString.append(passwordField.customChar)
    }
    }
    string = newString

    /// This will be your original text, So use passwordField.originalText instead of passwordField.text when needed
    passwordField.originalText = textField.text
    }

    let attributedString = NSMutableAttributedString(string: string!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    textField.attributedText = attributedString
    }





    share|improve this answer


























    • Thanks, it's working, but there's 1 password filed with secure entry, in that field txtField.attributedText doesn't work.

      – Kuldeep
      Apr 20 '18 at 5:02











    • @Kuldeep Please update this in your question too so that we can answer accordingly.

      – TheTiger
      Apr 20 '18 at 5:05











    • I updated my question.

      – Kuldeep
      Apr 20 '18 at 5:10











    • Its also working for secured text entry.

      – TheTiger
      Apr 20 '18 at 5:35











    • I updated my question with screenshot.

      – Kuldeep
      Apr 20 '18 at 5:42
















    1














    Your attirbutedString code is okay you're just putting it in wrong property of textField. attributedPlaceholder is the right one. attributedText is used to set the text value not placeholder.



    So change



    txtField.attributedText = attributedString


    to



    textField.attributedPlaceholder = attributedString


    Below is the output:



    enter image description here



    EDIT: While user is typing



    Add target on textField for editingDidChange event



    textField.addTarget(self, action: #selector(textFieldEdidtingDidChange(_ :)), for: UIControlEvents.editingChanged)


    And here is how can you give space while user is typing.



    @objc func textFieldEdidtingDidChange(_ textField :UITextField) {
    let attributedString = NSMutableAttributedString(string: textField.text!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    textField.attributedText = attributedString
    }


    Output:



    enter image description here



    EDIT: 2




    After analyzing the demo project provided by @kuldeep it seems secureTextEntry is not working if we use customFont (other than the system). I tried with systemFont and it works and its also working for all fonts if we uncheck the secureTextEntry.




    I don't know why it spacing is not working in that case but I come up with below solution:



    I'm not gonna using secureTextEntry instead I will replace all the characters on editingChange event. So I created a custom class:



    class PasswordField: TextFieldCustomize {
    var originalText: String? /// Your original text
    var customChar = "•" /// Custom character to replace with original character while typing
    }
    /// TextFieldCustomize is a custom class I found in demo project.


    And here is the updated method for editingChange:



    @objc func textFieldEdidtingDidChange(_ textField :UITextField) {

    var string = textField.text;
    if let passwordField = textField as? PasswordField {
    /// Replace all the characters by custom character •
    var newString = ""
    let count = string?.characters.count ?? 0
    for i in 0..<count {
    if i == count - 1 {
    let lastChar = string!.characters.last!
    newString.append(lastChar)
    }
    else {
    newString.append(passwordField.customChar)
    }
    }
    string = newString

    /// This will be your original text, So use passwordField.originalText instead of passwordField.text when needed
    passwordField.originalText = textField.text
    }

    let attributedString = NSMutableAttributedString(string: string!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    textField.attributedText = attributedString
    }





    share|improve this answer


























    • Thanks, it's working, but there's 1 password filed with secure entry, in that field txtField.attributedText doesn't work.

      – Kuldeep
      Apr 20 '18 at 5:02











    • @Kuldeep Please update this in your question too so that we can answer accordingly.

      – TheTiger
      Apr 20 '18 at 5:05











    • I updated my question.

      – Kuldeep
      Apr 20 '18 at 5:10











    • Its also working for secured text entry.

      – TheTiger
      Apr 20 '18 at 5:35











    • I updated my question with screenshot.

      – Kuldeep
      Apr 20 '18 at 5:42














    1












    1








    1







    Your attirbutedString code is okay you're just putting it in wrong property of textField. attributedPlaceholder is the right one. attributedText is used to set the text value not placeholder.



    So change



    txtField.attributedText = attributedString


    to



    textField.attributedPlaceholder = attributedString


    Below is the output:



    enter image description here



    EDIT: While user is typing



    Add target on textField for editingDidChange event



    textField.addTarget(self, action: #selector(textFieldEdidtingDidChange(_ :)), for: UIControlEvents.editingChanged)


    And here is how can you give space while user is typing.



    @objc func textFieldEdidtingDidChange(_ textField :UITextField) {
    let attributedString = NSMutableAttributedString(string: textField.text!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    textField.attributedText = attributedString
    }


    Output:



    enter image description here



    EDIT: 2




    After analyzing the demo project provided by @kuldeep it seems secureTextEntry is not working if we use customFont (other than the system). I tried with systemFont and it works and its also working for all fonts if we uncheck the secureTextEntry.




    I don't know why it spacing is not working in that case but I come up with below solution:



    I'm not gonna using secureTextEntry instead I will replace all the characters on editingChange event. So I created a custom class:



    class PasswordField: TextFieldCustomize {
    var originalText: String? /// Your original text
    var customChar = "•" /// Custom character to replace with original character while typing
    }
    /// TextFieldCustomize is a custom class I found in demo project.


    And here is the updated method for editingChange:



    @objc func textFieldEdidtingDidChange(_ textField :UITextField) {

    var string = textField.text;
    if let passwordField = textField as? PasswordField {
    /// Replace all the characters by custom character •
    var newString = ""
    let count = string?.characters.count ?? 0
    for i in 0..<count {
    if i == count - 1 {
    let lastChar = string!.characters.last!
    newString.append(lastChar)
    }
    else {
    newString.append(passwordField.customChar)
    }
    }
    string = newString

    /// This will be your original text, So use passwordField.originalText instead of passwordField.text when needed
    passwordField.originalText = textField.text
    }

    let attributedString = NSMutableAttributedString(string: string!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    textField.attributedText = attributedString
    }





    share|improve this answer















    Your attirbutedString code is okay you're just putting it in wrong property of textField. attributedPlaceholder is the right one. attributedText is used to set the text value not placeholder.



    So change



    txtField.attributedText = attributedString


    to



    textField.attributedPlaceholder = attributedString


    Below is the output:



    enter image description here



    EDIT: While user is typing



    Add target on textField for editingDidChange event



    textField.addTarget(self, action: #selector(textFieldEdidtingDidChange(_ :)), for: UIControlEvents.editingChanged)


    And here is how can you give space while user is typing.



    @objc func textFieldEdidtingDidChange(_ textField :UITextField) {
    let attributedString = NSMutableAttributedString(string: textField.text!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    textField.attributedText = attributedString
    }


    Output:



    enter image description here



    EDIT: 2




    After analyzing the demo project provided by @kuldeep it seems secureTextEntry is not working if we use customFont (other than the system). I tried with systemFont and it works and its also working for all fonts if we uncheck the secureTextEntry.




    I don't know why it spacing is not working in that case but I come up with below solution:



    I'm not gonna using secureTextEntry instead I will replace all the characters on editingChange event. So I created a custom class:



    class PasswordField: TextFieldCustomize {
    var originalText: String? /// Your original text
    var customChar = "•" /// Custom character to replace with original character while typing
    }
    /// TextFieldCustomize is a custom class I found in demo project.


    And here is the updated method for editingChange:



    @objc func textFieldEdidtingDidChange(_ textField :UITextField) {

    var string = textField.text;
    if let passwordField = textField as? PasswordField {
    /// Replace all the characters by custom character •
    var newString = ""
    let count = string?.characters.count ?? 0
    for i in 0..<count {
    if i == count - 1 {
    let lastChar = string!.characters.last!
    newString.append(lastChar)
    }
    else {
    newString.append(passwordField.customChar)
    }
    }
    string = newString

    /// This will be your original text, So use passwordField.originalText instead of passwordField.text when needed
    passwordField.originalText = textField.text
    }

    let attributedString = NSMutableAttributedString(string: string!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    textField.attributedText = attributedString
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 8:56









    Kuldeep

    2,49441534




    2,49441534










    answered Apr 19 '18 at 13:16









    TheTigerTheTiger

    10.8k34365




    10.8k34365













    • Thanks, it's working, but there's 1 password filed with secure entry, in that field txtField.attributedText doesn't work.

      – Kuldeep
      Apr 20 '18 at 5:02











    • @Kuldeep Please update this in your question too so that we can answer accordingly.

      – TheTiger
      Apr 20 '18 at 5:05











    • I updated my question.

      – Kuldeep
      Apr 20 '18 at 5:10











    • Its also working for secured text entry.

      – TheTiger
      Apr 20 '18 at 5:35











    • I updated my question with screenshot.

      – Kuldeep
      Apr 20 '18 at 5:42



















    • Thanks, it's working, but there's 1 password filed with secure entry, in that field txtField.attributedText doesn't work.

      – Kuldeep
      Apr 20 '18 at 5:02











    • @Kuldeep Please update this in your question too so that we can answer accordingly.

      – TheTiger
      Apr 20 '18 at 5:05











    • I updated my question.

      – Kuldeep
      Apr 20 '18 at 5:10











    • Its also working for secured text entry.

      – TheTiger
      Apr 20 '18 at 5:35











    • I updated my question with screenshot.

      – Kuldeep
      Apr 20 '18 at 5:42

















    Thanks, it's working, but there's 1 password filed with secure entry, in that field txtField.attributedText doesn't work.

    – Kuldeep
    Apr 20 '18 at 5:02





    Thanks, it's working, but there's 1 password filed with secure entry, in that field txtField.attributedText doesn't work.

    – Kuldeep
    Apr 20 '18 at 5:02













    @Kuldeep Please update this in your question too so that we can answer accordingly.

    – TheTiger
    Apr 20 '18 at 5:05





    @Kuldeep Please update this in your question too so that we can answer accordingly.

    – TheTiger
    Apr 20 '18 at 5:05













    I updated my question.

    – Kuldeep
    Apr 20 '18 at 5:10





    I updated my question.

    – Kuldeep
    Apr 20 '18 at 5:10













    Its also working for secured text entry.

    – TheTiger
    Apr 20 '18 at 5:35





    Its also working for secured text entry.

    – TheTiger
    Apr 20 '18 at 5:35













    I updated my question with screenshot.

    – Kuldeep
    Apr 20 '18 at 5:42





    I updated my question with screenshot.

    – Kuldeep
    Apr 20 '18 at 5:42













    0














    If you want left padding in TextField just use below code, it'll add padding to the left for placeholder as well as text



    txtField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);


    The amount of left padding you can change by changing the value of at first argument .



    Edit



    If you want space between characters, why don't you use attributedPlaceholder property? I tried your code with attributedPlaceholder, and it works perfectly.



    let attributedString = NSMutableAttributedString(string: "Developer")
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    txtField.attributedPlaceholder = attributedString





    share|improve this answer


























    • This code only add Leading space between placeholder but I want to add space between textField every character. suppose textField text is Developer than I want D e v e l o p e r.

      – Kuldeep
      Apr 19 '18 at 13:09











    • Why don't you use attributedPlaceholder?

      – Arnab Hore
      Apr 19 '18 at 13:29











    • @ArnabHore Updated your answer after reading mine :|

      – TheTiger
      Apr 20 '18 at 4:49
















    0














    If you want left padding in TextField just use below code, it'll add padding to the left for placeholder as well as text



    txtField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);


    The amount of left padding you can change by changing the value of at first argument .



    Edit



    If you want space between characters, why don't you use attributedPlaceholder property? I tried your code with attributedPlaceholder, and it works perfectly.



    let attributedString = NSMutableAttributedString(string: "Developer")
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    txtField.attributedPlaceholder = attributedString





    share|improve this answer


























    • This code only add Leading space between placeholder but I want to add space between textField every character. suppose textField text is Developer than I want D e v e l o p e r.

      – Kuldeep
      Apr 19 '18 at 13:09











    • Why don't you use attributedPlaceholder?

      – Arnab Hore
      Apr 19 '18 at 13:29











    • @ArnabHore Updated your answer after reading mine :|

      – TheTiger
      Apr 20 '18 at 4:49














    0












    0








    0







    If you want left padding in TextField just use below code, it'll add padding to the left for placeholder as well as text



    txtField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);


    The amount of left padding you can change by changing the value of at first argument .



    Edit



    If you want space between characters, why don't you use attributedPlaceholder property? I tried your code with attributedPlaceholder, and it works perfectly.



    let attributedString = NSMutableAttributedString(string: "Developer")
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    txtField.attributedPlaceholder = attributedString





    share|improve this answer















    If you want left padding in TextField just use below code, it'll add padding to the left for placeholder as well as text



    txtField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);


    The amount of left padding you can change by changing the value of at first argument .



    Edit



    If you want space between characters, why don't you use attributedPlaceholder property? I tried your code with attributedPlaceholder, and it works perfectly.



    let attributedString = NSMutableAttributedString(string: "Developer")
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    txtField.attributedPlaceholder = attributedString






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 19 '18 at 13:28

























    answered Apr 19 '18 at 13:05









    Arnab HoreArnab Hore

    2,06411128




    2,06411128













    • This code only add Leading space between placeholder but I want to add space between textField every character. suppose textField text is Developer than I want D e v e l o p e r.

      – Kuldeep
      Apr 19 '18 at 13:09











    • Why don't you use attributedPlaceholder?

      – Arnab Hore
      Apr 19 '18 at 13:29











    • @ArnabHore Updated your answer after reading mine :|

      – TheTiger
      Apr 20 '18 at 4:49



















    • This code only add Leading space between placeholder but I want to add space between textField every character. suppose textField text is Developer than I want D e v e l o p e r.

      – Kuldeep
      Apr 19 '18 at 13:09











    • Why don't you use attributedPlaceholder?

      – Arnab Hore
      Apr 19 '18 at 13:29











    • @ArnabHore Updated your answer after reading mine :|

      – TheTiger
      Apr 20 '18 at 4:49

















    This code only add Leading space between placeholder but I want to add space between textField every character. suppose textField text is Developer than I want D e v e l o p e r.

    – Kuldeep
    Apr 19 '18 at 13:09





    This code only add Leading space between placeholder but I want to add space between textField every character. suppose textField text is Developer than I want D e v e l o p e r.

    – Kuldeep
    Apr 19 '18 at 13:09













    Why don't you use attributedPlaceholder?

    – Arnab Hore
    Apr 19 '18 at 13:29





    Why don't you use attributedPlaceholder?

    – Arnab Hore
    Apr 19 '18 at 13:29













    @ArnabHore Updated your answer after reading mine :|

    – TheTiger
    Apr 20 '18 at 4:49





    @ArnabHore Updated your answer after reading mine :|

    – TheTiger
    Apr 20 '18 at 4:49











    0














    For placeholder you should do it like this,



    let attributedString = NSMutableAttributedString(string: txtField.placeholder!)
    attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
    txtField.attributedPlaceholder = attributedString


    If you have not set placeholder text in storyboard then you should also set its value before calling the above snippet,



    txtField.placeholder = "Developer"





    share|improve this answer




























      0














      For placeholder you should do it like this,



      let attributedString = NSMutableAttributedString(string: txtField.placeholder!)
      attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
      txtField.attributedPlaceholder = attributedString


      If you have not set placeholder text in storyboard then you should also set its value before calling the above snippet,



      txtField.placeholder = "Developer"





      share|improve this answer


























        0












        0








        0







        For placeholder you should do it like this,



        let attributedString = NSMutableAttributedString(string: txtField.placeholder!)
        attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
        txtField.attributedPlaceholder = attributedString


        If you have not set placeholder text in storyboard then you should also set its value before calling the above snippet,



        txtField.placeholder = "Developer"





        share|improve this answer













        For placeholder you should do it like this,



        let attributedString = NSMutableAttributedString(string: txtField.placeholder!)
        attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
        txtField.attributedPlaceholder = attributedString


        If you have not set placeholder text in storyboard then you should also set its value before calling the above snippet,



        txtField.placeholder = "Developer"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 19 '18 at 13:29









        KamranKamran

        6,54821028




        6,54821028






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49921687%2fhow-to-add-space-between-uitextfield-placeholder-characters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen