Easy way to make a UILabel cover and exactly match contents of a UITextField?
-
06-09-2019 - |
Question
UI crimes aside... I'd like to have a UILabel replace a UITextField after some user input. Is there an easy way to accomplish this so when the UITextField is hidden its value gets replaced by a UILabel that doesn't appear to move...appearing to "set" into the background so to speak?
I've managed to do this by nudging the fields around in IB and making the fonts identical but as I'm slowly getting comfortable with Cocoa I was wondering if there might be a quick trick in the frameworks somewhere?!? Perhaps some way to extract the text's view off the UITextField?
Solution
Setting the borderStyle property on the UITextField
to UITextBorderStyleNone
should make it look a lot like an UILabel
.
Basically implementing two of the UITextFieldDelegate methods like so:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.borderStyle = UITextBorderStyleRoundedRect;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.borderStyle = UITextBorderStyleNone;
}