Xamarin.Forms Embed phone # hyperlinks (0 to many) in text - c#

I have a requirement that I provide a dialog that will display some text. In this text, there may or may not be phone numbers.
I have the regex for recognizing these phone numbers, so I know where they are.
I do not know of any solution to provide a way that the user click on the phone numbers and perform an action (the action will be launch the phone app, which I already have code for).
It appears that you cannot format portions of a label or editor, nor embed hyperlinks.
If I want to preserve the text format (not break it up) do I have no option other than handle a click on the entire label?
My only other idea was to take the text, parse the phone numbers, and dynamically create multiple labels, with each phone number being it's own separate label, which I could format as bold and provide a tap gesture action. The problem with this is the formatting.
Are there any other solutions that I am not seeing?

I found a solution using WebView thanks to the comment suggestion from Gusman.
I replace the phone numbers in the text with html anchors like so
555.555.5555 - > <a href='555.555.5555'>555.555.5555></a>
I then handle the OnNavigating event, cancel the request, and instead open up the phone with the phone number obtained from the url.
WebView:
<WebView x:Name="EmergencyContactInfoWebView" HorizontalOptions="Center" HeightRequest="150" Navigating="OnNavigating">
</WebView>
OnNavigtaing event handler:
protected void OnNavigating(object sender, WebNavigatingEventArgs e)
{
e.Cancel = true; //cancel request
int separatorIdx = e.Url.LastIndexOf('/');
string phoneNumber = e.Url.Substring(separatorIdx+1); //parse phone number href from url
var phoneDialer = CrossMessaging.Current.PhoneDialer;
if (phoneDialer.CanMakePhoneCall)
{
phoneDialer.MakePhoneCall(phoneNumber); //make the call (using a xamarin forms plugin)
}
}

Related

How can I create a text box on iOS/Android and handle the input in MonoGame?

I want that people can create a new account in my MonoGame iOS and Android application. I need a text box for username, password and recovery e-mail address (if someone forgot his password).
How can I create the text box? Should I make a white rectangle sprite (Texture2D) and a spriteFont to write the text (for example the username)?
Or are there better ways to create the text box?
How can I find out if someone tapped with his finger on the blank text box? Can I use Rectangle.Intersects(TappRectangle) or is it better to do this differently?
How can I show the keyboard on iOS/Android after someone tapped on the text box so that they can write their username in the textbox?
How can I get the characters they tapped on the keyboard?
Is it possible to save/remember the text(login data) so that people don't need to write it always when they start my application? I want that the text boxes are already filled out with their login credentials when they start the application, then they would just need to tap on a login button to log in.
You can use the KeyboardInput.Show(string WindowTitle, string Prompt, string Default, bool HidePassword = false) method.
// Load saved data into the following variables:
var defaultUsername = "";
var defaultPassword = "";
var defaultEmail = "";
var username = await KeyboardInput.Show("Name", "What's your username?", defaultUsername);
var password = await KeyboardInput.Show("Password", "What's your Password?", defaultPassword, true);
var email = await KeyboardInput.Show("E-Mail", "What's your Email?", defaultEmail);
// validate and save if the values do not match the defaults above
This will show three separate input boxes, but it handles prompts and pop-up keyboard display and input.
As far as saving the data: Look at the TitleContainer class. You can use standard file I/O to read and write the data.

C# Winforms Help Text Change font

I have a little help pop-up that displays some text when the user presses a "?" label next to a drop-down to explain the different selections.
I did it using the Help.ShowPopup command since that seemed the easiest.
I was hoping there was a way to add different font properties to parts of the text or at least to the whole thing without having to go the direction of a CHM/HTML help-file.
Here is what I am trying to do:
private void helpLbl_Click(object sender, EventArgs e)
{
// for some reason, it ignores the 'parent' parameter
// and lays it out on the screen's coordinates
Point helpLocation = helpLbl.PointToScreen(Point.Empty);
helpLocation.Y += helpLbl.Height; // have it display underneath the control
Help.ShowPopup(this, // hosting form
#"<b>Fixed:</b>
Removes a fixed amount from the sale
<b>Percent Value:</b>
Removes a set percentage of the selected package from the sale
...", helpLocation);
I was hoping since there's the option to use an HTML document to display the help, I could use HTML tags to format what was being displayed, but it doesn't appear so. Any ideas?
Is there a way to do something like displaying a RichTextBox in the help pop-up?
Another possibility is generating a HTML document on-the-fly, but it asks for a "url" if I'm not supplying the text directly and I think that might be a little over-kill for the small amount I'm trying to do here.
You have two options. One is to use a WebBrowser Control. This natively accepts HTML and displays it. The problem with it is its kind of bloated just to use as a simple label.
Your second option is to simply create a RichTextLabel, simply like this:
public class RichTextLabel : RichTextBox
{
public RichTextLabel()
{
BorderStyle = BorderStyle.None;
}
}
Add this to your form and set the Rtf property to your RTF code. You will have to convert your HTML to RTF, which is easy if you got a program such as Microsoft Word, for example.

Windows Phone Webbrowser - Cannot input text on empty line in contenteditable div

I'm developing a note app in Windows Phone 8.1 and here is a bug I cannot fix.
Say you have an WebBrowser control and loaded a html page, inside which is a content-editable div.
<div id="content" contenteditable="true">
<div>Please edit content here...</div>
</div>
After running this app, I can edit content inside div.
The strange thing is, If I tap return several times, there will be several empty lines. Then I tap empty line before current caret position, and try to input text there. It turns out I cannot input text there, and text will always append to last line of the entire div.
content here...
(Wish to insert text here.)
But text goes here.
I tested it on my lumia 925 device running Windows Phone 8.1 and using Chinse Input Method.

Android Webview equivalent in Windows Phone 8 app

I have text with some HTML tags in it.
Example : <I> I Play Football and Cricket </I>
Now when i try to display this text in Textblock it displays with Italics tag <I> as well.
The data is present in the XML file and i am working on Windows Phone 8 application.
In Android we use Webview, but what do i need to use here ?
EDIT
I have tried using TextBlock with inlines:
Italic itlText = new Italic();
itlText.Inlines.Add(new Run() { Text = "This is some example text in italics" });
Inilinetext.Inlines.Add(itlText);
It works good But issue here is suppose i have a text like below.
"This is text in <I>Italics</I> here"
Now it display entire text in italics.
This depends on how much content you want to display. If you will show large amount of HTML text, use WebBrowser control for that.
In case you only have a couple of tags i, em, b or strong, then you can actually use TextBlock, but you would have to do the parsing yourself then in order to convert the HTML to the suitable XAML components.
You also have to ask yourself how many such controls you need to display on a single page. If the answer is a few, use the WebBrowser then. If you need to display lots of them, say you have a chat or a feed, then the second route would be better.
EDIT:
Use the following code:
Inilinetext.Inlines.Add(new Run() { Text = "This is text in " });
Inilinetext.Inlines.Add(new Run() { Text = "Italics", FontStyle = FontStyle.Italic });
Inilinetext.Inlines.Add(new Run() { Text = " here" });
I told you you have to parse the original string to see which parts are italic and which are not.

Text areas and hyperlinks?

I have two quick, easy questions on C# in Visual Studio. First, is there anything like the label, but for an area of text in the program? I would like to have multiple lines of text in my program, but can only seem to accomplish it with a DotNetBar label with wordwrap turned on.
Second, is there any way to have a hyperlink in the middle of the text without using a link label? If I wanted to generate text like "An update is available, please visit http://example.com to download it!", is it possible to make the link clickable without having to position a link label in the middle of the text?
You can use a LinkLabel and set its LinkArea property:
//LinkArea (start index, length)
myLinkLabel.LinkArea = new LinkArea(37, 18);
myLinkLabel.Text = "An update is available, please visit http://example.com to download it!";
The above will make the http://example.com a link whilst the rest of the text in normal.
Edit to answer comment:
There are various ways of handling the link. One way is to give the link a description (the URL) and then launch the URL using Process.Start.
myLinkLabel.LinkArea = new System.Windows.Forms.LinkArea(37, 18);
myLinkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(myLinkLabel_LinkClicked);
myLinkLabel.Text = "An update is available, please visit http://example.com to download it!";
myLinkLabel.Links[0].Description = "http://example.com";
And the event handler can read the description and launch the site:
void myLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(e.Link.Description);
}
You may try RichTextBox control.
string text = "This is the extract of text located at http://www.google.com and http://www.yahoo.com";
richTextBox1.Text = text;
richTextBox1.ReadOnly = true;
richTextBox1.LinkClicked += (sa, ea) =>
{
System.Diagnostics.Process.Start(ea.LinkText);
};
You can use the normal label and make the AutoSize property as false.
And then adjust your width and height it will wrap by it self
I assume you are using doing a windows application, and not a web application.
In C# you can create a normal textbox by dragging and dropping it onto your form, change its property to multi-line, and make it read only. Thats what I always do.
As for adding a link to the text without a linklabel. There is a way to add links to textboxes. You can check out a pretty good tutorial at http://www.codeproject.com/KB/miscctrl/LinkTextBox.aspx/

Categories