gpt4 book ai didi

swift - 如何快速获取导航栏的字体大小和字体名称?

转载 作者:行者123 更新时间:2023-11-30 14:10:25 32 4
gpt4 key购买 nike

我有我的导航栏,我没有设置字体并保留所有默认值。根据我的研究,它必须在 navigationBar.titleTextAttributes 中,但我不知道如何从 titleTextAttributes 获取字体属性。

如何获取导航栏的字体大小和字体名称?

var navigationBar = UINavigationBar(frame: CGRectMake(0, 0, 400, 40));
navigationItem.title = "my Title";
let fontsize=navigationBar.titleTextAttributes.????????

更新:我想做什么?

我正在模式模式(弹出窗口)中调用 ViewController 以显示较小的对话框。该对话框有一个导航栏。通过代码创建导航栏时:

        var navigationBar = UINavigationBar();
let navigationItem = UINavigationItem()
navigationItem.title = sBookmarktitle;
// Create left and right button for navigation item
let leftButton = UIBarButtonItem(title: sBacklabel, style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")
let rightButton = UIBarButtonItem(title: saddBookmark, style: UIBarButtonItemStyle.Plain, target: self, action: "addBookmarkAction:")

// Create two buttons for the navigation item
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
navigationBar.items = [navigationItem];
self.view.addSubview(navigationBar);

并像这样调用 ViewController:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("Bookmarks") as! BookmarkViewController;// UIViewController
vc.parentviewname=callerName;

vc.modalPresentationStyle = UIModalPresentationStyle.Popover
let popoverPresentationController = vc.popoverPresentationController
// result is an optional (but should not be nil if modalPresentationStyle is popover)
if let _popoverPresentationController = popoverPresentationController {

// set the view from which to pop up
_popoverPresentationController.sourceView = actualViewController.view;
_popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.allZeros;
actualViewController.presentViewController(vc, animated: true, completion: nil)
}

然后它看起来像这样:

enter image description here

正如您所看到的,导航栏看起来不太好:-)。所以我尝试手动设置大小:

navigationBar = UINavigationBar(frame: CGRectMake(0, 0, fDialogWidth, navbarHeigth))

我尝试计算导航栏的最小宽度。我的方法是计算我的栏中的所有文本大小,其中之间有一些空格:标题+左链接('后退')+右链接('添加')。为此,我需要字体大小和字体类型。

这种方法可能不是最佳实践?

最佳答案

不要将导航栏作为 subview 添加到 View Controller 中。完全相反:将 View Controller 作为 subview Controller 添加到具有导航栏的导航 Controller !像这样:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc2 = storyboard.instantiateViewControllerWithIdentifier("Bookmarks") as! BookmarkViewController;// UIViewController
vc2.parentviewname=callerName
let vc = UINavigationController(rootViewController:vc2)

现在您的 View Controller (BookmarkViewController) 像往常一样配置自己的 navigationItem

关于swift - 如何快速获取导航栏的字体大小和字体名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31838889/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com