2017-03-10 17 views
0

UILabelに画像とテキストをつけて作っています。私は以下のように多くのことを試しています。このコードUILabel画像とテキストがiOS(Xamarin)

var textAttchment = new NSTextAttachment(); 
textAttchment.Image = UIImage.FromBundle("location_grey_icon"); 
var attachmentString = NSAttributedString.FromAttachment(textAttchment); 
var attributedString = new NSMutableAttributedString(lbl_inr.Text.ToString()); 
this.lbl_inr.AttributedText = attributedString; 

を使用して

しかし、それだけで、テキストではない画像が表示されます

は1を試してみてください。

は2を試してみてください。このコード

var attchment = new NSTextAttachment(); 
attchment.Image = UIImage.FromBundle("location_grey_icon"); 
attchment.Bounds = new CGRect(0, -1, 13, 13); 
var intiliazeText = this.text_inr.AttributedText; 
var newText = new NSMutableAttributedString(intiliazeText); 
newText.Append(NSAttributedString.CreateFrom(attchment)); 
this.text_inr.AttributedText = newText; 

を使用して

それは画像とテキストの両方を表示しています。

出力:私が欲しいもの

enter image description here

が、私はテキストので、第1の画像と[テキストの前に画像を設定したいと私の要件があります。

助けてください。

答えて

2

要件は非常に限定されています。これを実行する最適な方法は、カスタムコントロールを使用することです。

のUIView +ラベル+ UIImageView

とそこに、あなたのニーズに、あなたの限界を設定することができます。デザイナーはこれを簡単な作業です。

または、UITextFieldで枠線を削除して無効にすることができます。次に、UIImageViewをRightView(または、あなたが望むようにLeftView)として追加します。

var textEdit = new UITextField (new CGRect (15, 100, 200, 30)); 
textEdit.Enabled = false; 

textEdit.Text = "Hello from Xamarin"; 
var xamImageView = new UIImageView (new CGRect (0,0, 25, 25)); 
xamImageView.Image = UIImage.FromBundle ("xamagon"); 
textEdit.RightView = xamImageView; 
textEdit.TextAlignment = UITextAlignment.Right; 
textEdit.RightViewMode = UITextFieldViewMode.Always; 
textEdit.BorderStyle = UITextBorderStyle.None; 

結果:

results

は、この情報がお役に立てば幸い!

+0

を感謝しかし、私はとにかく1 'UILabel'が、おかげでこれをやりたいです。 – Ironman

+0

私の答えを見る私は1つの 'UILabel'を使ってそれを実行しています。 – Ironman

+1

@アイロンマンあなたが言ったとき "私の要件はテキストの前にイメージを設定して、最初のイメージとテキストを設定したいということです。"私は別の何かを理解した。あなたは道を見つけました。 – apineda

1

コードの下に使用して、私は

var attchment = new NSTextAttachment(); 
attchment.Image = UIImage.FromBundle("location_grey_icon"); 
attchment.Bounds = new CGRect(0, -2, 14, 14); 
var newText = new NSMutableAttributedString(); 
newText.Append(NSAttributedString.CreateFrom(attchment)); 
NSAttributedString s = new NSAttributedString(item.Total.ToString()); 
newText.Append(s); 
this.lbl_inr.AttributedText = newText; 

出力管理:

enter image description here

関連する問題