【问题标题】:Xamarin Forms iOS CustomRenderer Entry PlaceHolder And Button Text Padding BottomXamarin Forms iOS CustomRenderer Entry PlaceHolder 和 Button Text Padding Bottom
【发布时间】:2020-04-22 00:43:01
【问题描述】:

这是我的第一篇文章,不要讨厌我。这种情况解释起来很复杂,但我会尽量说清楚。我有一个 xamarin 形式的项目,我正在开发 iOS 版本,我将字体从基本字体更改为 Nexa。这导致改变了“g”的大小并产生了我无法解决的问题。我有一个用于条目的自定义渲染器,但我没有在网上找到任何方法来纠正底部填充。我已经在页面的 xaml、自定义渲染器和样式上尝试了许多更改,但没有解决问题 https://github.com/xamarin/Xamarin.Forms/issues/6141 。我也读过这个,但它对我没有帮助。在这里我写我的代码来澄清情况:

风格:

<!-- Default Entries -->
    <Style TargetType="cr:BorderlessEntry">
        <Setter Property="HeightRequest" Value="45"/>
        <Setter Property="TextColor" Value="Black"/>
        <Setter Property="PlaceholderColor" Value="Gray"/>
        <Setter Property="FontFamily" Value="{StaticResource NormalFont}"/>
    </Style>
<!-- Default Frame -->
    <Style x:Key="FrameEntry" TargetType="Frame">
        <Setter Property="BorderColor" Value="{StaticResource LightBlue}"/>
        <Setter Property="CornerRadius" Value="25"/>
        <Setter Property="Padding" Value="10,3,10,3"/>
        <Setter Property="Margin" Value="0"/>
    </Style>

Xaml 页面:

<Frame Style="{StaticResource FrameEntry}">
    <cr:BorderlessEntry Placeholder="{x:Static res:AppResources.PLACE}" Text="{Binding Place}"/>
</Frame>

CS 自定义渲染器:

   public class BorderlessEntryRenderer : EntryRenderer
    {
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control == null)
                return;

            Control.Layer.BorderWidth = 0;
            Control.BorderStyle = UITextBorderStyle.None;
            //Control.VerticalAlignment = UIControlContentVerticalAlignment.Fill;
            //Control.LeftView = new UIView(new CGRect(0, 0, 0, 0));
            //Control.LeftViewMode = UITextFieldViewMode.Always;
            //Control.RightView = new UIView(new CGRect(0, 0, 0, 0));
            //Control.RightViewMode = UITextFieldViewMode.Always;
        }
    }

很明显,如果我尝试在条目中写 g 它不会被删减!

我还发现按钮内的文本也出现了同样的问题,您可以在链接的测试项目中找到这两个示例。

这里是重新创建问题的测试项目的链接(仅在 iOS 上)

https://drive.google.com/open?id=1J2FAbh_jjJ8JnURh_-C7jBXbr02xBBGD

【问题讨论】:

  • 您的描述含糊不清。所以您能否分享一个包含该问题的示例,以便我可以测试它。
  • 更新示例链接
  • 权限问题无法下载,你可以上传到github。
  • 现在我修复了下载,我也修复了按钮

标签: c# xaml xamarin mvvm xamarin.ios


【解决方案1】:

在渲染器的OnElementPropertyChanged集合中

 var unconstrainedSize = new CGSize(99999999999, 99999999999);
 Control.HeightAnchor.ConstraintEqualTo(unconstrainedSize.Height).Active = true;

您也可以使用填充:

public override void LayoutSubviews()
{

   base.LayoutSubviews();

            CGRect frame = this.top.Frame; // save frame
            frame.Height = this.Bounds.Size.Height + 20f; //modify frame
            //Frame.Offset(0, 20); dunno can play with this too
            this.Frame = frame; // set modifyed
}

【讨论】:

  • 第一个不起作用。我应该把第二个放在哪里?
  • 在你的 ios 入口渲染器中
猜你喜欢
  • 2017-10-07
  • 1970-01-01
  • 2017-01-21
  • 2018-10-18
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多