How to include ttf fonts to iOS app

Up till now there hasn’t been an easy way to add custom fonts to your iPhone applications. As of iOS 4 it has become very easy to do. Here is what you need to do in order to add custom fonts:

  1. Add your custom font files into your project using XCode as a resource
  2. Add a key to your info.plist file called UIAppFonts.
  3. Make this key an array
  4. For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array
  5. Save info.plist
  6. Now in your application you can simply call [UIFont fontWithName:@"CustomFontName" size:12] to get the custom font to use with your UILabels and UITextViews, etc…

OR

add your font to your project and next function, and perform from start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#import

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

It’s that simple!

NSDateFormatter and UIFont

Потерял свои закладки с этими данными. Для себя делаю заметку, но может кому то тоже понадобится.
Continue reading

Quartz 2D iPhone

The most frequently used examples in Quartz

Quartz Demo

Open context:

1
2
3
4
5
6
// Create context
UIGraphicsBeginImageContext(itemImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();

//Draw image in context
CGContextDrawImage(context, imageRect, [itemImage CGImage]);

Draw square in context:

1
2
3
4
5
CGContextSaveGState(context);
// Set color to context for draw
CGContextSetRGBFillColor(context, 0.4f, 0.4f, 0.4f, 0.4f);  // gray color with alpha 40 %
CGContextFillRect(context, rect);                               // draw in rect of square
CGContextRestoreGState(context);

Draw text in context:

1
2
3
4
5
6
7
8
9
10
NSString *text = @"Hello world!";
UIFont* font = [UIFont fontWithName:@"Helvetica" size:14];

// Open
UIGraphicsPushContext(context);
// Color for text
[[UIColor whiteColor] set]; // or CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
[text drawInRect: CGRectMake(2.0f, 0.0f, 30.0f, 30.0f) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
// Close
UIGraphicsPopContext();

Use CGContextSetShadow for draw shadow, example:

1
2
CGSize myShadowOffset = CGSizeMake (-15,  20);
CGContextSetShadow (context, myShadowOffset, 5);

Quartz 2D Programming Guide – pdf file from “iPhone Dev Center”

How to include ttf fonts in iPhone app

Хороший способ подключения шрифтов.

Кидаем в ресурсы наш шрифт. Вставляем и выполняем функцию.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)
                    for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"TTF" inDirectory:nil])
                         newFontCount += GSFontAddFromFile([fontFile UTF8String]);
          }
     }
     return newFontCount;
}

Смотрим что у нас появилось

1
NSLog(@"%@",[UIFont familyNames]);