question

TefiloCerda-4213 avatar image
0 Votes"
TefiloCerda-4213 asked ·

Selecteable Custom Label Renderer not fixing height iOS

I just create a custom selectable label but is not fixing the height content if I change the value. Code: Shared code: public class SelectableLabel : View { private static Color DefaultTextColor { get { return Device.RuntimePlatform == Device.iOS ? Color.Black : Color.Gray; } } public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(SelectableLabel), default(string)); public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(SelectableLabel), DefaultTextColor); public static readonly BindableProperty FontAttributesProperty = BindableProperty.Create(nameof(FontAttributes), typeof(FontAttributes), typeof(SelectableLabel), FontAttributes.None); public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(double), typeof(SelectableLabel), Device.GetNamedSize(NamedSize.Default, typeof(Label)), BindingMode.OneWay); 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); } } } iOS code: public class SelectableLabelRenderer : ViewRenderer<SelectableLabel, UITextView> { UITextView uiTextView; protected override void OnElementChanged(ElementChangedEventArgs<SelectableLabel> e) { base.OnElementChanged(e); var label = (SelectableLabel)Element; if (label == null) return; if (Control == null) { uiTextView = new UITextView(); } uiTextView.Selectable = true; uiTextView.Editable = false; uiTextView.ScrollEnabled = false; uiTextView.TextContainerInset = UIEdgeInsets.Zero; uiTextView.TextContainer.LineFragmentPadding = 0; uiTextView.BackgroundColor = UIColor.Clear; // Initial properties Set uiTextView.Text = label.Text; uiTextView.TextColor = label.TextColor.ToUIColor(); switch (label.FontAttributes) { case FontAttributes.None: uiTextView.Font = UIFont.SystemFontOfSize(new nfloat(label.FontSize)); break; case FontAttributes.Bold: uiTextView.Font = UIFont.BoldSystemFontOfSize(new nfloat(label.FontSize)); break; case FontAttributes.Italic: uiTextView.Font = UIFont.ItalicSystemFontOfSize(new nfloat(label.FontSize)); break; default: uiTextView.Font = UIFont.BoldSystemFontOfSize(new nfloat(label.FontSize)); break; } SetNativeControl(uiTextView); } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (e.PropertyName == "Text" && Control != null) { Control.Text = ((SelectableLabel)sender).Text; } } }

dotnet-xamarinforms
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,TefiloCerda-4213. I created a basic demo to test the function, the 'SelectableLabel' could be shown well as below. Could you post more details about the issue?

<local:SelectableLabel TextColor="Red" FontAttributes="None" FontSize="18" Text="testing for custom label" HorizontalOptions="CenterAndExpand"/>

Here is the screenshot:
77055-image.png



0 Votes 0 ·
image.png (10.3 KiB)

0 Answers