Skip to content

Allowed to open links in webview and external browser. #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/HtmlLabel/HtmlLabel.Forms.Plugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">
<Compile Include="iOS\*.cs" />
</ItemGroup>
<ItemGroup>
<Compile Update="Shared\LinkEventArgs.cs">
<SubType></SubType>
</Compile>
</ItemGroup>
</Project>
37 changes: 33 additions & 4 deletions src/HtmlLabel/Shared/HtmlLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,39 @@ public int AndroidListIndent
set { SetValue(AndroidListIndentProperty, value); }
}

/// <summary>
/// Fires before the open URL request is done.
/// </summary>
public event EventHandler<WebNavigatingEventArgs> Navigating;
/// <summary>
/// Identify the AndroidLegacyMode property.
/// </summary>
public static readonly BindableProperty IsExternalBrowserProperty =
BindableProperty.Create(nameof(IsExternalBrowserProperty), typeof(bool), typeof(HtmlLabel), default);

/// <summary>
/// Get or set if the Android renderer separates block-level elements with blank lines.
/// </summary>
public bool IsExternalBrowser
{
get { return (bool)GetValue(IsExternalBrowserProperty); }
set { SetValue(IsExternalBrowserProperty, value); }
}

// Declare the delegate (if using non-generic pattern).
public delegate void LinkClickEventHandler(object sender, LinkEventArgs e);

// Declare the event.
public event LinkClickEventHandler LinkClickEvent;

// Wrap the event in a protected virtual method
// to enable derived classes to raise the event.
public void RaiseLinkClickEvent(string link)
{
// Raise the event in a thread-safe manner using the ?. operator.
LinkClickEvent?.Invoke(this, new LinkEventArgs(link));
}

/// <summary>
/// Fires before the open URL request is done.
/// </summary>
public event EventHandler<WebNavigatingEventArgs> Navigating;

/// <summary>
/// Fires when the open URL request is done.
Expand Down
10 changes: 10 additions & 0 deletions src/HtmlLabel/Shared/LinkEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace LabelHtml.Forms.Plugin.Abstractions
{
public class LinkEventArgs
{
public LinkEventArgs(string link) { Link = link; }
public string Link { get; }
}
}

11 changes: 9 additions & 2 deletions src/HtmlLabel/Shared/RendererHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,15 @@ public static bool HandleUriClick(HtmlLabel label, string url)

if (uri.IsHttp())
{
uri.LaunchBrowser(label.BrowserLaunchOptions);
result = true;
if (label.IsExternalBrowser)
{
uri.LaunchBrowser(label.BrowserLaunchOptions);
}
else
{
label.RaiseLinkClickEvent(url);
}
result = true;
}
else if (uri.IsEmail())
{
Expand Down
6 changes: 3 additions & 3 deletions src/HtmlLabel/iOS/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ private void SetText(string html)
var md = new NSMutableDictionary(value);
var font = md[UIStringAttributeKey.Font] as UIFont;

if (font != null)
if (Control.Font != null)
{
md[UIStringAttributeKey.Font] = Control.Font.WithTraitsOfFont(font);
md[UIStringAttributeKey.Font] = Control.Font;
}
else
{
md[UIStringAttributeKey.Font] = Control.Font;
md[UIStringAttributeKey.Font] = Control.Font.WithTraitsOfFont(font);
}

var foregroundColor = md[UIStringAttributeKey.ForegroundColor] as UIColor;
Expand Down