使用 Interface Builder 设置 UILabel 字体 Helvetica 出来
我在使用 Interface Builder 设置 UILabels 和 UITextViews 的字体时遇到问题。 我正在尝试将字体设置为 Gill Sans。 如果我以编程方式设置它,它可以正常工作,如下所示:
myLabel.font = [UIFont fontWithName:@"Gill Sans" size:24.0];
但是,如果我尝试使用 Interface Builder 设置它,我会得到与此问题中描述的相同的行为 iPhone SDK:Interface Builder 标签字体,仅在编辑标签时显示,但据说 Gill Sans 在 iPad 上可用(确实如此,因为如果我通过代码设置它它就可以工作)。如果我运行它并执行以下操作:
NSLog(@"%@", myLabel.font.fontName);
它会打印出“Helvetica”。
通常我不介意以编程方式设置它,但问题是这个特定的类在多个不同的地方使用不同的 nib 文件来提供不同的布局,所以我不能将其硬编码为始终使用相同的字体系列。每次出现时对其进行子类化将是一个巨大的痛苦,特别是因为我希望设计人员能够随时使用界面生成器创建和更改所有布局,并且如果他们每次都必须告诉我他们想要什么字体,那么我可以对每个特定情况进行硬编码,这会非常尴尬。
以前有人遇到过这个问题吗? 也许 Interface Builder 限制我使用 iPhone 字体,即使 xib 文件的目标已经设置为 iPad,但我如何说服它我的目标是 iPad?
提前致谢, 菲利佩
I'm having a problem setting the font for UILabels and UITextViews with Interface Builder.
I'm trying to set the font to Gill Sans.
If I set it programmatically it works fine, like this:
myLabel.font = [UIFont fontWithName:@"Gill Sans" size:24.0];
But if I try setting it with Interface Builder, I get the same behaviour described in this question here iPhone SDK: Interface Builder label font, only shows when editing label, but the Gill Sans is supposedly available on the iPad (and it is, since it works if I set it by code). And if I run it and do this:
NSLog(@"%@", myLabel.font.fontName);
it prints out "Helvetica".
Usually I wouldn't mind setting it programmatically, but the problem is that this particular class is used in several different places with different nib files to provide different layouts, so I can't have it hardcoded to always use the same font family. And subclassing it for each time it appears would be a huge pain, specially because I want to enable designers to create and change all the layout with interface builder whenever they want, and if they have to tell me what font they want every time so I can hardcode every particular case, that would be very awkward.
Has anyone experienced this problem before?
Maybe Interface Builder is limiting me to use the iPhone fonts even though the xib file's target is already set to iPad, but how do I convince it that I'm targeting the iPad?
Thanks in advance,
filipe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好的,显然这已被报告为 IB 3.2.3 的错误:
https://devforums.apple.com/message/236134#236134
这个人说它曾经在 3.2.2 上运行,所以我看看是否可以降级,或者我就等待苹果的修复。
Ok, apparently this has been reported as a bug with IB 3.2.3:
https://devforums.apple.com/message/236134#236134
This guy says it used to work on 3.2.2, so I'll see if I can downgrade, or I'll just wait for a fix from Apple.
您只能使用 iPhone 操作系统上存在的字体。
检查此答案了解在您的应用程序中嵌入自定义字体的方法。
You can only use fonts that are present on the iPhone OS.
Check this answer for a way to embed custom fonts in your app.
如果您不想创建任何 IBOutlet,请查看这篇文章。
https://stackoverflow.com/a/6620938/400909
Check out this post, if you do not want to create any IBOutlet.
https://stackoverflow.com/a/6620938/400909
查看我写的 IBCustomFonts 类别:
https://github.com/deni2s/IBCustomFonts
IBCustomFonts 类别允许您使用 Interface Builder 中的自定义字体( IB) 构建 iOS 应用程序时。
使用 IBCustomFonts 类别的应用程序已获得 Apple App Store 的批准(截至 2013 年 9 月)。
无需使用 IBOutlets、UILabels 和 UIButtons 的子类化或更改代码中的字体。
在 iOS6 和 iOS7 上测试。
Check out IBCustomFonts category I wrote:
https://github.com/deni2s/IBCustomFonts
IBCustomFonts category allows you to use custom fonts from Interface Builder (IB) when building your iOS apps.
Apps using IBCustomFonts category are approved by Apple App Store (as of September 2013).
No need to use IBOutlets, subclassing of UILabels and UIButtons or change fonts in code.
Tested on iOS6 and iOS7.
清洁所有目标几次即可。我必须退出 IB 和 xcode 才能在最后一个实例中获取字体。我正在使用 xcode/IB 3.2.5 并使用 chalkduster 13pt
HIH
Cleaning all targets works few times. I had to quit IB and xcode to get the fonts in my last instance. I'm using xcode/IB 3.2.5 and I used chalkduster 13pt
HIH
如果您使用子类化 UILabel 方法并且还想保留 IB 中设置的权重,那么 use 可以执行如下所示的操作。我找不到更好的方法来确定现有字体是粗体、斜体还是常规字体
If you use the subclassing UILabel approach and also want to keep the weight set in IB then use can do some thing like below. I couldn't find a better way to find out if the existing font is bold, italic or regular
我创建了一个 UILabel 子类,允许您在界面生成器中设置自定义字体:
https://github.com/IntrepidPursuits/ IP自定义字体标签
I created a UILabel subclass that allows you to set a custom font in interface builder:
https://github.com/IntrepidPursuits/IPCustomFontLabel