![](/img/trans.png)
[英]ViewDidAppear is causing my label to dissapear (Xamarin.ios, C#)
[英]C# Xamarin Label Padding for iOS app
我知道这个问题已经问了很多,但是我尝试了各种解决方案,但似乎找不到可行的解决方案。 我的故事板上有一个标题为Messages的标签。 单击按钮时,标签中将显示不同的文本。 我只需要粘贴这个标签。
我看过: 在UILabel中添加空格/填充 , UILabel文本边距 ,以及调整UILabel的大小以适应inset 。
也不确定在我的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;
我仍然没有得到任何填充。
您可以使用UITextView并直接提供inset(如本链接中所述 ),您可以在ViewDidLoad或ViewWillAppear中进行操作,也可以将UILabel子类化并重写DrawText方法 。
DrawText方法有助于处理在其中绘制标签内文本的矩形。 我建议您花一些钱来开始使用它。
就像是:
public partial class PaddedUILabel : UILabel
{
//Override draw text here
}
如果您使用的是Xamarin iOS Native,请从故事板上为标签(例如:myLabel)提供自定义类,然后将标签检索为:
PaddedUILabel _paddedUILabel = this.myLabel as PaddedUILabel;
很抱歉,我会给您完整的代码,但是到目前为止,您无法访问Mac环境。 需要帮助请叫我。
对于TextView中的垂直对齐,请点击链接 。
干杯
我尝试了这里讨论的许多解决方案,但实际结果并不是我想要的,所以我做到了:
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);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.