Creating a custom UITextView with UILabels and also the text in it
I would like to create a custom one UITextView
with the ability to enter text in it, as well as programmatically add there UILabels
, which will act like text (I need to delete them with the "backspace" button when the cursor is near them).
This one UITextView
should be expandable and the labels can have different widths.
Any ideas on how you can create stuff like this, any tutorials or such?
source to share
you can create a textbox using this code.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
And you can use this code to create a label:
CGRect labelFrame = CGRectMake( 10, 40, 100, 30 );
UILabel* label = [[UILabel alloc] initWithFrame: labelFrame];
[label setText: @"My Label"];
[label setTextColor: [UIColor orangeColor]];
label.backgroundColor =[UIColor clearColor];
[view addSubview: label];
and to remove the shortcut if the reverse tape uses this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:@""]) {
NSLog(@"backspace button pressed");
[label removeFromSuperview];
}
return YES;
}
If the backspace key is pressed, replaceString (string) will have a null value. So we can identify the backspace key press with this.
source to share