2015-12-19 28 views
5

私はSwiftの新作ですので、私は画像とテキストをUITextFieldを中心とした水平方向の整列として追加したいと思います。次のコードを使用して、テキストボックスに画像を追加することができましたが、中心にあり、テキストボックスにフォーカスが移ってもそこに残ります。私はプレースホルダーのテキストを中心にしたいと思って、UITextFieldがフォーカスを得るとそれは隠れます。UITextFieldにイメージとテキストを追加する方法

var imageView = UIImageView() 
var image = UIImage(named: "all.png") 
imageView.image = image 
searchFiled.leftView = imageView 

答えて

0

フォーカスによって、あなたはUITextFieldの上でユーザーがクリックを意味するならば、何が欲しいのはUITextFieldDelegateとして、あなたのビューコントローラの行為を持っていることです。オプションのfunc textFieldDidBeginEditing(_ textField:UITextField)関数を実装したいとします。この関数では、画像を非表示にします。

3

残念ながら、あなたは、いわゆる「プレースホルダ」について話して、私はスウィフトの「話す」はありませんが、Objective-Cのバージョンを変換することが容易に可能でなければなりません...

。実際にテキストが挿入されていない場合はUITextFieldに表示されている文字列(または属性付き文字列)。

// Create a NSTextAttachment with your image 
    NSTextAttachment* placeholderImageTextAttachment = [[NSTextAttachment alloc] init]; 
    placeholderImageTextAttachment.image = [UIImage imageNamed:@"Your image name"]; 

    // Use 'bound' to adjust position and size 
    placeholderImageTextAttachment.bounds = CGRectMake(0, 0, 16, 16); 
    NSMutableAttributedString* placeholderImageString = [[NSAttributedString attributedStringWithAttachment:placeholderImageTextAttachment] mutableCopy]; 

    // Append the placeholder text 
    NSMutableAttributedString* placeholderString = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"Search", nil)]; 
    [placeholderImageString appendAttributedString:placeholderString]; 

    // set as (attributed) placeholder 
    _yourTextField.attributedPlaceholder = placeholderImageString; 
:ここに画像を表示するように

は、次のコードを使用します

関連する問題