如何創建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-2020 ITdaan.com