我正在使用Xamarin.TTTAttributedLabel在 xamarin.ios 应用程序中解析 HTML。大多数时候,每当我点击链接时,它都会冻结应用程序。有时它有效,有时则无效。我在UIView --> ScrollView中使用Label。我的滚动视图没有任何点击手势。这不会发生在模拟器上。仅在物理设备上。
View.AddSubview (scrollView)
TTTAttributedLabel lbl = new TTTAttributedLabel();
lbl.SetText(attributedString, 15f);
lbl.Lines = 0;
lbl.EnabledTextCheckingTypes = NSTextCheckingType.Link | NSTextCheckingType.PhoneNumber;
lbl.UserInteractionEnabled = true;
lbl.Delegate = new TTTHTMLLabelDelegate(this);
lbl.Frame = new CGRect (15, 0, Common.screenWidth - 30, 20);
lbl.SizeToFit();
view2.AddSubview(lbl);
scrollView.AddSubview(view2);
//Delegate code
public override void DidSelectLinkWithURL(TTTAttributedLabel label, NSUrl url)
{
if (url != null)
{
var PageUrl = url.ToString();
if (!string.IsNullOrWhiteSpace(PageUrl))
{
if (PageUrl.StartsWith("mailto"))
{
//code to open email app
}
else if (PageUrl.StartsWith("tel"))
{
//code to open phone app
}
else
{
//for <a> links
UIApplication.SharedApplication.OpenUrl(url)
}
}
}
}
更新了示例代码。我还观察到一件事,DidSelectLinkWithURL
当应用程序冻结时不会被调用。