UITextField's custom font changes while in edit mode - iphone

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.

Related

Make a UITextView object editable

This is obviously an extremely "noobish" question. I'm aware that UITextView has a Boolean editable property, (since I'm not entirely illiterate) so the answer to my question seems really straightforward. But...
I create a UITextView programatically like this:
UITextView* input = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 100, 30)];
input.backgroundColor = [UIColor whiteColor];
input.editable = YES;
input.text = #"Type something here...";
[input setUserInteractionEnabled: YES];
....
[mainView addSubview: input];
But when I run the iPhone simulator with Xcode, clicking on the input field doesn't do anything. I can't input any text, the virtual keyboard doesn't appear, etc.
So, what am I doing wrong here? Why isn't the UITextView object actually editable in the Xcode iPhone emulator?
I just ran your code in the viewDidLoad command of a sample app that I test code in and it works fine. In my case, I have a UIWebView that I added via Interface Builder which your UITextView pops on top of. I click it and the keyboard pops up.
Is it possible that you may be creating some other object that is begin added to the view after your textfield that is covering it up? Any other view objects being added in the .... part of your sample code?

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.

Custom Fonts wont work on device?

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];

UILabel font switches back after unselecting text

I have a problem: I want to change the font in my UILabel. When I select the text in the UILabel and I change the font it works, but when I unselect the text, the font comes back to defaut.
Someone have an idea?
Thx
Are you doing it in interface builder? It is very straight forward though. You can also do that using code.
lblName.font= [UIFont fontWithName:#"Arial" size:15];
Something like this.
UIFont *myFont = [ UIFont fontWithName: #"Arial" size: 18.0 ];
textLabel.font = myFont;
This should change the font, however Are you changing it in IB? Also it depends on the type of font you are selecting. It may not be visible in IB and rendering on device depends on the fonts available on it.
labelName.font=[UIFont boldSystemFontOfSize:18]; or any other font style can be assigned as desired.

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