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

时间:2015-02-09 07:24:57

标签: ios uitextfield placeholder

我知道这个问题已被多次询问,但是我已经弃用了,我需要在ios 8中使用它。由于我有一个文本字段,我需要占位符在中心对齐并且测试的其余部分保持对齐。请帮助我。< / p>

5 个答案:

答案 0 :(得分:20)

接受的答案方式使事情过于复杂......

通过使用attributedPlaceholder加上段落样式,您可以将占位符置于UITextField的中心位置。

let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center
let attributedPlaceholder = NSAttributedString(string: "Placeholder", attributes: [NSAttributedStringKey.paragraphStyle: centeredParagraphStyle])
textField.attributedPlaceholder = attributedPlaceholder

答案 1 :(得分:6)

创建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;
}

答案 2 :(得分:6)

@Clay Ellis的答案是正确的,这是针对Objective-C:

UITextField* field = [[UITextField alloc] initWithFrame: fieldRect];
NSTextAlignment alignment = NSTextAlignmentCenter;
NSMutableParagraphStyle* alignmentSetting = [[NSMutableParagraphStyle alloc] init];
alignmentSetting.alignment = alignment;
NSDictionary *attributes = @{NSParagraphStyleAttributeName : alignmentSetting};
NSAttributedString *str = [[NSAttributedString alloc] initWithString:placeholder attributes: attributes];
field.attributedPlaceholder = str;

答案 3 :(得分:0)

  

基于 Clay Ellis 回答

将UIKit导入扩展文件,因为NSTextAlignment是UIKit的一部分。

详细

xCode 9.1,swift 4

解决方案

extension String {
    func attributedString(alignment: NSTextAlignment) -> NSAttributedString {
        return NSAttributedString(text: self, alignment: alignment)
    }
}

extension NSAttributedString {
    convenience init(text: String, alignment: NSTextAlignment) {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = alignment
        self.init(string: text, attributes: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
    }
}

用法

// Way 1
textField.attributedPlaceholder = text.attributedString(alignment: .center)
// Way 2
textField.attributedPlaceholder = "title".attributedString(alignment: .center)
// Way 3
textField.attributedPlaceholder = NSAttributedString(text: text, alignment: .left)

代码示例:

let placeholder = "This is a placeholder"
let textField = UITextField()
textField.placeholder = placeholder
textField.attributedPlaceholder = placeholder.attributedString(alignment: .center)

对于Swift 4.2,将NSAttributedStringKey更改为NSAttributedString.Key。

答案 4 :(得分:0)

@Clay Ellis在Swift 5中的答案

let centeredParagraphStyle = NSMutableParagraphStyle()
    centeredParagraphStyle.alignment = .center
    let attributedPlaceholder = NSAttributedString(string: "Placeholder", attributes: [NSAttributedString.Key.paragraphStyle: centeredParagraphStyle])
    textField.attributedPlaceholder = attributedPlaceholder