Day 3:FontLabel,自定义字体Label

FontLabel

http://old.code4app.com/ios/Font-Label/4fa251e106f6e7870b000000

 

自定义字体Label,支持各种ttf字体。你可以在http://www.webpagepublicity.com/free-fonts.html下载更多ttf字体。

 

运行效果图如下:

123

 

工程下载下来之后运行,可看到整个目录结构,如下:

Classes为AppDelegate、FontLabelViewController和FontLabel相关类文件。

Other Sources为pch预编译文件和main.m文件。

Resources为ttf字体文件、xib、plist文件。

UnitTests为单元测试类。

 

应用启动后首先加载字体文件

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[[FontManager sharedManager] loadFont:@"A Damn Mess"];

[[FontManager sharedManager] loadFont:@"Abberancy"];

[[FontManager sharedManager] loadFont:@"Abduction"];

[[FontManager sharedManager] loadFont:@"Paint Boy"];

[[FontManager sharedManager] loadFont:@"Schwarzwald Regular"];

[[FontManager sharedManager] loadFont:@"Scissor Cuts"];

 

// Override point for customization after app launch

[window addSubview:viewController.view];

[window makeKeyAndVisible];

}

在FontLabelViewController中主要添加了四种不同的FontLabel,代码简单,不做介绍。

 

下面对FontLabel相关类进行解释,首先看下FontManager,从字面上理解为字体管理器,FontManager为单例对象,提供了方法如下:

 

+ (FontManager *)sharedManager;

//从main bundle中加载TTF字体

- (BOOL)loadFont:(NSString *)filename;

//从URL中加载字体

- (BOOL)loadFontURL:(NSURL *)url;

- (CGFontRef)fontWithName:(NSString *)filename __AVAILABILITY_INTERNAL_DEPRECATED;

- (ZFont *)zFontWithName:(NSString *)filename pointSize:(CGFloat)pointSize;

- (ZFont *)zFontWithURL:(NSURL *)url pointSize:(CGFloat)pointSize;

- (CFArrayRef)copyAllFonts;

 

+ (FontManager *)sharedManager {

@synchronized(self) {

if (sharedFontManager == nil) {

sharedFontManager = [[self alloc] init];

}

}

return sharedFontManager;

}

延伸:除了这种单例写法,其它写法应该怎么写?

 

下面看下loadFont方法中是怎么实现的?

- (BOOL)loadFont:(NSString *)filename {

//根据TTF文件名称,从bundle众获取路径,如果获取到的字体路径为空则返回NO,成功则通过NSURL构建一个NSURL对象,然后再调用自身的loadFontURL方法进行加载,加载成功后将字体名称和url进行缓存。

NSString *fontPath = [[NSBundle mainBundle] pathForResource:filename ofType:@"ttf"];

if (fontPath == nil) {

fontPath = [[NSBundle mainBundle] pathForResource:filename ofType:nil];

}

if (fontPath == nil) return NO;

 

NSURL *url = [NSURL fileURLWithPath:fontPath];

if ([self loadFontURL:url]) {

[urls setObject:url forKey:filename];

return YES;

}

return NO;

}

 

loadFontURL 主要负责从给定的URL中加载字体CGFontRef。

 

- (BOOL)loadFontURL:(NSURL *)url {

CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((CFURLRef)url);

if (fontDataProvider == NULL) return NO;

CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);

CGDataProviderRelease(fontDataProvider);

if (newFont == NULL) return NO;

 

CFDictionarySetValue(fonts, url, newFont);

CGFontRelease(newFont);

return YES;

}

 

ZFont主要为自定义字体,ZMutableAttributedString为带属性的自定义字符串,FontLabelStringDrawing为NSString的类别,用于计算自定义Font的size等。

 

FontLabel继承于UILabel,提供的自定义方法如下:

@property (nonatomic, setter=setCGFont:) CGFontRef cgFont __AVAILABILITY_INTERNAL_DEPRECATED;

@property (nonatomic, assign) CGFloat pointSize __AVAILABILITY_INTERNAL_DEPRECATED;

@property (nonatomic, retain, setter=setZFont:) ZFont *zFont;

// if attributedText is nil, fall back on using the inherited UILabel properties

// if attributedText is non-nil, the font/text/textColor

// in addition, adjustsFontSizeToFitWidth does not work with attributed text

@property (nonatomic, copy) ZAttributedString *zAttributedText;

// -initWithFrame:fontName:pointSize: uses FontManager to look up the font name

- (id)initWithFrame:(CGRect)frame fontName:(NSString *)fontName pointSize:(CGFloat)pointSize;

- (id)initWithFrame:(CGRect)frame zFont:(ZFont *)font;

- (id)initWithFrame:(CGRect)frame font:(CGFontRef)font pointSize:(CGFloat)pointSize __AVAILABILITY_INTERNAL_DEPRECATED;

 

 

FontLabel用法举例:

FontLabel *label = [[FontLabel alloc] initWithFrame:CGRectMake(10, 10, 0, 0) fontName:@"Paint Boy" pointSize:40.0f];

label.textColor = [UIColor magentaColor];

label.text = @"lorem ipsum";

[label sizeToFit];

label.backgroundColor = nil;

label.opaque = NO;

[self.view addSubview:label];

[label release];

 

FontLabel *label2 = [[FontLabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(label.frame) + 10, 300, 100)

fontName:@"Schwarzwald Regular" pointSize:20.0f];

label2.textColor = [UIColor blackColor];

label2.text = @"centered in a frame.\nthis is line two. Long lines should wrap, possibly multiple times. Really long lines should truncate with an ellipsis.";

label2.textAlignment = UITextAlignmentCenter;

label2.lineBreakMode = UILineBreakModeTailTruncation;

label2.backgroundColor = [UIColor greenColor];

label2.numberOfLines = 0;

[self.view addSubview:label2];

[label2 release];

 

FontLabel *label3 = [[FontLabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(label2.frame) + 10, 300, 100)

fontName:@"Schwarzwald Regular" pointSize:20.0f];

label3.textColor = [UIColor magentaColor];

label3.text = @"right justified.\nThis is line two. Long lines should wrap";

label3.textAlignment = UITextAlignmentRight;

label3.backgroundColor = [UIColor blueColor];

label3.numberOfLines = 0;

[self.view addSubview:label3];

[label3 release];

 

FontLabel *label4 = [[FontLabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(label3.frame) + 10, 300, 100)];

ZMutableAttributedString *str = [[ZMutableAttributedString alloc] initWithString:@"This is an attributed string.\nIt even supports underline."

attributes:[NSDictionary dictionaryWithObjectsAndKeys:

[[FontManager sharedManager] zFontWithName:@"Schwarzwald Regular" pointSize:20],

ZFontAttributeName,

nil]];

[str addAttribute:ZFontAttributeName value:[[FontManager sharedManager] zFontWithName:@"Abberancy" pointSize:30] range:NSMakeRange(11, 10)];

[str addAttribute:ZForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(11, 10)];

[str addAttribute:ZBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11, 10)];

[str addAttribute:ZStrikethroughStyleAttributeName value:[NSNumber numberWithInt:ZUnderlineStyleSingle] range:NSMakeRange(11, 10)];

[str addAttribute:ZUnderlineStyleAttributeName value:[NSNumber numberWithInt:ZUnderlineStyleSingle] range:NSMakeRange(47, 9)];

label4.zAttributedText = str;

[str release];

label4.textAlignment = UITextAlignmentLeft;

label4.backgroundColor = [UIColor lightGrayColor];

label4.numberOfLines = 0;

[self.view addSubview:label4];

[label4 release];

 

 

总结:

这个demo可学到的知识:

1、CFDictionary、CFArray相关应用

2、CGFont相关应用

3、自定义UILabel

本文来自Awnlab.com麦芒实验室,转载请注明出处,谢谢合作。