Simply put another label with the desired text behind or beside the label or text field that is going to be filled. You can set the font
and textColor
and even the alpha
according to your taste.
Make sure that if they overlap (which they should not!) the backgroundColor
property of the label is set to [UIColor clearColor]
.
You could subclass UITextField
like this:
@interface LabeledTextField : UIView
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UITextField *textField;
@end
#define kPercentWidth 0.5
@implementation LabeledTextField
-initWithCoder:(NSCoder)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
CGRect f = self.frame;
_label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,
f.size.width*kPercentWidth,f.size.height)];
_textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,
f.size.width*(1-kPercentWidth),f.size.height)];
[self addSubView:_label];
[self addSubView:_textField];
}
return self;
}
@end
You could then use it like this: insert a UIView
into your storyboard view controller and change the class to your LabeledTextField
. This would ensure initWithCoder
is called. Otherwise, you might have to put the init code into its own setup
function and call it from your override of initWithFrame
. Make sure you wire up the view with your outlet
// .h
#include LabeledTextField.h
//...
@property (nonatomic, strong) IBOutlet LabeledTextField *labeledTextField;
// .m, in some method
labeledTextField.textField.text = @"editable text";
labeledTextField.label.text = @"non-editable text";
Similarly, you could modify all properties of the label and text field, including colors, fonts etc.