iphone - Detecting backspace in UITextField the right way -
attention! question not duplicate of this or this question! new question create things more clear.
so have followed instructions of posts above , have uitextfield
becomes empty , moves new position every time user hits homecoming button. moreover, trace input of textview , create label of text in position textview was, this:
- (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 %@", nsstringfromcgrect(newtextfieldframe)); textfield.frame = newtextfieldframe; textfield.text = @"\u200b"; } homecoming yes; }
i set text in uitextfield
text @"\u200b"
, want observe backspace button on this:
- (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"; } } homecoming yes; }
but problem is, works once , then, special character added @ origin of textfield doesn't work. perchance wrong?
after setting
textfield.text = @"\u200b";
in textfield:shouldchangecharactersinrange:replacementstring:
you have add
return no;
otherwise go on homecoming yes , replace @"" , when pressing backspace 1 time 1 time again nil changed , delegate method not called.
it possible misunderstood goal here, hope helps.
iphone ios objective-c cocoa-touch uitextfield
No comments:
Post a Comment