Reputation: 1191
I'd like to have textfield with constant text in it non-editable like:
ENTER YOUR NAME:
And clicking at textfield would get cursor right after :
The user should not be able to delete or edit "ENTER YOUR NAME:" also.
How can I implement this?
Upvotes: 1
Views: 308
Reputation: 1191
I have managed to get it done in a bit tricky but easy way.
UIButton *rem = [UIButton buttonWithType:UIButtonTypeCustom];
rem.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
rem.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
rem.frame = CGRectMake(0.0f, 210.0f, 300.0f, 40.0f);
[rem setTitle:@"*X1*" forState:UIControlStateNormal];
CALayer * layer1 = [rem layer];
[layer1 setMasksToBounds:YES];
[layer1 setCornerRadius:8.0];
[layer1 setBorderWidth:0.0];
[layer1 setBorderColor:[[UIColor grayColor] CGColor]];
rem.backgroundColor = [UIColor colorWithPatternImage:image];
rem.titleLabel.font = [UIFont systemFontOfSize:17.0f];
rem.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField = [[UITextField alloc] initWithFrame:CGRectMake(45.0f, 210.0f, 255.0f, 40.0f)];
textField.backgroundColor = [UIColor clearColor];
[_textFields addObject:textField];
[textField setBorderStyle:(UITextBorderStyleNone)];
textField.textColor = [UIColor whiteColor];
[TableControll.view addSubview:rem];
[TableControll.view addSubview:textField];
Basicaly i just create button with title of UNEDITABLE NAME which size is the size of desirable textField
. Then I create textfield without style with width - size of text
and add it on top. Uneditable text won't be selectable. But i guess you can implement action for button to select textfield.
Upvotes: 0
Reputation: 2940
I don't have enough reputation or I'd just leave a comment, but what Omar is suggesting is to use a UILabel that in your case would say "ENTER YOUR NAME:" and then place your UITextField however far apart from that label as you'd like to control where the cursor is.
To make it look nice, you could put some sort of background textfield-style image (which is what I do), or stretch the UILabel's default background to the desirable length and then make sure the UITextField's subview is above the label's.
Upvotes: 0
Reputation: 663
One solution would be to check if your static content (static text) still exists, and if not just return NO in shouldChangeCharactersInRange. Here a a sample
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSRange prefixRange = [textField.text rangeOfString:@"ENTER YOUR NAME:"];
if(suffixRange.location == NSNotFound) {
return NO;
}
// Only characters in the NSCharacterSet you choose will insertable.
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
ALPHA is #define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Upvotes: 0
Reputation: 726579
Set the delegate of your text field to an instance of UITextFieldDelegate
implementation, and use the textField:shouldChangeCharactersInRange:replacementString:
method to see if the user is attempting to change the ENTER YOUR NAME:
string:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// "ENTER YOUR NAME:" occupies the first 16 characters; if the user us trying
// to change something that is within that 16-character range, say "NO"
if (range.location < 16) return NO;
... check additional conditions here
return YES;
}
I don't think there is a way to place the cursor directly, but you can make an empty selection after the trailing colon of the initial text as described in this answer.
Upvotes: 2