igo igo -4 years ago 107
Objective-C Question

How to give equal spacing between letters of the word for the placeholder text in iOS

- (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textField.placeholder];
[attributedString addAttribute:NSKernAttributeName
textField.attributedPlaceholder = attributedString;
return YES;

I have tried this method but unfortunately, the method will work only something entered in the UITextField, But I want before entering anything in UITextField. The placeholder should have equal spacing between characters.
Thank you in advance

I want the output as the second UITextField shown in the image:enter image description here

Answer Source
  • (void)viewDidLoad { [super viewDidLoad];

    NSMutableAttributedString *attributedString ;

    attributedString = [[NSMutableAttributedString alloc] initWithString:self.firsttextfield.placeholder]; [attributedString addAttribute:NSKernAttributeName value:@(1.5) range:NSMakeRange(0,self.firsttextfield.placeholder.length)]; self.firsttextfield.attributedPlaceholder = attributedString; self.secondtexfield.attributedPlaceholder = attributedString; }

    I just kept the above lines in viewdidload rather than keeping them in the textfield function. hope it works

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download