注意力!这个问题不是这个或这个问题的重复!这是一个让事情更清楚的新问题。
因此,我已按照上述帖子的所有说明进行操作,UITextField
每次用户点击返回按钮时,我都会将其变为空并移动到新位置。此外,我跟踪 textView 的输入,并在 TextView 所在的位置创建该文本的标签,如下所示:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.text.length > 5) {
CGRect labelFrame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 0, 0);
UILabel *label = [[UILabel alloc] initWithFrame: labelFrame];
label.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
[label setText:textField.text];
[label setTextColor: [BSFunctions getColorFromHex:@"3f3f3f"]];
label.backgroundColor =[UIColor lightGrayColor];
[label sizeToFit];
[labelsArray addObject:label];
[self.view addSubview: label];
CGRect newTextFieldFrame = CGRectMake(labelFrame.origin.x + label.frame.size.width + 5, labelFrame.origin.y, 320, 30);
NSLog(@"Rect is %@", NSStringFromCGRect(newTextFieldFrame));
textField.frame = newTextFieldFrame;
textField.text = @"\u200B";
}
return YES;
}
我将文本中的UITextField
文本设置为@"\u200B"
,然后我想检测它上面的退格按钮,如下所示:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:@""]) {
NSLog(@"backspace button pressed");
if (labelsArray.count > 0) {
UILabel *labelToDelete = [labelsArray lastObject];
CGRect labelPosition = labelToDelete.frame;
CGRect oldPosition = textField.frame;
textField.frame = CGRectMake(labelPosition.origin.x, labelPosition.origin.y, oldPosition.size.width, oldPosition.size.height);
[labelToDelete removeFromSuperview];
[labelsArray removeLastObject];
textField.text = @"\u200B";
}
}
return YES;
}
但问题是,它只能工作一次,即使在 textField 开头添加了特殊字符,它也不起作用。什么可能是错的?