Custom Fonts wont work on device? - iphone

I've just tried running my application on my iOS device(iphone 4 running IOS 6)
On the iPhone simulator it worked perfectly with Custom fonts?
But on device it goes to default font?
I use a custom class for my font here:
- (void) awakeFromNib {
[super awakeFromNib];
self.font = [UIFont fontWithName#"TOONISH" size:self.font.pointSize];
}
The class inharets UILabel

You may have more work to do to get a font working on the device.
Check this link:
Use custom fonts in iPhone App
You can also try the font on a lable, instead of the class, to verify the issue.
UIFont *toonishFont= [UIFont fontWithName:#"TOONISH-Bold" size:12.0f];
[uiLabel setFont:toonishFont];

Related

UIButton.titlelabel setfont doesn't work

I have added button in interface builder and referenced them, then I want to change their title font, but it doest work.
When I use:
[_button setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f]];
it WORKS, but this code is depreciated. So I have looked online to see what to use instead of this code, I found this code to use:
[_button.titleLabel setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f]];
_button.titleLabel.font = [UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f];
but NEITHER of this work. I have no idea what to do with it or what is wrong. I have tried to set globally appearance of UIButton, that works too, but I cant use it as I need that font only on some buttons.
Button is as:
#property (nonatomic, weak) IBOutlet UIButton *button;
and added in storyboard.
Font is defined:
#define APP_FONT_FUTURASTD_LIGHT #"FuturaStd-Light"
and added to Info.plist section "Fonts provided by application".
I have found out what the problem was. The other programmer who has started this project has set
[[UIButton appearance] setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_BOLD size:12.0f]];
in CSAppDelegate.m and so all my customizations were overwritten by this.
So to anyone who configures buttons font globally like this, you will not be able to then customize font of some buttons differently.
And second advice, if you continue work after someone else make sure that they give you some technical documentation or to explain to you what how and where they did.
EDIT
You can however set appearance for one view in viewWillAppear and then set it back to what you have in your CSAppDelegate.m in viewDidAppear of the view.
EDIT
Now this code is depreciated so you have to use this instead:
[[[UIButton appearance] titleLabel] setFont:UIFont];
The Problem seems to me is the Font name. The Font Name you are using in the code is not the exact name.
You Can Use the Following code to get List Of all Fonts name supported by your app
// List all fonts on iPhone
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
from the Console of xcode you can find out the exact name of the font that you added in your application
Please remove the AutoLayout checkmark if You used with Xib.It will works fine.
I think its helps to you.

What should I do to adapt my app to iOS 5.0 keeping compatibility with iOS 4

I've started playing with iOS 5 today and I've seen that XCode 4.2 only let me select iOS 5 as Base SDK but not iOS 4.
Until now I've overwriting drawRect: method in UINavigationBar to customize its appearance but iOS 5 doesn't call that method anymore. Now I've to use [UINavigationBar appearance] to do it (which I think is much better). However, appearance method is only available in iOS 5 so if I use it my app it crashes when executing on iOS 4. What should I do? Do I have to check iOS version with macros in every place I use a iOS 5 method?
Thank you,
Ariel
The answer to your first question is: You must use iOS5 (or Latest iOS SDK) as your base SDK, but you set your minimum supported iOS version under Deployment Target. There you can set iOS4.0 (or whatever you want).
The correct way to deal with your second question is to test for capability, not version. So, something like this would work in say, your application:didFinishLaunchingWithOptions: method:
// iOS5-only to customize the nav bar appearance
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)]) {
UIImage *img = [UIImage imageNamed: #"NavBarBackground.png"];
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
You will then be compiling this against the iOS5 SDK, so the use of appearance at all will be fine. But when this compiled code runs on a version of iOS before 5, it will be fine.
As said before, you can keep your drawRect: code as-is.
Another way to customized your header is like this.
UIImage *image = [UIImage imageNamed:#"header.png"];
if([navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
}
else{
UIImageView *imgView = [[[UIImageView alloc] initWithImage:image] autorelease];
[imgView setUserInteractionEnabled:NO];
[imgView setTag:TOOLBAR_TAG];
[navigationBar insertSubview:imgView atIndex:0];
}
Using the respondsToSelector you can know if the function is here.
You can put the same piece of code in both the drawRect: that iOS 4 uses and the proxy returned by [UINavigationbar appearance]. Two different code paths.
You can't do this with macros since both code paths have to be in place and the correct route to go depends on a run-time check.
Soooo... use something like this:
NSString *os_version = [[UIDevice currentDevice] systemVersion];
to get the iOS version you're currently running on and do the [UINavigationBar appearance] under 5 & newer, and you can fall back to the drawRect thing on iOS 4.
Also take advantage of downloading the 4.3 simulators for iPhone and iPad. Then you can crash faster when you accidentally use iOS 5 stuff on 4.3
--Tom

UITextField's custom font changes while in edit mode

I have a UITextField that is loaded from a xib. In it's view controllers viewDidLoad method, I set the font to a custom value which is set up correct in the .plist file and everything. It displays fine except for when it is in edit mode, at which point the font switches from my custom font to the default font, which I believe is Helvetica. This is jarring, and I'd like to keep the custom font throughout. I've looked around and I don't see any immediate solution, the only thing I've tried is resetting the textField.font property in the textFieldShouldBeginEditing and textFieldDidBeginEditing delegate methods, neither of which did anything.
Edit: I've been asked for the code, it really only is one line but here goes.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.myTextField.font = [UIFont fontWithName:#"MyFont" size:18];
}
I've also tried resetting the font in the following two methods:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.font = [UIFont fontWithName:#"MyFont" size:18];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.font = [UIFont fontWithName:#"MyFont" size:18];
return YES;
}
This does nothing, the font still changes while editing takes place, but then changes back to the custom font once the keyboard is dismissed.
Second edit:
Well, I just did something I probably should have tried before, and used a couple of different font files. Both of those fonts worked fine, but for whatever reason the custom font file I was using is causing the problem, despite it working normally in all other situations.
I had this problem with an otf font (in fact I've always had problems with otf fonts...) so now I use only ttf fonts, which work great. Check out this font converter website if you need to convert fonts from otf.
Try this:
- (void)textFieldDidChange:(UITextField *)textField {
textField.font = [UIFont fontWithName:#"MyFont" size:18];
}
Or as an alternative to fhat create an IBAction, connect it to the textField with Editing Changed and add this code to your .m:
- (IBAction)textFieldEditingChanged:(id)sender {
textField.font = [UIFont fontWithName:#"MyFont" size:18];
}
From my experience, the problem is with the font file. I found that the only solution was to find an alternative font and switch.

Setting font of UILabel with Interface Builder comes out Helvetica

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
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.
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.
Check out this post, if you do not want to create any IBOutlet.
https://stackoverflow.com/a/6620938/400909
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.
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
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
- (void)awakeFromNib
{
[super awakeFromNib];
NSString *weight = [self.font.fontName substringFromIndex:[self.font.fontName length] - 5];
if ( [weight isEqualToString:#"-Bold"] )
{
self.font = [UIFont fontWithName:#"MyriadWebPro-Bold" size:self.font.pointSize];
}
else if ( [weight isEqualToString:#"-Italic"] )
{
self.font = [UIFont fontWithName:#"MyriadWebPro-Italic" size:self.font.pointSize];
}
else
{
self.font = [UIFont fontWithName:#"MyriadWebPro" size:self.font.pointSize];
}
}
I created a UILabel subclass that allows you to set a custom font in interface builder:
https://github.com/IntrepidPursuits/IPCustomFontLabel

Repeating background image in native iPhone app

Short of putting a UIWebView as the back-most layer in my nib file, how can I add a repeating background image to an iPhone app (like the corduroy look in the background of a grouped UITableView)?
Do I need to create an image that's the size of the iPhone's screen and manually repeat it using copy and paste?
Apparently a UIColor is not necessarily a single color, but can be a pattern as well. Confusingly, this is not supported in Interface Builder.
Instead you set the backgroundColor of the view (say, in -viewDidLoad) with the convenience method +colorWithPatternImage: and pass it a UI Image. For Instance:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"gingham.png"]];
}
Of course, don't forget to add the image file to your application bundle.
There are also some built-in background pattern "colors":
groupTableViewBackgroundColor
viewFlipsideBackgroundColor
Because the are used globally across all iPhone apps, you incur the double-edged sword of an OS update updating the look and feel of your application (giving it a fresh new look that may or may not work right).
For instance:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
You should have a look at the QuartzDemo iPhone example code from Apple, specifically QuartzImageDrawing.m. Should use the following method call.
CGContextDrawTiledImage
You can even have an animated tiled background images that move. :D
Apps Amuck has a simple tutorial that show you how to do this on their site.
31 Days of iPhone Apps - Day 16: World Tour
You should use colorWithPatternImage