How do I get the font name from an otf or ttf file?
Follow these four easy steps to add and use a new font in your iOS app:
- Add your_new_font.ttf or your_new_font.otf to your Xcode project
- In your project's info.plist, add a new entry for your_new_font.ttf or your_new_font.otf to the
UIAppFonts
array (plain text for this one is 'Fonts provided by application') - At this point, I recommend adding this temporary chunk of debug code to dump all fonts that are accessible by your app, including your newly added your_new_font:
//Swift
for family: String in UIFont.familyNames {
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family) {
print("== \(names)")
}
}
//Objective-c
for(NSString *fontfamilyname in [UIFont familyNames]) {
NSLog(@"family:'%@'",fontfamilyname);
for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) {
NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"-------------");
}
- In the debug output, look for your new font's 'family' and 'font' name. Pass whatever is displayed as the 'font' name corresponding to your new font family (there could be more than one 'font' associated with your new font 'family') to
UIFont *myNewFont = [UIFont fontWithName:@"font_name_from_debug_output" size:20]
and you should be in business!
How can I get the font family name from a .ttf file in golang?
It is possible to read the data of a ttf file with this go library freetype, you just need to provide the font data and it will parse all the data. There is also an article about reading ttf files content manually with javascipt here.
Edit: If you are using freetype to get the family name or other information, you can use the Name reciever function of the struct, it accepts a NameId which is an alias for uint16. (You can find the complete table of the valid value here in the name id codes section)
for example, you can get the font family name by using the following code:
package main
import (
"fmt"
"io/ioutil"
"github.com/golang/freetype"
)
func main() {
fontFile := "./font.ttf"
fontBytes, err := ioutil.ReadFile(fontFile)
font, err := freetype.ParseFont(fontBytes)
if err == nil {
fmt.Println(font.Name(1))
}
}
How to get the title (not the name) of a text font file? (TTF and OTF)
I noticed that the "Title" column in the Explorer list view shows the exact font name string that I need, and so knowing that, then I realized that the problem can be easily solved by using the win32 shell property wrapper of the WindowsAPICodePack library to retrieve the Title property of a font file.
I'm aware that I requested a solution without 3rd party libraries, however I'm almost sure it will not be a more ideal solution than using this, because... well, the consistent alternative seems to implement the Win32 shell property wrapper ourselves.
A sample code:
Imports Microsoft.WindowsAPICodePack.Shell
Dim diInfo As New DirectoryInfo("C:\Fonts\")
For Each fiInfo As FileInfo In diInfo.GetFiles("*.ttf", SearchOption.TopDirectoryOnly)
Dim sFile As ShellFile = ShellFile.FromFilePath(fiInfo.FullName)
Dim title As String = sFile.Properties.System.Title.Value
Dim sb As New StringBuilder()
sb.AppendLine(String.Format("Name.: {0}", fiInfo.Name))
sb.AppendLine(String.Format("Title: {0}", title))
Console.WriteLine(sb.ToString())
Next
An example output:
Name.: OpenSans LightItalic.ttf
Title: Open Sans Light Italic
Name.: OpenSans Light_0.ttf
Title: Open Sans Light
Name.: OpenSans-Bold.ttf
Title: Open Sans Bold
Name.: OpenSans-BoldItalic.ttf
Title: Open Sans Bold Italic
Name.: OpenSans-ExtraBold.ttf
Title: Open Sans Extrabold
Name.: OpenSans-ExtraBoldItalic.ttf
Title: Open Sans Extrabold Italic
Name.: OpenSans-Italic.ttf
Title: Open Sans Italic
Name.: OpenSans-Light.ttf
Title: Open Sans Light
Name.: OpenSans-LightItalic.ttf
Title: Open Sans Light Italic
Name.: OpenSans-Regular.ttf
Title: Open Sans
Name.: OpenSans-Semibold.ttf
Title: Open Sans Semibold
Name.: OpenSans-SemiboldItalic.ttf
Title: Open Sans Semibold Italic
The only missing thing is to add "(TrueType)" or "(OpenType)" at the end of the string if we really need the exact same string as in the Windows Registry is shown (in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts
key).
Getting the Glyph Name from a TTF or OTF font file
In TrueType-based fonts (.TTF files), you can try parsing the 'post' table. It's fairly easy to figure out. But, only format 2.0 explicitly stores glyph names. If the post table is format 3.0, there are no glyph names stored (there are a couple of other formats defined, but fonts using them are very, very rare). In that case, your only option is to back-track using Unicode values from the 'cmap'...there are some standard references for Unicode-to-glyph names that may be useful.
For CFF-based fonts (.OTF files), glyph names are stored inside of the 'CFF ' table. That's a bit trickier to parse, but if you're only looking for the glyph name references it shouldn't be too difficult to figure out.
Related Topics
Uiview Hide/Show with Animation
Swift - How to Detect Orientation Changes
Didreceiveremotenotification Not Working in the Background
Cannot Assign a Value of Type "String" to Type "Uilabel" in Swift
How to Access File Included in App Bundle in Swift
Creating Delegates on the Spot with Blocks
How to Programmatically Fake a Touch Event to a Uibutton
Jenkins - Xcode Build Works Codesign Fails
Conversion from Nstimeinterval to Hour,Minutes,Seconds,Milliseconds in Swift
App Rejected Because of Advertisingidentifier in Facebook Sdk and Flurry Sdk
Ibeacon Notification When the App Is Not Running
Dismissmodalviewcontrolleranimated Deprecated
How to Save, Retrieve, Delete & Update My Data in Plist File in iOS
How to Get Touches When Parent View Has Userinteractionenabled Set to No in iOS
iOS Aes Encryption - Fail to Encrypt
Rotation Methods Deprecated, Equivalent of 'Didrotatefrominterfaceorientation'