如何创建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,左占位符,右编辑文本?

textfield image

UITextField itself does not allow using its properties to put placeholder and edited text to different sides. 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.

UITextField本身不允许使用它的属性将占位符和编辑文本放到不同的边。尝试在故事板->属性检查器->对齐中设置它。它们都在左边或都在右边。只有占位符或文本没有特殊对齐。

4 个解决方案

#1


7  

To do this you should add a UILabel to the UITextField which is not editable and doesn't allow touches. Then set the text alignment on the UITextField to NSTextAlignmentRight.

为此,您应该向UITextField添加一个UILabel,这是不可编辑的,不允许触摸。然后将UITextField上的文本对齐设置为NSTextAlignmentRight。

#2


5  

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:

Fir占位符,您可以将其添加为子视图,或者使用leftView属性:

    [textField setLeftView:<your label here>];

#3


1  

I just created a custom UITextField subclass that does this. 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:

我刚刚创建了一个定制的UITextField子类来实现这一点。要使用它,只需在故事板或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%的完美,因为标签的大小不是动态设置的…但是它应该能让你起床跑步,它能工作:-)

#4


0  

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.

希望这一点你能找到正确的方向。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/09/11/72078cc579d52586b730c611ec068e91.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com