自定义字体显示在IB中,但不在模拟器中 [英] Custom font displays in IB but not in simulator
问题描述
我建立了一个 UITextView
和一个 UILabel
来使用自定义字体。 (这是一个垂直镜像蒙古字体,但我也包括英文文本,以便您可以看到效果。)在界面生成器中显示的文字,但在模拟器中的大多数字符在 UITextView
只是空的框。奇怪的是,在 UILabel
中的字符在模拟器中正确显示。
如何让它们显示在 UITextView
中?
注:
- 我正在使用Xcode版本6.3.2。从中运行了代码,以确认我的字体真的在捆绑中。我得到了以下结果,证实它是在捆绑,所以,仍然不能解决我的问题。
家庭:ChimeeWhiteMirrored
字体:ChimeeWhiteMirrored
家庭:ChimeeWhiteMirrored
字体:ChimeeWhiteMirrored
有没有人看到我的错误或有任何其他的想法?
更新
收到@ darkheartfelt的downvote和
这个问题可以复制如下(使用Xcode 7.1.1):
开始一个新的单个视图项目。
UITextView
li> 1-10: 一二三四五六七八九十十
这会再现特殊字符和正如我在下面的答案中所描述的,我发现解决这个问题的唯一办法就是在代码中设置字体。导入UIKit
类ViewController:UIViewController {
@IBOutlet弱var textView:UITextView!
$ b $重写func viewDidLoad(){
super.viewDidLoad()
//设置自定义字体
textView.font = UIFont(name:ChimeeWhiteMirrored ,size:(textView.font?.pointSize)!)
}
}
如果我错了或错过了什么,请让我知道。在这之前,我将不得不继续使用我接受的答案。
进入我的项目,我选择添加到目标成员,检查字体后,没有添加到目标!它在Interface Builder中工作得很好,但在SIM或编译时不起作用。
选择项目文件夹视图中的字体文件并检查目标成员资格框修正了它:
I have set up a UITextView
and a UILabel
to use a custom font. (It is a vertically mirrored Mongolian font, but I also included English text so that you can see the effect.) The words display in the Interface Builder, but in the simulator most of the characters in the UITextView
are just empty boxes. Strangely, in the characters in the UILabel
do display correctly in the simulator.
How do I get them to display in the UITextView
as well?
Notes:
- I am using Xcode version 6.3.2. From this SO Q&A I know that custom fonts should work with Xcode 6 in IB.
- I have added my font file to the project and selected it from the Custom Font List for both the
UITextView
and theUILabel
. - I added the key "Fonts provided by application" in my project's
Info.plist
and added my custom font to it. This question is a very similar one (but is different because it doesn't mention the
UITextView
andUILabel
issue). At the time of this writing the OP for that question has not indicated if the top voted answer helped or not. I ran the code in that answer to confirm that my font is really in the bundle. I got the following result, which confirms that it is in the bundle, so that still doesn't solve my problem.Family : ChimeeWhiteMirrored Font : ChimeeWhiteMirrored
Does anyone see my mistake or have any other ideas?
Update
After receiving @darkheartfelt's downvote and comment to my answer below, I recreated my original problem thinking maybe I had missed something. Although in iOS 9 the font displays somewhat differently in the simulator, the basic problem is still there: it looks ok in IB but not in the simulator.
The problem can be reproduced as follows (using Xcode 7.1.1):
- Start a new single view project.
- Add the the font to the project (can be downloaded here from related github project)
- In Info.plist file add "Fonts provided by application" with font name "ChimeeWhiteMirrored.ttf".
- Confirm that the target's "Copy Bundle Resources" contains ChimeeWhiteMirrored.ttf.
- Add a
UITextView
to the storyboard. - In the IB Attributes inspector paste in the following text:
1-10: one two three four five six seven eight nine ten
- In the IB Attributes inspector set the font to Custom and the family to ChimeeWhiteMirrored.
This reproduces the problem for the special characters and as I described in my answer below, the only thing I have found to solve this problem it to set the font in code.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// set custom font
textView.font = UIFont(name: "ChimeeWhiteMirrored", size: (textView.font?.pointSize)!)
}
}
If I'm wrong or missing something then please let me know. Until then I'll have to continue using my accepted answer below.
It appears that, while when I first imported the font into my project I selected "Add to target membership", after inspecting the font, it was not added to the target! It worked just fine in Interface Builder, but didn't work in the sim or when compiled.
Selecting the font file in the project folder view and checking the target membership box fixed it:
这篇关于自定义字体显示在IB中,但不在模拟器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!