How to add a touchable Url link to a url in a UIAlertView's message?

天大地大妈咪最大 提交于 2019-12-05 00:10:40
sergio

The only way I see to implement what you are trying to is through a custom alert view.

There are several approaches that you can take. One is subclassing UIAlertView and here you can find a short tutorial: Subclass UIAlertView. In your subclass you could then build the alert any way you like to implement the touch-enabled text. Have a look at this tutorial for a way to do it.

I ran into this problem today, I needed to have clickable phone numbers and addresses in my alert view and was stumped for quite awhile as custom alert views are out of the question.

After some research it seems that you can add a textview to an alertview which seemed to solve my problem. Here is my approach that allows for dynamically scaling alertviews (note: using C# with Xamarin):

// create text view with variable size message
UITextView alertTextView = new UITextView();
alertTextView.Text = someLongStringWithUrlData;

// enable links data inside textview and customize textview
alertTextView.DataDetectorTypes = UIDataDetectorType.All;
alertTextView.ScrollEnabled = false; // is necessary 
alertTextView.BackgroundColor = UIColor.FromRGB(243, 243, 243); // close to alertview default color
alertTextView.Editable = false;

// create UIAlertView 
UIAlertView Alert = new UIAlertView("Quick Info", "", null, "Cancel", "OK");
Alert.SetValueForKey(alertTextView, (Foundation.NSString)"accessoryView");

// IMPORTANT/OPTIONAL need to set frame of textview after adding to subview
// this will size the text view appropriately so that all data is shown (also resizes alertview
alertTextView.Frame = new CoreGraphics.CGRect(owner.View.Center, alertTextView.ContentSize);
Alert.Show();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!