Not able to customize entry with underline using Customentryhandler with .NET MAUI on iOS

159 Views Asked by At

Not able to customize entry with underline using Customentryhandler with .NET MAUI on iOS targeting .NET 8. the values with view.Width and view.Height are showing as -1 but if I hardcode the values, I'm able to see the underline. Please help me to get the width and height or any other way to resolve the issue.

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("MyEntry", (handler, view) => {
#if __IOS__
        handler.PlatformView.BorderStyle = UITextBorderStyle.None;
        var borderLayer = new CALayer();
        borderLayer.MasksToBounds = true;
        borderLayer.Frame = new CoreGraphics.CGRect(0, view.Height-1, view.Width, 1);
        borderLayer.BorderColor = view.TextColor.ToCGColor();
        borderLayer.BorderWidth = 1.0f;
        handler.PlatformView.Layer.AddSublayer(borderLayer);
#endif
    });
1

There are 1 best solutions below

4
On

the values with view.Width and view.Height are showing as -1

This is because the entry doesn't measure and layout now. So you can't get the size such as height and width of the view. So you can put the code into the page's LayoutChildren method. Such as:

In the xaml:

<Entry x:Name="entry" TextColor="Red"/>

And in the contentpage.cs:

        protected override void LayoutChildren(double x, double y, double width, double height)
        {
            base.LayoutChildren(x, y, width, height);
#if __IOS__
            var uitextfielf = entry.Handler.PlatformView as UITextField;
            uitextfielf.BorderStyle = UITextBorderStyle.None;
            var borderLayer = new CALayer();
            borderLayer.MasksToBounds = true;
            borderLayer.Frame = new CoreGraphics.CGRect(0, entry.Height - 1, entry.Width, 1);
            borderLayer.BorderColor = entry.TextColor.ToCGColor();
            borderLayer.BorderWidth = 1.0f;
            uitextfielf.Layer.AddSublayer(borderLayer);
#endif
        }

But you need to add the code for every entry in your project.

but if I hardcode the values, I'm able to see the underline.

I have tested your code. The underline is not a really underline. It the border (borderLayer.BorderColor = view.TextColor.ToCGColor(); ) you set. So if you set the border color as borderLayer.BorderColor = Colors.Transparent.ToCGColor(); and hardcode the size of the entry, the underline will disappear. Such as:

In the xaml

<Entry HeightRequest="60" WidthRequest="200" TextColor="Red"/>

And in the App class's constructor:

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("MyEntry", (handler, view) => {
#if __IOS__
        handler.PlatformView.BorderStyle = UITextBorderStyle.None;
        var borderLayer = new CALayer();
        borderLayer.MasksToBounds = true;
        borderLayer.Frame = new CoreGraphics.CGRect(0, view.Height-1, view.Width, 1);
        borderLayer.BorderColor = Colors.Transparent.ToCGColor();
        borderLayer.BorderWidth = 1.0f;
        handler.PlatformView.Layer.AddSublayer(borderLayer);
#endif