我需要在 UITextField
中移动 text
(从右到左,然后向后移动),就像在 iOS 7 中的 Safari 应用程序 (见下面的截图)。
有什么办法吗?
我知道我可以设置 textAlignment
,但不会设置动画。
也许我应该更改 UITextField
中的 leftView
大小?但我不确定如何计算大小,当文本应该在中心对齐时。请帮忙~
(来源:cs424818.vk.me)
最佳答案
您可以通过这样做来实现此行为:
- (void)viewDidLoad
{
// add this to viewDidLoad
// keep the alignment left justified
[self.yourTextField setTextAlignment:NSTextAlignmentLeft];
[self.yourTextField addTarget:self action:@selector(textFieldDidChange)forControlEvents:UIControlEventEditingChanged];
}
-(void)textFieldDidChange
{
// this will cause your textfield to grow and shrink as text is added or removed
// note: this will always grow to the right, the lefthand side will stay anchored
[self.yourTextField sizeToFit];
}
然后当你想将你的 UITextField 从中心移动到左边时,你可以简单地做:
// 1 second animation
[UIView animateWithDuration:1.0 animations:^{
// move to the left side of some containing view
self.yourTextField.center = CGPointMake(containingView.frame.origin.x + self.yourTextField.frame.size.width/2, self.yourTextField.center.y);
}];
同样从左侧移动到中心你可以这样做:
[UIView animateWithDuration:1.0 animations:^{
// move to the center of containing view
self.yourTextField.center = CGPointMake(containingView.frame.size.width/2, self.yourTextField.center.y);
}];
希望这对您有所帮助!
关于ios - 使用动画在 UITextField 中移动文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20891627/