我一直在使用Texture(以前的AsyncDisplayKit)很长一段时间,并习惯于能够轻松地在ASTextNode中点击电话号码和链接。
然后我切换到一个旧的xamarin项目,并在那里需要相同的功能。我正在努力为这个找到正确而简单的解决方案,并发现人们建议使用UIWebView和类似的东西。
这有点棘手,所以我会尝试描述在解决这个问题时我想出的一些步骤。
答案 0 :(得分:0)
-
//something right here and get your potential links list
var attributedContacts = new NSMutableAttributedString (value as string, UIFont.SystemFontOfSize (14), UIColor.White, UIColor.Clear);
foreach (var item in numbers) {
var nsStringItem = new NSString (item);
var nsTelPromptLink = new NSString (string.Format ("telprompt://{0}", item));
var range = attributedContacts.MutableString.LocalizedStandardRangeOfString (nsStringItem);
attributedContacts.AddAttribute (new NSString ("TextLinkAttributeName"), nsTelPromptLink, range);
}
textView.AttributedText = attributedContacts;
-
var tapRecognizer = new UITapGestureRecognizer ((UITapGestureRecognizer recognizer) => {
var textView = recognizer.View as UITextView;
if (textView == null)
return;
var tappedPoint = recognizer.LocationInView (textView);
tappedPoint.X -= textView.TextContainerInset.Left;
tappedPoint.Y -= textView.TextContainerInset.Top;
var layoutManager = textView.LayoutManager;
nfloat tmp = 0;
var tappedCharacterIndex = layoutManager.CharacterIndexForPoint (tappedPoint, textView.TextContainer, ref tmp);
NSRange range = new NSRange ();
var url = textView.AttributedText.GetAttribute ("TextLinkAttributeName", (nint)tappedCharacterIndex, out range) as NSString;
if (url == null)
return;
UIApplication.SharedApplication.OpenUrl (new NSUrl (url.ToString ()));
});
ContactsTextView.AddGestureRecognizer (tapRecognizer);
所以,它是相当粗略的解决方案,应该有额外的检查,但我认为这个想法本身很清楚。它可以很容易地应用于Swift或Objective-C中的本机解决方案。