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]);
}
Related
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
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/
Background: I have been trying to display a sentence with Bold and Italic font as well as normal ones.
Question: How can I display something like this "Hello, my name is Byte". Notice that Byte is both bold and italic, while other words remains normal.
I have tried: I think coreText should be able to do something along the line, I just have not been able to find the correct way to do it. I also used TTTAttributeLabel and cannot make it both bold and italic. I have Three20 loaded, just do not know which or what to use. Webview does not work with my background.
As a reply to Carles Estevadeordal:
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 360, 300, 40)];
[webView setBackgroundColor: [UIColor clearColor]];
[webView loadHTMLString:[NSString stringWithFormat:#"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil];
[self.view addSubview:webView];
This is exactly the code, I used. It displayed white background.
After a good night sleep, I found a way to do it using TTTAtributedlabel.
Here is how:
TTTAttributedLabel *attLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(x, y, xx, yy)];
NSString *text = #"Hello, my name is Byte";
[attLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^(NSMutableAttributedString *mutableAttributedString) {
//font helvetica with bold and italic
UIFont *boldSystemFont = [UIFont fontWithName:#"Helvetica-BoldOblique" size:10];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
NSRange boldRange = [[mutableAttributedString string] rangeOfString:#"Byte" options:NSCaseInsensitiveSearch];
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
CFRelease(font);
return mutableAttributedString;
}];
I still do not have a way to add 2 attributes (ie: bold and italic separately) into the same word/letter. But this does the trick.
In case you are still interested in a CoreText solution to this issue :
newFont = CTFontCreateCopyWithSymbolicTraits(fontRef,
self.textSize,
NULL,
kCTFontBoldTrait | kCTFontItalicTrait,
kCTFontBoldTrait | kCTFontItalicTrait);
[attrString addAttribute:(NSString*)kCTFontAttributeName
value:(id)newFont
range:[copyString rangeOfString:customText.text]];
You can use this where sender is instance of UIButton
sender.titleLabel.font=[UIFont fontWithName:#"Helvetica-BoldOblique" size:18.0f];
To add two attributes (this is non ARC code):
#define kLabelFontSize 16.0
NSString labelString = #"Let's slide loudly";
[self.tttLabel setText:labelString afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:kLabelFontSize];
CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:NSMakeRange(6, 11)];
CFRelease(boldFont);
}
UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:kLabelFontSize];
CTFontRef italicFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, italicSystemFont.pointSize, NULL);
if (italicFont) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)italicFont range:NSMakeRange(12, 18)];
CFRelease(italicFont);
}
}];
return mutableAttributedString;
}];
If the sentence has a fixed format the easiest solution is to use 2 different UILabels, one with the normal font and the other one with a Bold and Italic font set.
If the sentence is dynamic then you should use a UIWebView object as it was a lavel and load a simple webpage with your text and the < b > < i > < /i > < /b > fields set at the spot when you want it bold and italic like:
[myWebViewLabel loadHTMLString:[NSString stringWithFormat:#"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil];
I hope it helps.
I want to use a custom font for a UILabel. The custom font is loaded by from a file:
NSString *fontPath = ... ; // a TTF file in iPhone Documents folder
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
How can I convert the CGFontRef to a UIFont to be used in [UILabel setFont:]?
You can't convert CGFontRef to UIFont directly but you can register CGFontRef using CTFontManagerRegisterGraphicsFont and then create corresponding UIFont.
NSString* fpath = [documentsDirectory stringByAppendingPathComponent:#"custom_font_file_name.ttf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fpath UTF8String]);
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
NSString *fontName = (__bridge NSString *)CGFontCopyFullName(customFont);
CFErrorRef error;
CTFontManagerRegisterGraphicsFont(customFont, &error);
CGFontRelease(customFont);
UIFont* uifont = [UIFont fontWithName:fontName size:12];
Conrad's answer is close but doesn't quite work. You need to provide UIFont with the PostScript name, rather than the full name.
NSString *fontName = (NSString *)CGFontCopyPostScriptName(fontRef);
UIFont *font = [UIFont fontWithName:fontName size:someSize]
CTFontRef and UIFont are toll-free bridged, so you can turn your CGFont into a CTFont and then turn that into a UIFont:
CTFontRef ctFont = CTFontCreateWithGraphicsFont(cgFont, pointSize, NULL, NULL);
UIFont *uiFont = CFBridgingRelease(ctFont);
They are, in principle, not directly convertible. One simple reason is that UIFont encapsulates font size, whereas CGFont is size-independent (with size being a property of the graphics context; see CGContextSetFontSize()).
Assuming that you have otherwise determined what font size you want, you should be able to something like:
NSString *fontName = (NSString *)CGFontCopyFullName(someCGFontRef);
UIFont *font = [UIFont fontWithName:fontName size:someSize];
[fontName release];
I haven't actually tested this, but it should work (maybe with some minor additions). I believe that there is a correspondence between names for CGFont and UIFont - but if there isn't, this obviously won't work.
You can convert your CGFont to CTFont, but unfortunately neither of those will get you a UIFont. Here are the ways to get a UIFont: ask for a system font, or ask for a font by its PostScript name. Therefore, for custom supplied fonts in a UILabel, I suggest you use the latter.
If you know the font file your app will use ahead of time you can add it to your bundle and load it with UIFont, +fontWithName:size: as UIFont searches your bundle for a font file with that PostScript name.
For example, the TrueType font file "VeraMono.ttf" has PostScript name "Bitstream Vera Sans Mono", so it's loaded into a UILabel like:
label.font = [UIFont fontWithName:#"Bitstream Vera Sans Mono" size:12];
To get PostScript names non-dynamically, use a font tool, or, in the case above the PostScript name happens to be equal to the "Full Name" display by Finder > Get Info.
However, for cases where you won't necessarily know the font's PostScript name ahead of time (such as supporting user-defined fonts), perhaps you can load the filename into NSData and "grep" for its PostScript name...
Good luck,
Updated for Swift 4 to get PostScript name (where font is the CGFont):
let fontName = font?.postScriptName as String?
textLabel.font = UIFont(name: fontName!, size: 17)
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];