2017-10-31 6 views
0

テーブルビューのセルで中央揃えのUITextfieldを持っています。 ユーザーがテキストフィールドをタップすると、そのプレースホルダーが少し左に移動します。 私はスクリーンショットも添付しました。私を助けてください。中央揃えのUITextfieldは、タップしたときにそのプレースホルダーを左に向かって変更します。

In first picture, fusion id is moved towards left as its selected.

In second picture, password is moved towards left as its selected.

+0

コードを投稿できますか?または、これをストーリーボード/ xibで作成しましたか? –

+0

@RhuariGlen in storyboard –

+0

最初の文字は空白であってはならないことをご了承ください(プレースホルダ付き)。 –

答えて

1

私はこのようなのUITextFieldのサブクラス:Interface Builderで

class MyTextField : UITextField { 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 

     if let placeholderText = self.placeholder { 

      let centeredParagraphStyle = NSMutableParagraphStyle() 
      centeredParagraphStyle.alignment = .center 
      let attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: [NSAttributedStringKey.paragraphStyle: centeredParagraphStyle]) 
      self.attributedPlaceholder = attributedPlaceholder 
     } 
    } 
} 

その後はするmyTextField

Set the Custom Class of the IB Object

をするUITextFieldのクラスを設定します

ここではnormal carat aligns to beginning of Placeholder which is also beginning UITexField centered carat aligns only to beginning of placeholder normal text entry aligns to beginning of UITextField "centred" text entry also aligns to beginning of MyTextField

これは、あなたが望む完全な動作ではないかもしれない

Two placeholders behaving differently UITextFieldの

に比べするmyTextFieldの行動のイメージですが、プレースホルダは、それを「飛び回る」としません。必要に応じてこの種の技法を拡張して他の振る舞いを克服することが可能である。

+0

私のケースでは動作しません –

+0

カスタムクラスを正しく設定したかどうか確認できますか?更新された回答を参照してください... – Damo

+0

さらに、コードはswift 3でコンパイルされていません。 [NSAttributedStringKey.paragraphStyle:centeredParagraphStyle] –

関連する問題