向 UIAlertController 的消息文本插入超链接 (Xamarin.iOS)
Inserting hyperlinks to UIAlertController's message text (Xamarin.iOS)
UIAlertController 使用 UILabel 来显示文本,我读到只有 UITextViews 可以显示可点击的链接。
我会替换对话框中的标签,但是一旦我删除它,程序就会抛出一个错误,指出它的约束无法激活。
为了消除该约束,我循环浏览了 UILabel 的超级视图,但找不到它。
有人成功过吗?
UIAlertController dialog = UIAlertController.Create("", message, UIAlertControllerStyle.Alert);
dialog.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
UILabel label = (UILabel)dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[2];
UIView view = label;
do
{
view = view.Superview;
Console.WriteLine(view);
foreach (NSLayoutConstraint constraint in view.Constraints)
{
Console.WriteLine("Constraint: " + constraint.FirstItem + " --- " + constraint.FirstAttribute + " --- " + constraint.SecondItem + " --- " + constraint.SecondAttribute + " --- " + constraint.Constant);
if (constraint.FirstItem == label)
{
Console.WriteLine("FirstItem found");
view.RemoveConstraint(constraint);
}
if (constraint.SecondItem == label)
{
Console.WriteLine("SecondItem found");
view.RemoveConstraint(constraint);
}
}
} while (view != dialog.View);
//label.RemoveFromSuperview();
好吧,您可以只更改标签的可见性,然后插入文本视图。正如 Ivan 指出的那样,最好有一个备份解决方案。
UIAlertController dialog = UIAlertController.Create("", message, UIAlertControllerStyle.Alert);
dialog.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
try
{
UILabel label = (UILabel)dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[2];
label.Hidden = true;
UITextView textView = new UITextView();
dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].InsertSubviewAbove(textView, label);
textView.TranslatesAutoresizingMaskIntoConstraints = false;
textView.LeadingAnchor.ConstraintEqualTo(label.LeadingAnchor).Active = true;
textView.TopAnchor.ConstraintEqualTo(label.TopAnchor, -8).Active = true;
textView.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0);
textView.ScrollEnabled = false;
textView.Editable = false;
textView.DataDetectorTypes = UIDataDetectorType.Link;
textView.Selectable = true;
textView.Font = UIFont.SystemFontOfSize(13);
textView.Text = message;
}
catch
{
}
context.PresentViewController(dialog, true, null);
UIAlertController 使用 UILabel 来显示文本,我读到只有 UITextViews 可以显示可点击的链接。 我会替换对话框中的标签,但是一旦我删除它,程序就会抛出一个错误,指出它的约束无法激活。 为了消除该约束,我循环浏览了 UILabel 的超级视图,但找不到它。 有人成功过吗?
UIAlertController dialog = UIAlertController.Create("", message, UIAlertControllerStyle.Alert);
dialog.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
UILabel label = (UILabel)dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[2];
UIView view = label;
do
{
view = view.Superview;
Console.WriteLine(view);
foreach (NSLayoutConstraint constraint in view.Constraints)
{
Console.WriteLine("Constraint: " + constraint.FirstItem + " --- " + constraint.FirstAttribute + " --- " + constraint.SecondItem + " --- " + constraint.SecondAttribute + " --- " + constraint.Constant);
if (constraint.FirstItem == label)
{
Console.WriteLine("FirstItem found");
view.RemoveConstraint(constraint);
}
if (constraint.SecondItem == label)
{
Console.WriteLine("SecondItem found");
view.RemoveConstraint(constraint);
}
}
} while (view != dialog.View);
//label.RemoveFromSuperview();
好吧,您可以只更改标签的可见性,然后插入文本视图。正如 Ivan 指出的那样,最好有一个备份解决方案。
UIAlertController dialog = UIAlertController.Create("", message, UIAlertControllerStyle.Alert);
dialog.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
try
{
UILabel label = (UILabel)dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[2];
label.Hidden = true;
UITextView textView = new UITextView();
dialog.View.Subviews[0].Subviews[0].Subviews[0].Subviews[0].Subviews[0].InsertSubviewAbove(textView, label);
textView.TranslatesAutoresizingMaskIntoConstraints = false;
textView.LeadingAnchor.ConstraintEqualTo(label.LeadingAnchor).Active = true;
textView.TopAnchor.ConstraintEqualTo(label.TopAnchor, -8).Active = true;
textView.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0);
textView.ScrollEnabled = false;
textView.Editable = false;
textView.DataDetectorTypes = UIDataDetectorType.Link;
textView.Selectable = true;
textView.Font = UIFont.SystemFontOfSize(13);
textView.Text = message;
}
catch
{
}
context.PresentViewController(dialog, true, null);