1

NavigationBar のタイトル テキストのカーニングを調整しようとしています。カスタムUIStringAttributesを 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;
}
4

1 に答える 1

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;

これが役立つことを願っています!

于 2015-02-17T19:44:20.143 に答える