IOS 中需要为自定义字体设置导航标题的行高

Need to set line height of navigation title for custom font in IOS

我只是在所有文本区域设置了自定义字体,但创建的字体比 space 稍长。文字切割的上侧。我将它固定在标签项目上,并将 lineheight 设置为 1.2。但是我不能在我的导航标题和输入框上这样做。

这是我的导航自定义渲染器:

   public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
        UINavigationBar.Appearance.ShadowImage = new UIImage();
        UINavigationBar.Appearance.BackgroundColor = UIColor.Clear;
        UINavigationBar.Appearance.TintColor = UIColor.White;
        UINavigationBar.Appearance.BarTintColor = UIColor.Clear;
        UINavigationBar.Appearance.Translucent = true;

        UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes()
        {
            Font = UIFont.FromName("CooperHewitt-Bold", 20),
            TextColor = UIColor.White
        });
    }

我也需要在入口处设置行高。

您可以创建自定义导航栏并根据需要设置其样式。

in Customrenderer

public override void ViewWillAppear(bool animated)
{
  base.ViewWillAppear(animated);

  NavigationController.NavigationBar.Hidden = true;


  double height = IsiphoneX();

  UIView backView = new UIView()
  {
    BackgroundColor = UIColor.White,
    Frame = new CGRect(0,20,UIScreen.MainScreen.Bounds.Width, height),
  };


  UIButton backBtn = new UIButton() {

     Frame = new CGRect(20, height-44, 40, 44),
     Font = UIFont.SystemFontOfSize(18),

  } ;

  backBtn.SetTitle("Back", UIControlState.Normal);
  backBtn.SetTitleColor(UIColor.Blue, UIControlState.Normal);
  backBtn.AddTarget(this,new Selector("GoBack"),UIControlEvent.TouchUpInside);

  UILabel titleLabel = new UILabel() 
     {
      Frame=new CGRect(UIScreen.MainScreen.Bounds.Width/2-75, 0,150, height),
      Font = UIFont.FromName("CooperHewitt-Bold", 20),
      Text = "xxx",
      TextColor = UIColor.Black,
      Lines = 0,
     };

     UILabel line = new UILabel() {

         Frame = new CGRect(0, height, UIScreen.MainScreen.Bounds.Width, 0.5),
         BackgroundColor = UIColor.Black,

     };

     backView.AddSubview(backBtn);
     backView.AddSubview(titleLabel);
     backView.AddSubview(line);

     View.AddSubview(backView);
}


double IsiphoneX()
{

  double height = 44;

  if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
   {
     if(UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom > 0.0)
       {
          height = 64;
       }
   }

   return height;
}

[Export("GoBack")]
void GoBack()
{
  NavigationController.PopViewController(true);
}

public override void ViewWillDisappear(bool animated)
{
  base.ViewWillDisappear(animated);

  NavigationController.NavigationBar.Hidden = false;
}

您可以根据需要设置属性 title , backButton 和 navigationBar (例如text , color ,BackgroundColor ,font )

For entry , you can also set height in CustomRenderer

using UIKit;
using xxx.iOS;


using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using CoreGraphics;

[assembly: ExportRenderer(typeof(Entry), typeof(MyEnterRenderer))]
namespace xxx.iOS
{
    public  class MyEnterRenderer:EntryRenderer
    {

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if(Control!=null)
            {
                SetNativeControl(new CustomTextField());
            }

        }

    }

    public class CustomTextField:UITextField
    {


        public CustomTextField()
        {
            Font = UIFont.SystemFontOfSize(20,2);
        }

    }

}

我刚刚通过创建一个新视图并将其设置为 TitleView 解决了我的问题。

 <ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
                   //.... 
 x:Class="MyApp.Views.CustomNavTitle">
    <StackLayout x:Name="stack" VerticalOptions="Center" HorizontalOptions="FillAndExpand" Margin="0,0,25,0">
        <Label Style="{StaticResource NavigationBarTitle}" Text="{Binding .}"/>
    </StackLayout>
</ContentView>

如果它是 IOS 设备,则在我的内容页面构造函数中设置它。

if (Device.RuntimePlatform == Device.iOS)
            NavigationPage.SetTitleView(this, new CustomNavTitle() { BindingContext = Title });

然后我设置了 "LineHeight"(作为 1.2)道具并设置了我想要的自定义字体系列,以 NavigationBarTitle 样式的标签。