ios – 在文本字段中居中对齐占位符

栏目: IOS · 发布时间: 7年前

内容简介:我知道这个问题已被多次询问,但是我已经弃用了,我需要在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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Essential ActionScript 3.0

Essential ActionScript 3.0

Colin Moock / Adobe Dev Library / June 22, 2007 / $34.64

ActionScript 3.0 is a huge upgrade to Flash's programming language. The enhancements to ActionScript's performance, feature set, ease of use, cleanliness, and sophistication are considerable. Essentia......一起来看看 《Essential ActionScript 3.0》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码