我正在转换iOS7的某些应用程序,并且工具栏中的按钮上的文本出现问题。
看起来字体大小大得多,无法调整大小。因此,我的文本按钮在iOS6中非常合适,因此无法再使用了。屏幕不够宽。是否可以在不重做整个应用程序的情况下进行调整?为什么要这样愚蠢的改变?我真的很想使应用程序适应iOS7的其他几个原因。
最佳答案
您可以使用 setTitleTextAttributes:forState:
设置UIBarItems(包括UIBarButtonItems)的标题文本属性。例如,要为所有UIBarButtonItems设置标题文本字体,可以执行以下操作:
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:9.0]} forState:UIControlStateNormal];
或者,仅将其设置为一个,
[myBarButton setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:9.0]} forState:UIControlStateNormal];
您还可以创建包含UIButton的UIBarButtonItem,并完全控制这些UIButton的显示方式(字体,字体大小等)。这可以在Interface Builder中通过将UIButton拖到UIToolbar上或在代码中完成:
UIButton* button = ...;
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[toolbar setItems:@[barButton, ...] animated:YES];
请注意,如果采用这种方法,则需要将IBActions/segues连接到UIButton而不是UIBarButtonItem。