Xamarin Forms MacOS 中的条目未垂直居中
Entry not vertically centered in Xamarin Forms MacOS
在尝试使用居中条目时偶然发现此错误 https://github.com/xamarin/Xamarin.Forms/issues/10789。
尝试通过自定义渲染器在本地解决此问题,但在此过程中遇到了一些麻烦。这是我目前拥有的:
[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);
}
}
}
所以我关注了这个问题:
Set text vertical center in NSTextField
但是有一些问题。第一个问题是我无法设置 titleRect.Size.Height = res2;
,因为它抱怨“无法修改 ´CGRECT.Size´ 的 return 值,因为它不是变量。第二个问题是我如何将这些新添加的值分配给控件本身,以便我可以测试它并查看它是否有效。
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.
关于这个,就是说你不能直接赋值Height
。
我们需要设置Size的总参数如下:
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.
根据共享讨论,我们还可以自定义一个 NSTextFieldCell
,然后设置为 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);
}
}
效果:
在尝试使用居中条目时偶然发现此错误 https://github.com/xamarin/Xamarin.Forms/issues/10789。
尝试通过自定义渲染器在本地解决此问题,但在此过程中遇到了一些麻烦。这是我目前拥有的:
[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);
}
}
}
所以我关注了这个问题: Set text vertical center in NSTextField
但是有一些问题。第一个问题是我无法设置 titleRect.Size.Height = res2;
,因为它抱怨“无法修改 ´CGRECT.Size´ 的 return 值,因为它不是变量。第二个问题是我如何将这些新添加的值分配给控件本身,以便我可以测试它并查看它是否有效。
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.
关于这个,就是说你不能直接赋值Height
。
我们需要设置Size的总参数如下:
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.
根据共享讨论,我们还可以自定义一个 NSTextFieldCell
,然后设置为 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);
}
}
效果: