How to add space between UITextField Placeholder characters?
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.attributedText
space between characters not applied.
Without Secure Field
With Secure Field
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
|
show 4 more comments
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.attributedText
space between characters not applied.
Without Secure Field
With Secure Field
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
Why aren't you usingattributedPlaceholder
?
– 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 forattributedPlaceholder
.
– 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
|
show 4 more comments
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.attributedText
space between characters not applied.
Without Secure Field
With Secure Field
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
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.attributedText
space between characters not applied.
Without Secure Field
With Secure Field
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
swift uitextfield nsattributedstring placeholder
edited Apr 20 '18 at 6:17
Kuldeep
asked Apr 19 '18 at 12:54
KuldeepKuldeep
2,50441735
2,50441735
Why aren't you usingattributedPlaceholder
?
– 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 forattributedPlaceholder
.
– 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
|
show 4 more comments
Why aren't you usingattributedPlaceholder
?
– 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 forattributedPlaceholder
.
– 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
|
show 4 more comments
3 Answers
3
active
oldest
votes
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"
add a comment |
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:
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:
EDIT: 2
After analyzing the demo project provided by @kuldeep it seems
secureTextEntry
is not working if we usecustomFont
(other than the system). I tried withsystemFont
and it works and its also working for all fonts if we uncheck thesecureTextEntry
.
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
}
Thanks, it's working, but there's 1password
filed withsecure entry
, in that fieldtxtField.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
|
show 14 more comments
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
This code only add Leading space between placeholder but I want to add space betweentextField
every character. supposetextField
text isDeveloper
than I wantD e v e l o p e r
.
– Kuldeep
Apr 19 '18 at 13:09
Why don't you useattributedPlaceholder
?
– Arnab Hore
Apr 19 '18 at 13:29
@ArnabHore Updated your answer after reading mine :|
– TheTiger
Apr 20 '18 at 4:49
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"
add a comment |
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"
add a comment |
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"
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"
answered Apr 19 '18 at 13:29
![](https://i.stack.imgur.com/1qmFo.jpg?s=32&g=1)
![](https://i.stack.imgur.com/1qmFo.jpg?s=32&g=1)
KamranKamran
6,85521028
6,85521028
add a comment |
add a comment |
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:
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:
EDIT: 2
After analyzing the demo project provided by @kuldeep it seems
secureTextEntry
is not working if we usecustomFont
(other than the system). I tried withsystemFont
and it works and its also working for all fonts if we uncheck thesecureTextEntry
.
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
}
Thanks, it's working, but there's 1password
filed withsecure entry
, in that fieldtxtField.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
|
show 14 more comments
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:
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:
EDIT: 2
After analyzing the demo project provided by @kuldeep it seems
secureTextEntry
is not working if we usecustomFont
(other than the system). I tried withsystemFont
and it works and its also working for all fonts if we uncheck thesecureTextEntry
.
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
}
Thanks, it's working, but there's 1password
filed withsecure entry
, in that fieldtxtField.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
|
show 14 more comments
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:
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:
EDIT: 2
After analyzing the demo project provided by @kuldeep it seems
secureTextEntry
is not working if we usecustomFont
(other than the system). I tried withsystemFont
and it works and its also working for all fonts if we uncheck thesecureTextEntry
.
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
}
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:
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:
EDIT: 2
After analyzing the demo project provided by @kuldeep it seems
secureTextEntry
is not working if we usecustomFont
(other than the system). I tried withsystemFont
and it works and its also working for all fonts if we uncheck thesecureTextEntry
.
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
}
edited Nov 22 '18 at 8:56
Kuldeep
2,50441735
2,50441735
answered Apr 19 '18 at 13:16
![](https://i.stack.imgur.com/tkXuD.png?s=32&g=1)
![](https://i.stack.imgur.com/tkXuD.png?s=32&g=1)
TheTigerTheTiger
10.9k34466
10.9k34466
Thanks, it's working, but there's 1password
filed withsecure entry
, in that fieldtxtField.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
|
show 14 more comments
Thanks, it's working, but there's 1password
filed withsecure entry
, in that fieldtxtField.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
|
show 14 more comments
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
This code only add Leading space between placeholder but I want to add space betweentextField
every character. supposetextField
text isDeveloper
than I wantD e v e l o p e r
.
– Kuldeep
Apr 19 '18 at 13:09
Why don't you useattributedPlaceholder
?
– Arnab Hore
Apr 19 '18 at 13:29
@ArnabHore Updated your answer after reading mine :|
– TheTiger
Apr 20 '18 at 4:49
add a comment |
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
This code only add Leading space between placeholder but I want to add space betweentextField
every character. supposetextField
text isDeveloper
than I wantD e v e l o p e r
.
– Kuldeep
Apr 19 '18 at 13:09
Why don't you useattributedPlaceholder
?
– Arnab Hore
Apr 19 '18 at 13:29
@ArnabHore Updated your answer after reading mine :|
– TheTiger
Apr 20 '18 at 4:49
add a comment |
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
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
edited Apr 19 '18 at 13:28
answered Apr 19 '18 at 13:05
![](https://i.stack.imgur.com/o3BDf.jpg?s=32&g=1)
![](https://i.stack.imgur.com/o3BDf.jpg?s=32&g=1)
Arnab HoreArnab Hore
2,07511329
2,07511329
This code only add Leading space between placeholder but I want to add space betweentextField
every character. supposetextField
text isDeveloper
than I wantD e v e l o p e r
.
– Kuldeep
Apr 19 '18 at 13:09
Why don't you useattributedPlaceholder
?
– Arnab Hore
Apr 19 '18 at 13:29
@ArnabHore Updated your answer after reading mine :|
– TheTiger
Apr 20 '18 at 4:49
add a comment |
This code only add Leading space between placeholder but I want to add space betweentextField
every character. supposetextField
text isDeveloper
than I wantD e v e l o p e r
.
– Kuldeep
Apr 19 '18 at 13:09
Why don't you useattributedPlaceholder
?
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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