CTFontCreateWithName returning ".LastResort" font - iphone

I have multiple custom fonts provided with my app, all defined in the app's plist correctly. I can load UIFonts with those custom fonts with no problems. UILabels using [UIFont fontWithName:#"some-custom-font" size:14] as font work flawlessly.
What doesn't work is CoreText methods.
I need to use those fonts in attributed strings, so I absolutely need the CTFontRef. Till iOS 5 I had no problem what-so-ever. Now, CTFontCreateWithName tends to return a font called .LastResort, instead of what I need. If I kill the app and relaunch, some of the fonts that failed last time do work now, but others don't. Seemingly random. If I call CTFontCreateWithName immediately on app launch, that specific font works later, but again most of the others do not.
I never encountered this kind of behavior before. My app isn't very memory heavy. This feels like some iOS 5 bug. Any help or advice will be very welcome.

I've only used custom fonts via UIKit. You may want to contact DTS, or definitely bugreport.apple.com

Related

Is there an overview of the text and string drawing system changes in iOS 7?

In iOS 7 some of the most important string drawing and metrics calculation methods were depracated with no obvious alternative given.
The page on NSString UIKit Additions Reference is red like blood. Almost everything deprecated. Xcode throws 300 warnings at me.
I try to find out what was running through Apples mind and what they changed in UIKit text system but where would I start? Did they mention somewhere why all of this is deprecated and how the text system works different now? And how to adapt? How to calculate text bounding box when label can scale the font to fit size? Is TextKit the solution?
I spent 3 hours on Google but I found no useful information on how to solve this problem.
We should document all alternatives here so all developers who run into this depressive deprecation mess find peace of mind quickly.
If you look at the deprecations, most of them deprecate the use of UIFont to use a dictionary of attributes instead.
drawInRect:withFont: (Deprecated in iOS 7.0. Use drawInRect:withAttributes: instead.)
drawInRect:withFont:lineBreakMode: (Deprecated in iOS 7.0. Use drawInRect:withAttributes: instead.)
sizeWithFont:(Deprecated in iOS 7.0. Use sizeWithAttributes: instead.)
etc…
So if you're looking for a place to start, learn how to use dictionaries of attributes to set up fonts. It looks like a lot of deprecations, but you don't actually need to learn that much new stuff.
If you want to update your code to use the new TextKit system, check out the WWDC videos and the TextKit Programming Guide.
If you want to know Apple's reason for deprecating so much, I'd guess that it has to do with how UILabel and UITextView used to be built on web views, now they're built on TextKit.

Double header on iphone

I have come across a weird double header/h1 tag on the iPhone 4. If you view the my site on an iPhone the header is rendered twice, but overlaying each other slightly, if you zoom in on the header, it becomes one, like a normal heading on any website.
Has anyone else encountered this and know a solution? I've been at it for weeks can't find anything like it online.
Here is my site: http://cafe.cic.hull.ac.uk/~405851/
Thanks for any help.
Your problem seems similar to the one discussed and answered here: iOS 4.2+ webfont (ttf) 's bold font-weight rendering bug
Apparently one solution is to to change the inherited style via a font-weight:normal styling. This doesn't solve your issue if you feel that the text needs to be bold of course. You could try using different font weights as well.
Custom font rendering has had funky bugs in iOS for a while. In the iOS3 days, I was able to crash webkit itself with custom fonts. Good times.
So I would recommend using something like http://www.google.com/webfonts instead. The font files they serve are guaranteed to work well cross platform, and the stylesheet to include the fonts is tailored to the quirks of platform that requests it. They do a good job at avoiding font rendering bugs and cross platform inconsistencies.
For that reason, I would use Google WebFonts to serve any custom fonts if I had web content that needed to be shown consistently cross platform.
The tradeoff is that you don't get as much selection of fonts. But they do have a good amount, just not every font.

How to replace the default font for the whole OS

Is there a way to make my app override the current font of the currently foreground application?
What I have in mind is no matter which app is on foreground my app to be able to attach a button to the keyboard which will switch the font to a custom one thus overriding the current font for all ui elements(labels, buttons, text fields etc.).
What you're looking for is not allowed by Apple.
It seems it can be done for jailbroken devices though. It's difficult to tell how exactly they're doing it but I guess they meddled around with the images/colors provided by Apple for the keyboard and replaced them with their custom ones leaving the default un-recoverable.
Here's a blog about it too.
You can create a category on UIFont and replace systemFontWithName:size: and boldSystemFontWithName:size: with your own font. But this may have some weird side effects, as even alertViews and actionSheets are customized with that method..
edit: of course this works only in your own app.. you cannot change anything outside of your app.

Formatting text within UILabel differently

I'd like different words in a UILabel to be different colors. Does this mean each word will need to be a different UILabel? I'm guessing yes, though sure would be nice to just put color codes in the label somehow, you know? I guess I'm a bit spoiled by text markup in HTML.
There is no proper UIRichTextView in iOS. It's high on my wish-list for iOS 6 (and there's some reason to believe we may get it then due to the release of Pages).
Your options are to use multiple UILabel views, NSString UIKit Additions, Core Text, UIWebView, or one of a few third-party frameworks such as:
NSAttributedString-Additions-for-HTML
CoreTextWrapper
OHAttributedLabel
OmniUI
All of the current solutions have different problems. The most common problem is that it's hard to get select and copy functionality to work with rich text unless you use a web view. Web views are incredibly annoying because they're asynchronous and you have to do a lot of your interactions in JavaScript.
I wish there were a better answer.
(Obligatory shilling: This topic is covered in depth in Chapter 18 of iOS 5 Programming Pushing the Limits.)
UILabel doesn't support segmented formatting (the entire thing can only have one format).
Have a look at OHAttributedLabel, which does what you want.
As far as I'm aware you'd need to have separate labels for each different coloured word. Depending what you're trying to do you may be able to make use of myLabel.textColor to change the colour of the periodically or on events etc.

Can I preload custom fonts on iPhone to improve performance?

I've added the UIAppFonts entry on my info.plist to enable custom ttf fonts, it works fine. However when using the font there's also a delay in the UIView the first time it's called, is there a way to preload the fonts or some other technique to avoid this?
Thanks
In your application delegate or some other place that is called when the application launches try creating a hidden UILabel that uses your custom font and remove it straight away. That way it will preload the font so that the next time you use a UILabel with that font it won't pay the first time penalty.