2017-07-24 60 views
0

我知道这个问题已被问了很多,但我已经尝试了各种解决方案,似乎无法找到一个工程。我的故事板上有一个标题为“消息”。点击按钮时,标签中会出现不同的文字。我需要填写这个标签。C#Xamarin标签填充的iOS应用

我看: Adding space/padding to a UILabelUILabel text margin, 和Resizing a UILabel to accommodate insets

也不知道在我的ViewController.cs中放置代码的位置。我把它放在public partial class ViewController : UIViewController下,但是会出错。

编辑2:

好,首先我做一个UITextView,但不能得到垂直对齐工作,所以又回到标签。这是我有:

public partial class ViewController : UIViewController 
{ 

    public partial class PaddedUILabel : UILabel 
    { 
     private UIEdgeInsets EdgeInsets { get; set; } 

     public PaddedUILabel() 
     { 
      EdgeInsets = new UIEdgeInsets(0, 10, 0, 10); 
     } 
     public override void DrawText(CoreGraphics.CGRect rect) 
     { 
      base.DrawText(EdgeInsets.InsetRect(rect)); 
     } 
    } 

public override void ViewDidLoad() 
    { 

     base.ViewDidLoad(); 

     Message.Layer.BorderWidth = 1; 
     Message.BackgroundColor = UIColor.FromWhiteAlpha(1, 0.88f); 
     PaddedUILabel _paddedUILabel = Message as PaddedUILabel; 

我还没有得到任何填充。

+0

您是否使用Xamarin iOS或Xamarin形式? –

+0

Xamarin iOS。下面的答案使用'UITextView'工作,但现在文本不是垂直对齐的。 –

+0

你在代码中使用SizeToFit吗?如果是的话,请删除它并尝试 –

回答

0

您可以使用一个UITextView并直接提供插图(如本link提到的),你可以在viewDidLoad中或viewWillAppear中做什么,或者你可以subclass your UILabel并覆盖DrawText method

DrawText方法有助于控制绘制标签内文本的矩形。我建议你付出一些价值来开始使用它。

喜欢的东西:

public partial class PaddedUILabel : UILabel 
{ 
    //Override draw text here 

} 

如果您正在使用Xamarin iOS原生,给你的标签(例如:myLabel)从故事板自定义类和检索标签为:

PaddedUILabel _paddedUILabel = this.myLabel as PaddedUILabel; 

对不起,我会给你整个代码,但到目前为止,无法访问Mac环境。需要帮助请叫我。

对于TextView中的垂直对齐,请按照link

干杯

+0

我不知道如何做到这一点,所以我使用了UITextView的其他建议,这工作,但现在我不知道如何让文本垂直对齐? –

+0

更新了我的答案 – Singhal2

0

我尝试了许多解决方案,这里讨论,但实际出未来不是我想要的,所以我做它这样:

if (this.Padding != default(Thickness)) 
       { 
        Device.BeginInvokeOnMainThread(() => 
         { 

          if (Parent is Grid) 
          { 
           var parentAsGrid = Parent as Grid; 
           var index = parentAsGrid.Children.IndexOf(this); 
           parentAsGrid.Children.Remove(this); 
           Grid marginGrid = new Grid() { BackgroundColor = this.BackgroundColor, HorizontalOptions = this.HorizontalOptions, VerticalOptions = this.VerticalOptions }; 


           var lbl = new Label() { Text = this.Text, TextColor = this.TextColor, BackgroundColor = this.BackgroundColor, HorizontalOptions = this.HorizontalOptions, VerticalOptions = this.VerticalOptions, FontSize = this.FontSize }; 
           lbl.Margin = this.Padding; 
           if (!parentAsGrid.Children.Contains(this)) 
           { 
            marginGrid.Children.Add(lbl); 
            parentAsGrid.Children.Insert(index, marginGrid); 
           } 

          } 
          if (Parent is StackLayout) 
          { 
           var parentAsGrid = Parent as StackLayout; 
           var index = parentAsGrid.Children.IndexOf(this); 
           parentAsGrid.Children.Remove(this); 
           Grid marginGrid = new Grid() { BackgroundColor = this.BackgroundColor, HorizontalOptions = this.HorizontalOptions, VerticalOptions = this.VerticalOptions }; 


           var lbl = new Label() { Text = this.Text, TextColor = this.TextColor, BackgroundColor = this.BackgroundColor, HorizontalOptions = this.HorizontalOptions, VerticalOptions = this.VerticalOptions, FontSize = this.FontSize }; 
           lbl.Margin = this.Padding; 
           if (!parentAsGrid.Children.Contains(this)) 
           { 
            marginGrid.Children.Add(lbl); 
            parentAsGrid.Children.Insert(index, marginGrid); 
           } 

          } 



         });