I have a cross platform app in which I have created a custom renderer to customize hyperlink label color on ios.
However, I am unable to display long text (including two links ) on separate (two/multiple) lines.
I have tried line breaks (\n , \r) but they are not not working.
Any idea how can I fix this issue?
[assembly: ExportRenderer(typeof(HyperLinkLabel), typeof(HyperLinkLabelRenderer))]
namespace MyApp.iOS
{
public class HyperLinkLabelRenderer : ViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
var view = (HyperLinkLabel)Element;
if (view == null) return;
UITextView uilabelleftside = new UITextView(new CGRect(0, 0, view.Width, view.Height));
uilabelleftside.AttributedText = view.Text.AsAttributedString(NSDocumentType.HTML);
uilabelleftside.Font = UIFont.SystemFontOfSize((float)view.FontSize);
uilabelleftside.Editable = false;
uilabelleftside.DataDetectorTypes = UIDataDetectorType.Link;
uilabelleftside.BackgroundColor = UIColor.Clear;
uilabelleftside.TextColor = UIColor.White;
uilabelleftside.TintColor = UIColor.Red;
SetNativeControl(uilabelleftside);
}
}
public static class NSStringExtensions
{
public static NSAttributedString AsAttributedString(this string input, NSDocumentType docType)
{
NSError err = null;
var rtn = new NSAttributedString(input, new NSAttributedStringDocumentAttributes { DocumentType = docType }, ref err);
if (err == null)
{
return rtn;
}
throw new NSErrorException(err);
}
}
}
Usage:
HyperLinkLabel DisplayThisLabel = new HyperLinkLabel
{
Margin = new Thickness(4, 0, 4, 0),
TextColor = AppColor.White,
HorizontalTextAlignment = TextAlignment.Start,
HorizontalOptions = LayoutOptions.FillAndExpand,
LineBreakMode = LineBreakMode.WordWrap,
Text = “Show text here including links from AppResources file”,
VerticalOptions = LayoutOptions.CenterAndExpand,
FontSize = 14,
};