How to cache or preload SKLabelNode font? - ios

I'm making a Sprite Kit app and in my scene I added an SKLabelNode. I noticed a pretty large lag-spike when I load the SKScene. After profiling the app I found it came from creating an SKLabelNode with a papyrus font(though the font doesn't matter). When I remove the label the scene starts up almost instantaneously, but with the label it takes an extra 1-3 seconds.
I am pretty sure it's from loading the font as when I go back to the main menu and play the game again it starts up instantly again.
Now is there a way to preload the font earlier so when the player selects the level there isn't a large pause?

We had this issue, and it turned out that we were simply not using the "correct" font name. In our case, we were using "Menlo" instead of "Menlo-Regular" when instantiating our SKLabelNode, and it was incurring the several-second penalty. Once we used the correct font name, the delay no longer happened.
(Curiously, the SKLabelNode still found Menlo and used it, so it was not immediately obvious that we had the wrong font name. Presumably, the delay is caused by the system having to figure out the appropriate substitute to use. It does a good job, finding the font we intended to use, but it takes a while to do it, hence the delay.)

I had the same issue. Add following code to your very first scene, with your font name:
SKLabelNode *preload = [SKLabelNode labelNodeWithFontNamed:#"Avenir"];
preload.text = #"text";
If you don't provide text it won't load font. Note that you don't need to add label as child to your scene.

Related

Swift - Strange text rendering at runtime

I am working on an iOS app right now. I usually use the Interface Builder and recently it has been producing an issue. When the storyboard is seen in Xcode the labels on everything look fine, but some UI elements don't render properly at runtime. This has also happened for some of the images in this project. The only font used is the system font.
I have tried readjusting the font and using attributed text, but this hasn't worked for the specific label shown. Just to mention, this app is a shared project through Git so it may be an issue with it pulling incorrectly or something, but that seems odd for it to affect the text after it has been changed and adjusted.
View post on imgur.com
The results should be crystal clear text on iOS, but it results in "fuzzy" text you would expect if you were running the Windows XP on a 480p screen. What could be causing this issue?
It seems like the layer of a superview of the UILabel in the provided image is set to pre-render. This is good for performance reasons but may not always look as good, as is seen here.
If you're setting a custom layer on a superview of the UILabel, try setting the layer's shouldRasterize to false.
Ex: exampleLayer.shouldRasterize = false

Inspect SpriteKit Scene at Runtime

I am generating my levels in SpriteKit via code (in Swift), and its not working out entirely how I'd like. Is there a way to inspect the scene of the simulator at run time. E.g. So I can view how things are being placed outside the visible screen.
Thanks!
Believe me, level editor via code rarely is a good idea. There are exceptions but if you need static levels then you should find another way to defined it.
How can you see your entire scene?
AFAIK you con't with Xcode 7, however you can change how the scene is resize in order to show the full scene inside the view. Depending by the size of your level you can judge whether this is solutions fits your case.
Just open GameViewController.swift and change this
scene.scaleMode = AspectFill
into this
scene.scaleMode = .AspectFit
Now your scene will be compressed to fit the size of the screen. Of course don't forget to restore the original value once you are done.

Xib taking long time (>1s) to load. UIFont cache seems to blame

I have a UIVC loading from a Storyboard which, in turn, loads a Xib. This inner load is causing the VC to take more than a second to load. There is some fancy footwork going on in the inner xib (it loads another xib which has dynamic drawing) but this doesn't appear to be the bottleneck.
According to Instruments, UIFont -initWithCoder is the culprit. (If you drill down further TBaseFont::CopyLocalizedName() is the deepest entry that accounts for the majority of the 1s time)
I'm a bit stumped as the custom font I use occurs all throughout the app with no problems. Any ideas?
Ok the problem was that the custom font was no longer embedded in the app. I had switched to another similar one which looked the same. Weird though as I'd have expected those offending labels to revert back the System font on the device but they did not. Is it possible they were being pulled from the Macbook?
Similar issue here — Hari and gbk hinted me on the right direction.
I found we were using Lucida Grande in one little hidden place (thank you grep), but Lucida Grande is NOT embedded in iOS. I just replaced Lucida Grande with Helvetica Neue, and I gained 3+ seconds at launch time. Impressive.
Had the similar problem - in my case another dev in team just use one more type of font so after merge system cant figure out font and change it for default one - it's take from 1200ms to 2300ms.
Solution - re-setup all fonts on ViewController that cause some freeze.

how to take a UIView screenshot faster?

I'm making a dictionary app, which allows user find a word definition by touch the word in screen, and there will be a magnifier on screen and follow user's finger. I have implement it by take a screenshot of view and assign the image to maginifer image view, which is a UIImageView, however, in order to take a screenshot, the method [self.layer renderInContext:c]; cost too much time, is there any other way to do it ?maybe openGL will help?
after profiling my app with instruments->core animation, it is only 9 fps showing magnifier, but it will 30 fps if showing the system default magnifier in a UITextView, I don't know why the system is so fast
You can use exact same view in magnifier view, and change position to visible words.
There are new methods in iOS 7 that are highly optimized :
– snapshotViewAfterScreenUpdates:
– resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets:
– drawViewHierarchyInRect:afterScreenUpdates:
However they are not available in previous versions.

UITextView: Link detection working within Simulator, not on device

I've got two UITextViews containing data that should be recongised by the data detection, however whilst one works fine on both device and simulator there's one that only works under Simulator. I've attempted trashing the build from my device, cleaning the product down, removing derived data and nothing seems to resolve the inconsistency.
Link detection was enabled within Interface Builder, the data is passed in with a NSString stringWithFormat: formatted string and set with UITextView setText:. Set the same way for both, so there's no difference there, but it just doesn't seem to work correctly for one of them.
EDIT: On the device if I tap on one of the items that should detect as a link, it'll then turn blue and do link detection. I'm not setting any custom fonts or colours that could have an impact.
It appears that the trick is to setScrollable:NO. Seems to fix the problem, although if you need scrolling, I'm not sure what the answer will be...
Apparently this issue is caused by how iOS is currently handling the UITextView links. It is creating an NSAttributedString that turns sections of the text blue ( when the view contains a link ). So I've figured out that this bug only occurs when a link is the first text in the AttributedString, i.e. the first text in the text view. So it's easily fixed by prepending an whitespace to your text before setting it. Or overriding setText to " " + text;
Hope this helps guys

Resources