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

时间:2019-07-04 01:21:26

标签: xamarin xamarin.forms xamarin.ios xamarin.forms.entry

我只是在所有文本区域上设置了自定义字体,但是字体创建的时间比放置的空间稍长。文字切割的上侧。我将其固定在将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
        });
    }

我也需要在输入时设置行高。

2 个答案:

答案 0 :(得分:1)

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

  

在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等)

  

对于输入,您还可以在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);
        }

    }

}

答案 1 :(得分:0)

我刚刚解决了问题,创建了一个新视图并将其设置为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样式标签的自定义字体系列。