I have follow below example to copy Label selected text using Clipboard but its copying full content not selected text. Is there a way to copy part of the text and paste in a label in xamarin.
Please check Below link. I think it's solve your problem
http://blog.rawatchetan.com/xamarin-forms-custom-label-to-copy-text/
You could use custom renderer.
I follow the steps in the blog. https://medium.com/@HeikkiDev/selectable-label-on-xamarin-forms-9b050267bf8e
MyLabel.cs
public class MyLabel : View
{
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(MyLabel), default(string));
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(MyLabel), Color.Black);
public static readonly BindableProperty FontAttributesProperty = BindableProperty.Create(nameof(FontAttributes), typeof(FontAttributes), typeof(MyLabel), FontAttributes.None);
public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(double), typeof(MyLabel), -1.0);
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public Color TextColor
{
get { return (Color)GetValue(TextColorProperty); }
set { SetValue(TextColorProperty, value); }
}
public FontAttributes FontAttributes
{
get { return (FontAttributes)GetValue(FontAttributesProperty); }
set { SetValue(FontAttributesProperty, value); }
}
[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
}
MyLabelRenderer.cs
[assembly: ExportRenderer(typeof(MyLabel), typeof(MyLabelRenderer))]
namespace LabelDemo.Droid
{
public class MyLabelRenderer : ViewRenderer<MyLabel, TextView>
{
TextView textView;
public MyLabelRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<MyLabel> e)
{
base.OnElementChanged(e);
var label = (MyLabel)Element;
if (label == null)
return;
if (Control == null)
{
textView = new TextView(this.Context);
}
textView.Enabled = true;
textView.Focusable = true;
textView.LongClickable = true;
textView.SetTextIsSelectable(true);
// Initial properties Set
textView.Text = label.Text;
textView.SetTextColor(label.TextColor.ToAndroid());
switch (label.FontAttributes)
{
case FontAttributes.None:
textView.SetTypeface(null, Android.Graphics.TypefaceStyle.Normal);
break;
case FontAttributes.Bold:
textView.SetTypeface(null, Android.Graphics.TypefaceStyle.Bold);
break;
case FontAttributes.Italic:
textView.SetTypeface(null, Android.Graphics.TypefaceStyle.Italic);
break;
default:
textView.SetTypeface(null, Android.Graphics.TypefaceStyle.Normal);
break;
}
textView.TextSize = (float)label.FontSize;
SetNativeControl(textView);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == MyLabel.TextProperty.PropertyName)
{
if (Control != null && Element != null && !string.IsNullOrWhiteSpace(Element.Text))
{
textView.Text = Element.Text;
}
}
else if (e.PropertyName == MyLabel.TextColorProperty.PropertyName)
{
if (Control != null && Element != null)
{
textView.SetTextColor(Element.TextColor.ToAndroid());
}
}
else if (e.PropertyName == MyLabel.FontAttributesProperty.PropertyName
|| e.PropertyName == MyLabel.FontSizeProperty.PropertyName)
{
if (Control != null && Element != null)
{
switch (Element.FontAttributes)
{
case FontAttributes.None:
textView.SetTypeface(null, Android.Graphics.TypefaceStyle.Normal);
break;
case FontAttributes.Bold:
textView.SetTypeface(null, Android.Graphics.TypefaceStyle.Bold);
break;
case FontAttributes.Italic:
textView.SetTypeface(null, Android.Graphics.TypefaceStyle.Italic);
break;
default:
textView.SetTypeface(null, Android.Graphics.TypefaceStyle.Normal);
break;
}
textView.TextSize = (float)Element.FontSize;
}
}
}
}
}
MainPage.xml
<StackLayout>
<local:MyLabel
FontAttributes="Bold"
HorizontalOptions="CenterAndExpand"
Text="This text can be selected!"
VerticalOptions="CenterAndExpand" />
</StackLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.