简体   繁体   English

如何使用左侧占位符和右侧编辑文本创建UITextField?

[英]How to create UITextField with placeholder to the left and edited text to the right?

How to create UITextField with placeholder to the left and edited text to the right? 如何使用左侧占位符和右侧编辑文本创建UITextField?

文本域图像

UITextField itself does not allow using its properties to put placeholder and edited text to different sides. UITextField本身不允许使用其属性将占位符和编辑的文本放到不同的方面。 Try to set it in the storyboard -> attributes inspector -> alignment. 尝试在故事板中设置它 - >属性检查器 - >对齐。 They are both to the left or both the right. 它们都在左边或右边。 There is no special alignment only for placeholder or only for the text. 仅对占位符或仅对文本没有特殊对齐。

To do this you should add a UILabel to the UITextField which is not editable and doesn't allow touches. 为此,您应该将UILabel添加到UITextField ,该UILabel不可编辑且不允许触摸。 Then set the text alignment on the UITextField to NSTextAlignmentRight . 然后将UITextField上的文本对齐方式设置为NSTextAlignmentRight

First, set the text alignment to the right: 首先,将文本对齐方式设置为右侧:

    [textField setTextAlignment:NSTextAlignmentRight];

Fir the placeholder, you can add it as a subview, or use the leftView property: leftView占位符,可以将其添加为子视图,或使用leftView属性:

    [textField setLeftView:<your label here>];

I just created a custom UITextField subclass that does this. 我刚刚创建了一个自定义的UITextField子类来执行此操作。 To use it simply create your UITextField on the Storyboard or Xib... put a placeholder text in there, set the text fields class as OBSTextFieldWithLabel and then you can use the following code: 要使用它,只需在Storyboard或Xib上创建UITextField ...在其中放置占位符文本,将文本字段类设置为OBSTextFieldWithLabel,然后您可以使用以下代码:

@interface OBSTextFieldWithLabel : UITextField
@end

@implementation OBSTextFieldWithLabel
  - (void)awakeFromNib
  {
    [super awakeFromNib];

    NSString *labelText = [self placeholder];
    self.placeholder = nil;
    CGRect labelFrame = CGRectMake(20, 10, 150, 15);
    UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:labelFrame];
    [placeholderLabel setFont:[UIFont systemFontOfSize:12]];
    [placeholderLabel setTextColor:[UIColor lightGrayColor]];
    [placeholderLabel setText:labelText];
    [self addSubview:placeholderLabel];
    [self setTextAlignment:NSTextAlignmentRight];
  }
@end

This is probably not 100% perfect as the label size is not dynamically set and all...but it should get you up and running and it works :-) 这可能不是100%完美,因为标签大小没有动态设置和所有...但它应该让你启动并运行它的工作原理:-)

I've written a control that might help you. 我写了一个可以帮助你的控件。 Essentially it's intended for highlighting a UITextField while it is being edited, but it also has a mechanism for a placeholder on the left, as you can see in the screenshots. 本质上它是用于在编辑时突出显示UITextField,但它也有一个左侧占位符的机制,如屏幕截图所示。

https://www.cocoacontrols.com/controls/sahighlightedtextfield https://www.cocoacontrols.com/controls/sahighlightedtextfield

Hope this points you in the right direction. 希望这能指出你正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM