I've included like always font file into project but it's not working in application. Is there some new way in iOS7 to include custom font?
This is my code which I used for iOS6:
Category:
+ (id)e_FontOpenSansBold:(CGFloat)size
{
return [UIFont fontWithName:#"OpenSans-Bold" size:size];
}
in app:
descriptionTextView.font = [UIFont e_FontOpenSansBold:30.0];
There is no error, just text is not in open sans bold like it was i previous iOS.
Related
To my knowledge, the default font of iOS 7 is Helvetica Neue UltraLight, which is a lot thinner compared to its bold predecessor. To provide a consistent design and make my forthcoming apps look the same across all common iOS versions, I'd like to apply Helvetica Neue UltraLight as the default (primary) font of the app.
Gladly, this "new font" is available since iOS version 5.0, so it's already supported by versions prior to iOS 7. Sadly, the only way I figured out to use it, is to manually call [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:size] on each UIView's font, which is tedious and error-prone to inconsistency.
So my question is, what is your way to do this or how do you handle this design change?
Here is the Objective-C Runtime solution:
#interface UIFont (CustomSystemFont)
+ (UIFont *)ln_systemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)ln_boldSystemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)ln_italicSystemFontOfSize:(CGFloat)fontSize;
#end
#implementation UIFont (CustomSystemFont)
+ (void)load
{
Method orig = class_getClassMethod([UIFont class], #selector(systemFontOfSize:));
Method swiz = class_getClassMethod([UIFont class], #selector(ln_systemFontOfSize:));
method_exchangeImplementations(orig, swiz);
orig = class_getClassMethod([UIFont class], #selector(boldSystemFontOfSize:));
swiz = class_getClassMethod([UIFont class], #selector(ln_boldSystemFontOfSize:));
method_exchangeImplementations(orig, swiz);
orig = class_getClassMethod([UIFont class], #selector(italicSystemFontOfSize:));
swiz = class_getClassMethod([UIFont class], #selector(ln_italicSystemFontOfSize:));
method_exchangeImplementations(orig, swiz);
}
+ (UIFont *)ln_systemFontOfSize:(CGFloat)fontSize
{
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
//Call original implementation.
return [self ln_systemFontOfSize:fontSize];
}
return [UIFont fontWithName:#"HelveticaNeue" size:fontSize];
}
+ (UIFont *)ln_boldSystemFontOfSize:(CGFloat)fontSize
{
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
//Call original implementation.
return [self ln_systemFontOfSize:fontSize];
}
return [UIFont fontWithName:#"HelveticaNeue-Medium" size:fontSize];
}
+ (UIFont *)ln_italicSystemFontOfSize:(CGFloat)fontSize
{
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
//Call original implementation.
return [self ln_systemFontOfSize:fontSize];
}
return [UIFont fontWithName:#"HelveticaNeue-Italic" size:fontSize];
}
#end
What I do in this example is replace the three system font methods with my own and test to see if the system version is 7 or up. If it is, I use the original methods, otherwise return a font of my choosing (in this case Helvetica Neue with UltraLight weight for regular and italic requests, and Medium weight for bold requests).
This works for everything generated in code, including system created views. It does not work when loading views from Xib and Storyboard files, because the fonts are hardcoded in the NIB file itself. Use the font picker to choose the font you need.
Why not just use the appearance API?
[[UILabel appearance] setFont:[UIFont fontWithName:#"YourFontName" size:14.0]];
UIAppearance API
Without breaking NDA by being specific about the method names - why not declare a category on UIFont to mimic the new methods for dynamic type?
For those looking for a solution in storyboards, I had some success opening the storyboard file in a text editor and doing a search/replace. You have to catch each type of font you want to change (e.g. Bold/Italic), but I was able to replace the standard system font with HelveticaNeue UltraLight by replacing:
key="fontDescription" type="system"
with
key="fontDescription" name="HelveticaNeue-UltraLight" family="Helvetica Neue"
For bold, you would replace:
key="fontDescription" type="boldSystem"
I chose to replace my bold fonts with the same UltraLight font, but for those wanting a "bold" treatment you could choose "HelveticaNeue-Thin".
I recommend using version control or making a backup copy of your file before doing this though!
How to change the Font of the Placeholder of the text filed?
Is this possible to change the default font of the Placeholder of the text filed?
If anyone know it please help me.
Thanks.
Create a subclass of UITextField and overwrite the drawPlaceholderInRect:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16.0]];
}
#fannheyward's answer was good. However, – drawInRect:withFont: is deprecated in iOS 7.0. In iOS 7, you should use:
- (void) drawPlaceholderInRect:(CGRect)rect {
UIFont *font = [UIFont fontWithName:#"STHeitiTC-Light" size:14.0];
[self.placeholder drawInRect:rect withAttributes:#{NSFontAttributeName: font}]
}
please also note that the "real" font name may be different from the one it shows on interface builder. You can use the following code to get the "real" name of available fonts in your project:
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}
I want to change the font of label. And font which i am using is shown in image.
How use it in my application. I have already add in .plist as show in image. But it not working proper. How i manage it?
Thanks in advances...
Use this to set the font programmatically:
[TheLabelName setFont:[UIFont fontWithName:#"American Typewriter" size:18]];
Custom fonts in IOS
to set an label font just
yourLabel.font = [UIFont fontWithName:#"CloisterBlack" size:64.0];
If you want to use the standard font, then just as usually: you can set it in the interface builder for this label or programmatically for this label.
If you want to apply your custom font (according to the image you want that), then you can do smth. like
UIFont *font = [UIFont fontWithName:#"MyFont" size:20];
[label setFont:font];
Also you can explore this ref:
Can I embed a custom font in an iPhone application?
it might be useful in your case.
Add your custom font file .ttf in resourse and use every time when you want to display formatted font like
headLbl.font = [UIFont fontWithName:#"Museo-700" size:20.f];
Add a font in your resource directory like this image that you have already done now main thing you cann't wirte the name of the font that you have added as it is check my image font having name ROCKB.ttf but i have write the Rockwell-Bold so you have to check by what name it has been installed in your code then pick the name from there and then put that name in lable the font will reflect the label now.
label.font = [UIFont fontWithName:#"Rockwell-Bold" size:20];
and font family by this code NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(#"Family name: %#", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSLog(#" Font name: %#", [fontNames objectAtIndex:indFont]);
}
[fontNames release];
}
[familyNames release];
I added custom fonts by following some instructions suggesting add them to plist.info under the key "Fonts provided by application" and supposedly magic happens and it works. But, I wrote this snippet to dump the fonts and I do not see my custom fonts, just the existing app fonts. I put the code in my app-delegate here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
...
// Snippet to dump the list of fonts in the system.
for (id objFamilyName in [UIFont familyNames]) {
if ([objFamilyName isKindOfClass:[NSString class]]) {
for(id objFontName in [UIFont fontNamesForFamilyName:(NSString*)objFamilyName]) {
if ([objFontName isKindOfClass:[NSString class]]) {
NSLog(#"%s: %#: %#", __FUNCTION__, objFamilyName, objFontName);
}
}
}
}
Likewise, if I try to load the named font, it does not have any effect.
My settings look like this (font names are like "cssbook.ttf"
First, load Font Book and check to see exactly what the font name / family is. then add the actual .ttf file to your application so that it is available to your app at runtime.
Hi is anyone have idea . from where i can get the list of font file (.ttf ) for supported iphone font name. some font file i have found in macOs Lib.but i need all font file . so any idea?????
You can get this information from the UIFont class. This will give you the true supported list of fonts.
NSArray *fontFamilies = [UIFont familyNames];
for(NSString *fontFam in fontFamilies) {
NSArray *fontNames = [UIFont fontNamesForFamilyName:fontFam];
... do whatever needed with the names
}