内容简介:我知道这个问题已被多次询问,但是我已经弃用了,我需要在ios 8中使用它.由于我有一个文本字段,我需要占位符在中心对齐,测试的其余部分保持对齐.请帮助我.翻译自:https://stackoverflow.com/questions/28404725/center-align-placeholder-in-textfield
我知道这个问题已被多次询问,但是我已经弃用了,我需要在ios 8中使用它.由于我有一个文本字段,我需要占位符在中心对齐,测试的其余部分保持对齐.请帮助我.
创建IBOutlet并将其连接到textField.在YourViewController.m中
@interface YourViewController () <UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *txt;
在你的viewDidLoad中
self.txt.delegate=self; self.txt.textAlignment=NSTextAlignmentCenter;
编写此委托方法.每当文本字段中的文本发生更改时,此方法都会调用此方法.
- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
NSRange textFieldRange = NSMakeRange(0, [self.txt.text length]);
// Check If textField is empty. If empty align your text field to center, so that placeholder text will show center aligned
if (NSEqualRanges(range, textFieldRange) && [string length] == 0) {
self.txt.textAlignment=NSTextAlignmentCenter;
}
else //else align textfield to left.
{
self.txt.textAlignment=NSTextAlignmentLeft;
}
return YES;
}
翻译自:https://stackoverflow.com/questions/28404725/center-align-placeholder-in-textfield
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
API Design for C++
Martin Reddy / Morgan Kaufmann / 2011-2-18 / USD 59.95
The design of application programming interfaces can affect the behavior, capabilities, stability, and ease of use of end-user applications. With this book, you will learn how to design a good API for......一起来看看 《API Design for C++》 这本书的介绍吧!