2015-02-03 2 views
1

NavigationBar에서 제목 텍스트의 커닝을 조정하려고합니다. NavigationBar 제목에 사용자 정의 UIStringAttributes을 지정하는 한까지 보았습니다. 글꼴 및 텍스트 색상을 설정하면 제대로 작동하는 것처럼 보이지만 커닝 조정을 입력하면 입력 한 값에 관계없이 아무 일도 발생하지 않습니다.제목 변경 방법 Xamarin을 사용하여 iOS에서 NavigationBar의 커닝 값

public void SetTitleFont (string fontName, float fontSize, Color foregroundColor) 
{ 
    var TitleAttr = new UIStringAttributes { 
     ForegroundColor = foregroundColor.ToUIColor(), 
     Font = UIFont.FromName (fontName, fontSize), 
     KerningAdjustment = 50 
    }; 

    this.NavigationController.NavigationBar.TitleTextAttributes = TitleAttr; 
} 

답변

3

알아 낸 것.

새로운 UILabel을 만들고, UILabel의 속성을 설정 한 다음, TitleView를 UILabel로 설정했습니다.

// We will grab the actual title of the Page further down.  
string viewTitle = string.Empty; 

UINavigationItem navItem = new UINavigationItem(); 

if (this.NavigationController != null) 
{ 
    if (this.NavigationController.VisibleViewController != null) 
    { 
     if (this.NavigationController.VisibleViewController.NavigationItem != null) 
     { 
      navItem = this.NavigationController.VisibleViewController.NavigationItem; 

      // Grab the title of the Page you are on. 
      if (navItem.Title != null) 
      { 
       viewTitle = this.NavigationController.VisibleViewController.NavigationItem.Title; 
      } 
     } 
    } 
} 

// We do not want to set an arbitrary size for the UILabel. 
// Otherwise, positioning it will be very difficult. 
// The StringSize function will set the size of the UILabel to the absolute 
// minimum size of whatever string you specify - given the correct 
// parameters (font and fontSize). 
CGSize titleSize = viewTitle.StringSize(UIFont.FromName (fontName, size)); 

// Create the new title UILabel. Make sure to set the Bounds! 
pageTitleView = new UILabel { 
    Bounds = new CGRect (0, 0, titleSize.Width, titleSize.Height), 
    BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0) 
}; 

var titleAttributes = new NSAttributedString (viewTitle, 
    new UIStringAttributes() { 
    ForegroundColor = foregroundColor.ToUIColor(), 
    Font = UIFont.FromName (fontName, size), 
    KerningAdjustment = 1.1f 
}); 

// Apply the new attributes to the UILabel and center the text. 
pageTitleView.AttributedText = titleAttributes; 
pageTitleView.TextAlignment = UITextAlignment.Center; 

// Set the TitleView to the new UILabel. 
navItem.TitleView = pageTitleView; 

이 정보가 도움이되기를 바랍니다.

관련 문제