插入超链接至UIAlertController的消息文本(Xamarin.iOS)

问题描述 投票:0回答:1

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();
xamarin.ios dialog uialertcontroller
1个回答
0
投票

嗯,您只需更改标签的可见性,然后插入textview。正如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);
© www.soinside.com 2019 - 2024. All rights reserved.