Uitextfield Attributedplaceholder Has No Effect

UITextField attributedPlaceholder has no effect

As noted by warren, the styling currently can't be accomplished the way you're trying. A good workaround would be to set up your textfield's font attributes the way you would like your placeholder to look and then change the font of the textfield whenever the user begins typing. It will look like the placeholder and text are different fonts.

You can do this by creating a delegate of the textfield and utilizing shouldChangeCharactersinRange like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// If there is text in the text field
if (textField.text.length + (string.length - range.length) > 0) {
// Set textfield font
textField.font = [UIFont fontWithName:@"Font" size:14];
} else {
// Set textfield placeholder font (or so it appears)
textField.font = [UIFont fontWithName:@"PlaceholderFont" size:14];
}

return YES;
}

iPhone UITextField - Change placeholder text color

You can override drawPlaceholderInRect:(CGRect)rect as such to manually render the placeholder text:

- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}

UITextField Issue With TextField Effects

I Solved the answer of this question.

Problem is that. if I was running on simulator. it was not showing me properly. But, When I run on Real Device (Means iPhone ) It was working fine.

UITextField hide placeholder text or bring cursor to front when clicked swift

First thing make sure you set the delegate of the text field either from storyboard or set it in the viewDidLoad()

And change your code to this

userTextField.placeholder = ""
passwordTextField.placeholder = ""

How to manually add UITextField placeholder text that disappears when clicked inside textfield?

You can use the delegate method textFieldShouldBeginEditing(textField:) for it:

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var textField: CustomTextField!

override func viewDidLoad() {
super.viewDidLoad()

textField.placeholder = "Some text..."
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
textField.placeholder = ""
return true
}
}

UIImage next to centered UITextField Placeholder Text

You can attach images inside attributed strings using NSTextAttachment

Swift 3.0

txtField.textAlignment = .center
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "searchIcon")
attachment.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
let attachmentStr = NSAttributedString(attachment: attachment)
let myString = NSMutableAttributedString(string: "")
myString.append(attachmentStr)
let myString1 = NSMutableAttributedString(string: "Find a Location (Zip Code or Name)")
myString.append(myString1)
txtField.attributedPlaceholder = myString

Objective-C

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"searchIcon"];
attachment.bounds = CGRectMake(0, 0, 10, 10);
NSAttributedString *attachmentStr = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:@""];
[myString appendAttributedString:attachmentStr];
NSMutableAttributedString *myString1 = [[NSMutableAttributedString alloc] initWithString:@"Find a Location (Zip Code or Name)"];
[myString appendAttributedString:myString1];
txtField.attributedPlaceholder = myString;


Related Topics



Leave a reply



Submit