Can't add custom font to application - iphone

I am having trouble using a custom font in my application.
The following describes the steps I took
1 - Add .TTF font to application
2 - Modify the info.plist file.
3 - Add the key "Fonts provided by application" to a new row
4 - And add each .TTF file (of font) to each line.
So I have an array of fonts, each of which has a .ttf extension.
And then I try to add the font
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(25, 65, 230, 33)];
lblName.backgroundColor = [UIColor clearColor];
[lblName setFont: [UIFont fontWithName:#"acmesab" size:32]]; // acmesab is my custom "font" name.
lblName.textAlignment = NSTextAlignmentLeft;
lblName.text = #"Name:";
[simapleView addSubview:lblName];

Open your project -> Targeta -> Build Phase -> Copy Bundle Resources. Check your files are shows in list or not? If not then add them and run the application again.It should work.
Use following code to check whether font is added or not and add appropriate font name from listing.
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];

For the custom font first you have to install your .ttf file in your system.after that import it to your project. Rename .ttf file as shown in "FontBook" and
add
<key>UIAppFonts</key>
<array>
<string>Times New Roman.ttf</string>
</array>
in your info.plist file. and then use it as fontname in your project
CCLabelTTF *label1 = [CCLabelTTF labelWithString:#"Name" fontName:#"Times New Roman" fontSize:26];

You should look at your font-properties what the actual font-name is.
Sometimes you import the font with name FjallaOne-Regular. But if you look at the properties you see that the font is named like Fjalla One.
Therefore you need to use it like this.
[lblName setFont: [UIFont fontWithName:#"Fjalla One" size:32]];
Instead of
[lblName setFont: [UIFont fontWithName:#"FjallaOne-Regular" size:32]];
Also check if you set the correct target membership
You can see what I mean by the correct name over here
LINK

It can be tricky sometimes as certain fonts don't seem to be recognised. Have a look at my answer from a few months ago which might help you - https://stackoverflow.com/a/10170599/331854

Related

NSMutableAttributeString not working in iOS 10.0 in UIAlertController

Hello am using NSMutableAttributedString to show different fonts but with same size in UIAlertController, the problem here is it showing two different fonts which is correct but it also showing two different sizes.Here is my code
UIFont *regularFont = [UIFont fontWithName:#"HelveticaNeue" size:11.0];
UIFont *boldFont = [UIFont fontWithName:#"HelveticaNeue-Bold" size:11.0];
UIAlertController *alertObj = [UIAlertController alertControllerWithTitle:nil message:messageStr preferredStyle:UIAlertControllerStyleAlert];
NSMutableAttributedString *attMessage = [[NSMutableAttributedString alloc] initWithString:messageStr];
[attMessage addAttribute:NSFontAttributeName value:regularFont range:NSRangeFromString(messageStr)];
[attMessage addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(25, 6)];
[attMessage addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(35, 15)];
[alertObj setValue:attMessage forKey:#"attributedMessage"];
Result can be match.Any help would be appreciated
The attributedMessage property is not public, using it is a good way to get your app rejected at the review. If you really need to style the dialogue and distribute the app on the App Store, I think you should build your own component to display the pop-up.

iPhone : Font is not affecting to UI

I'm using Constantia font family in my app, regular bold and italic style is my requirement, the problem I am facing is, I can only get output of regular style, and not the bold and italic, I've already added all three styled fonts into app, and in plist file under Fonts provided by application section. I tried with following
UIFont *bFont = [UIFont fontWithName:#"Constantia-Bold" size:24.0];
UIFont *bFont = [UIFont fontWithName:#"ConstantiaBold" size:24.0];
UIFont *bFont = [UIFont fontWithName:#"Constantia_Bold" size:24.0];
UIFont *iFont = [UIFont fontWithName:#"Constantia-Italic" size:24.0];
UIFont *iFont = [UIFont fontWithName:#"ConstantiaItalic" size:24.0];
UIFont *iFont = [UIFont fontWithName:#"Constantia_Italic" size:24.0];
but not a single case is working, only UIFont *font = [UIFont fontWithName:#"Constantia" size:24.0]; is working. I know that I'm missing something in font name only.
I tried find font into Mac font's option, I got this font under All Fonts section (left top), one strange this I found is, all Constantia bold, italic and regular are installed as a single name, i.e. Constantia only.
P.S. Fonts can be downloaded from here.
This is Step for, How to add custom font in Application.
1 - Add .TTF font in your application
2 - Modify the application-info.plist file.
3 - Add the key "Fonts provided by application" to a new row
4 - and add each .TTF file (of font) to each line.
For more info read This and This site.
For Bold
// Equivalent to [UIFont fontWithName:#"FontName-BoldMT" size:17]
UIFont* font = [UIFont fontWithFamilyName:#"FontName" traits:GSBoldFontMask size:17];
And bold/italic
UIFont* font = [UIFont fontWithMarkupDescription:#"font-family: FontName; font-size: 17px; font-weight: bold/italic;"]; // set here, either bold/italic.
Here is how you can see the real name of every font available for use by your app:
// Log fonts
for (NSString *family in [UIFont familyNames])
{
NSLog(#"Font family %#:", family);
for (NSString *font in [UIFont fontNamesForFamilyName:family])
NSLog(" %#", font);
}
UPDATE 16 June 2018: application can be rejected, try find another solution
#import <dlfcn.h>
// includer for font
NSUInteger loadFonts( )
{
NSUInteger newFontCount = 0;
NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:#"com.apple.GraphicsServices"];
const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
if (frameworkPath)
{
void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
if (graphicsServices)
{
BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
if (GSFontAddFromFile)
{
BOOL verizon = NO;
NSLog(#"%#",[[UIDevice currentDevice] machine]);
if ([[[UIDevice currentDevice] machine] rangeOfString:#"iPhone3,3"].location != NSNotFound) {
verizon = YES;
}
for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:#"ttf" inDirectory:nil])
{
if ([fontFile rangeOfString:#"_"].location != NSNotFound && verizon) {
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
}
if ([fontFile rangeOfString:#"-"].location != NSNotFound && !verizon) {
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
}
}
}
}
}
return newFontCount;
}
I'm using the function usually :)
I don't think such type of font is exist in IOS. Please check from here http://iosfonts.com/

UIFont fontWithName font name

Say you want a specific font for UIFont.
How do you know what it's called?
E.g. if you wanted to use this code:
[someUILabelObject setFont:[UIFont fontWithName:#"American Typewriter" size:18]];
From where do you copy the exact phrase "American Typewriter". Is there a header file in Xcode?
UPDATE
Also found this handy.
Might be interesting for you as Quick Win within the Debugger:
(lldb) po [UIFont fontNamesForFamilyName:#"Helvetica Neue"]
(id) $1 = 0x079d8670 <__NSCFArray 0x79d8670>(
HelveticaNeue-Bold,
HelveticaNeue-CondensedBlack,
HelveticaNeue-Medium,
HelveticaNeue,
HelveticaNeue-Light,
HelveticaNeue-CondensedBold,
HelveticaNeue-LightItalic,
HelveticaNeue-UltraLightItalic,
HelveticaNeue-UltraLight,
HelveticaNeue-BoldItalic,
HelveticaNeue-Italic
)
November 2018 - Update
A new swift-y reference for "Custom Fonts with Xcode" - by Chris Ching. I had to update, as this is a great value posting for the new way combined with all missing parts to use custom fonts in a project.
The documentation for UIFont is pretty clear on this:
You can use the fontNamesForFamilyName: method to retrieve the
specific font names for a given font family.
(Note: It is a class method)
You can get the family names like this:
NSArray *familyNames = [UIFont familyNames];
Try
NSArray *familyNames = [UIFont familyNames];
[familyNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
NSLog(#"* %#",obj);
NSArray *fontNames = [UIFont fontNamesForFamilyName:obj];
[fontNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
NSLog(#"--- %#",obj);
}];
}];
label.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:17];
I made a library to solve this problem:
https://github.com/Nirma/UIFontComplete
All fonts are represented as a system Font enum and the library also details a way of using it with your custom fonts in the read me.
Basically this:
let font = UIFont(name: "Arial-BoldItalicMT", size: 12.0)
Is replaced with either this:
let font = UIFont(font: .arialBoldItalicMT, size: 12.0)
Or this:
let myFont = Font.helvetica.of(size: 12.0)
This is how you get all font names in your project. That's it ... 3 lines of code
NSArray *fontFamilies = [UIFont familyNames];
for (int i=0; i<[fontFamilies count]; i++)
{
NSLog(#"Font: %# ...", [fontFamilies objectAtIndex:i]);
}

Changing placeholder Font of a textfiled in iphone sdk

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);
}
}

How change font of uilabel?

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