Reputation: 401
Stumbled upon this bug https://github.com/xamarin/Xamarin.Forms/issues/10789 when trying to work with a centered entry.
Tried to solve it locally by doing a custom renderer but having some troubles along the way. This is what i currently have:
[assembly: Xamarin.Forms.ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace Project.MacOS.Renderers
{
public class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
var element = e.NewElement as Entry;
if (Control != null && element != null)
{
Control.BackgroundColor = NSColor.Clear;
Control.Bordered = false;
var stringHeight = Control.AttributedStringValue.Size.Height;
var titleRect = Bounds;
var oldOriginY = this.Bounds.Y;
var res = titleRect.Y + (Bounds.Size.Height - stringHeight) / 2.0;
titleRect.Y = (System.nfloat)res;
var res2 = titleRect.Size.Height - (titleRect.Y - oldOriginY);
titleRect.Size.Height = res2;
}
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
}
}
}
So I followed this question: Set text vertical center in NSTextField
But having a few issues. First issue is that I am unable to set the titleRect.Size.Height = res2;
as it is complaining about "Cannot modify the return value of ´CGRECT.Size´ because it is not a variable. Second issue is how I assign these newly added values to the control itself so that i can test it and see if it worked.
Upvotes: 1
Views: 206
Reputation: 12723
First issue is that I am unable to set the titleRect.Size.Height = res2;as it is complaining about "Cannot modify the return value of ´CGRECT.Size´ because it is not a variable.
About this, that means you can not assign Height
directly.
We need to set the total paramater of Size as follows:
titleRect = new CoreGraphics.CGRect(titleRect.X, titleRect.Y, titleRect.Width, res2 );
Second issue is how I assign these newly added values to the control itself so that i can test it and see if it worked.
From shared discussion, we also can custom a NSTextFieldCell
and then set for NSTextField
.
public class CustomEntryRenderer :EntryRenderer
{
public CustomEntryRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
var element = e.NewElement as Entry;
if (Control != null & element != null)
{
Control.BackgroundColor = NSColor.Clear;
VerticallyCenteredTextfieldCell verticallyCenteredTextfieldCell = new VerticallyCenteredTextfieldCell(Control);
Control.Cell = verticallyCenteredTextfieldCell;
Control.StringValue = element.Text;
Control.Bordered = true;
}
}
}
public class VerticallyCenteredTextfieldCell : NSTextFieldCell
{
private NSTextField control;
public VerticallyCenteredTextfieldCell(NSTextField control)
{
this.control = control;
}
public override CoreGraphics.CGRect TitleRectForBounds(CoreGraphics.CGRect theRect)
{
var stringheight = control.AttributedStringValue.Size.Height;
CoreGraphics.CGRect titleRect = base.TitleRectForBounds(theRect);
var oldOriginY = control.Frame.Y;
titleRect.Y = control.Frame.Y + (control.Frame.Size.Height - stringheight) / 2;
var height = titleRect.Size.Height - (titleRect.Y - oldOriginY);
titleRect = new CoreGraphics.CGRect(titleRect.X, titleRect.Y, titleRect.Width, height);
return titleRect;
}
public override void DrawInteriorWithFrame(CoreGraphics.CGRect cellFrame, NSView inView)
{
base.DrawInteriorWithFrame(TitleRectForBounds(cellFrame), inView);
}
}
The effect:
Upvotes: 2