Необходимо установить высоту строки заголовка навигации для пользовательского шрифта в IOS - PullRequest
0 голосов
/ 04 июля 2019

Я просто установил пользовательский шрифт на всю текстовую область, но шрифты создавали немного больше места на месте.Верхняя сторона вырезания текста.Я исправил это на элементах ярлыка с установленной высотой строки 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
        });
    }

Мне нужно также установить высоту строки при вводе.

Ответы [ 2 ]

1 голос
/ 04 июля 2019

Вы можете создать пользовательскую панель навигации и установить ее стиль по своему желанию.

в 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;
}

Вы можете установить свойство заголовка, backButton и navigationBar, как вам нужно (например, текст, цвет, BackgroundColor, шрифт, например)

Для ввода вы также можете установить высоту в 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);
        }

    }

}
0 голосов
/ 13 июля 2019

Я только что решил свою проблему с созданием нового представления и установил его как 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) prop и установил пользовательское семейство шрифтов, которое я хочу, чтобы метка в стиле NavigationBarTitle.

...