最近項目需要用自己的字體般哼,針對這個做一個記錄。首先我們拿到字體庫
1惠窄、先將字體庫拖入到項目中蒸眠,然后到 Target - Build Phases下,在下圖中的兩個地方將字體庫文件進行添加(如果沒有就添加上)
2杆融、去 Info.plist 中添加鍵值對楞卡,記住值一定是指定格式,就比如下圖這個脾歇,需要將.tff也要加上(Fonts provided by application - 鍵)這里如果有多個字體就添加多個item.
3蒋腮、尋找字體庫的特有名稱,新加入字體庫的名稱并不一定是字體的名稱藕各,所以我們要確定真實的字體名稱
(1)可以按照我這種方法池摧,右鍵顯示簡介,查看全名,如下圖
(2)第二種就是雙擊字體蔽氨,安裝在mac上商模,然后打開字體冊進行查看
(3)第三種方法就是通過代碼,將字體打印出來宦棺,去找你所安裝的字體庫,我沒使用這種黔帕,找起來太慢了代咸。
// 遍歷所有字體
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]);
}
}
4、怎么使用成黄,如下:
self.label.font = [UIFont fontWithName:@"Post No Bills Colombo ExtraBold" size:22.0f];
添加字體庫到這里就結(jié)束了.....
接下來講一下文字閃爍的實現(xiàn)
1呐芥、首先創(chuàng)建一個繼承UILable的類,方便后續(xù)使用奋岁,.h代碼如下:
#import <UIKit/UIKit.h>
/** 擁有光暈掃過效果的Label */
@interface CLHaloLabel : UILabel
/** 光暈循環(huán)一次的持續(xù)時間思瘟,默認循環(huán)時間為3秒 */
@property (nonatomic, assign) CGFloat haloDuration;
/** 光暈寬度占Label寬度的百分比,默認0.5 */
@property (nonatomic, assign) CGFloat haloWidth;
/** 光暈顏色闻伶,默認白色 */
@property (nonatomic, strong) UIColor *haloColor;
/** 是否執(zhí)行動畫滨攻,默認為NO */
@property (nonatomic, assign, getter = isAnimated) BOOL animated;
@end
.m代碼如下
#import "CLHaloLabel.h"
#import <CoreText/CoreText.h>
/** 默認光暈循環(huán)一次的持續(xù)時間 */
static const NSTimeInterval kHaloDuration = 3;
/** 默認光暈寬度 */
static const CGFloat kHaloWidth = 0.5f;
/** 默認光暈顏色 */
#define kHaloColor [UIColor whiteColor]
/** 光暈動畫ID */
static NSString *const kAnimationKey = @"CLHaloLabelAnimation";
@interface CLHaloLabel ()
/** 文字層 */
@property (nonatomic, strong) CATextLayer *textLayer;
/** 動畫步調(diào) */
@property (nonatomic, copy) NSString *animationPacing;
/** 動畫步調(diào)可選的選項
kCAMediaTimingFunctionLinear // 線性動畫
kCAMediaTimingFunctionEaseIn // 快速進入動畫
kCAMediaTimingFunctionEaseOut // 快速出來動畫
kCAMediaTimingFunctionEaseInEaseOut // 快速進入出來動畫
kCAMediaTimingFunctionDefault // 默認動畫是curve動畫,也就是曲線動畫
*/
@end
@implementation CLHaloLabel
@synthesize animated = _animated;
#pragma mark - 初始化
/** 初始化方法蓝翰,用于從代碼中創(chuàng)建的類實例 */
- (instancetype)init
{
self = [super init];
if (self)
{
[self defaultInit];
}
return self;
}
/** 初始化方法光绕,用于從代碼中創(chuàng)建的類實例 */
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self defaultInit];
}
return self;
}
/** 初始化方法,用于從xib文件中載入的類實例 */
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self)
{
[self defaultInit];
}
return self;
}
/** 默認的初始化方法 */
- (void)defaultInit
{
// 設(shè)置默認的光暈顏色畜份、光暈持續(xù)時間诞帐、光暈寬度
_haloColor = kHaloColor;
_haloDuration = kHaloDuration;
_haloWidth = kHaloWidth;
_animationPacing = kCAMediaTimingFunctionEaseInEaseOut;
// 設(shè)置漸變層參數(shù)
CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
gradientLayer.backgroundColor = [super.textColor CGColor];
gradientLayer.startPoint = CGPointMake(-_haloWidth, 0);
gradientLayer.endPoint = CGPointMake(0, 0);
gradientLayer.colors = @[(id)[self.textColor CGColor],
(id)[self.haloColor CGColor],
(id)[self.textColor CGColor]];
// 設(shè)置文字層參數(shù)
self.textLayer = [CATextLayer layer];
self.textLayer.backgroundColor = [[UIColor clearColor] CGColor];
self.textLayer.contentsScale = [[UIScreen mainScreen] scale];
self.textLayer.rasterizationScale = [[UIScreen mainScreen] scale];
self.textLayer.frame = self.bounds;
self.textLayer.anchorPoint = CGPointZero;
// 設(shè)置Label參數(shù),針對從xib文件中載入的Label爆雹,需要調(diào)用self的屬性設(shè)置方法
[self setFont: super.font];
[self setTextAlignment: super.textAlignment];
[self setText: super.text];
[self setTextColor: super.textColor];
// 將文字層作為蒙層停蕉,覆蓋到漸變層上
gradientLayer.mask = self.textLayer;
// 開啟動畫
self.animated = YES;
}
#pragma mark - 重載類方法
/** 重寫Label的layer為漸變層 */
+ (Class)layerClass
{
return [CAGradientLayer class];
}
/** 停止重繪愕鼓,使用文字層繪制 */
- (void)drawRect:(CGRect)rect {}
#pragma mark - 布局子層
- (void)layoutSublayersOfLayer:(CALayer *)layer
{
[super layoutSublayersOfLayer:layer];
self.textLayer.frame = self.layer.bounds;
}
#pragma mark - 屬性
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
[self setNeedsDisplay];
}
/** 光暈持續(xù)時間 */
- (void)setHaloDuration:(CGFloat)haloDuration
{
_haloDuration = haloDuration;
if(_animated)
{
[self stopAnimating];
[self startAnimating];
}
}
/** 光暈寬度 */
- (void)setHaloWidth:(CGFloat)haloWidth
{
_haloWidth = haloWidth;
CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
gradientLayer.startPoint = CGPointMake(-_haloWidth, 0);
if(_animated)
{
[self stopAnimating];
[self startAnimating];
}
}
/** 文字顏色 */
- (UIColor *)textColor
{
// 文字顏色為漸變層背景色
UIColor *textColor = [UIColor colorWithCGColor:self.layer.backgroundColor];
if (!textColor)
{
textColor = [super textColor];
}
return textColor;
}
- (void)setTextColor:(UIColor *)textColor
{
UIColor *haloColor = self.haloColor ? self.haloColor : kHaloColor;
// 重設(shè)漸變層背景色和漸變層顏色表
CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
gradientLayer.backgroundColor = [textColor CGColor];
gradientLayer.colors = @[(id)[textColor CGColor],
(id)[haloColor CGColor],
(id)[textColor CGColor]];
[self setNeedsDisplay];
}
/** 文字 */
- (NSString *)text
{
// 返回文字層文字
return self.textLayer.string;
}
- (void)setText:(NSString *)text
{
self.textLayer.string = text;
[self setNeedsDisplay];
}
/** 文字字體 */
- (UIFont *)font
{
CTFontRef ctFont = self.textLayer.font;
NSString *fontName = (__bridge_transfer NSString *)CTFontCopyName(ctFont, kCTFontPostScriptNameKey);
CGFloat fontSize = CTFontGetSize(ctFont);
return [UIFont fontWithName:fontName size:fontSize];
}
- (void)setFont:(UIFont *)font
{
CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)(font.fontName), font.pointSize, &CGAffineTransformIdentity);
self.textLayer.font = fontRef;
self.textLayer.fontSize = font.pointSize;
CFRelease(fontRef);
[self setNeedsDisplay];
}
/** 文字對齊方式 */
- (NSTextAlignment)textAlignment
{
return [self.class UITextAlignmentFromCAAlignment:self.textLayer.alignmentMode];
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
self.textLayer.alignmentMode = [self.class CAAlignmentFromUITextAlignment:textAlignment];
}
+ (NSString *)CAAlignmentFromUITextAlignment:(NSTextAlignment)textAlignment
{
switch (textAlignment) {
case NSTextAlignmentLeft: return kCAAlignmentLeft;
case NSTextAlignmentCenter: return kCAAlignmentCenter;
case NSTextAlignmentRight: return kCAAlignmentRight;
default: return kCAAlignmentNatural;
}
}
+ (NSTextAlignment)UITextAlignmentFromCAAlignment:(NSString *)alignment
{
if ([alignment isEqualToString:kCAAlignmentLeft]) return NSTextAlignmentLeft;
if ([alignment isEqualToString:kCAAlignmentCenter]) return NSTextAlignmentCenter;
if ([alignment isEqualToString:kCAAlignmentRight]) return NSTextAlignmentRight;
if ([alignment isEqualToString:kCAAlignmentNatural]) return NSTextAlignmentLeft;
return NSTextAlignmentLeft;
}
#pragma mark - 光暈動畫
- (BOOL) isAnimated
{
return _animated;
}
- (void) setAnimated:(BOOL)animated
{
_animated = animated;
if (_animated)
[self startAnimating];
else
[self stopAnimating];
}
- (void)setHaloColor:(UIColor *)haloColor
{
_haloColor = haloColor;
CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
gradientLayer.colors = @[(id)[self.textColor CGColor],
(id)[self.haloColor CGColor],
(id)[self.textColor CGColor]];
[self setNeedsDisplay];
}
/** 開啟動畫 */
- (void)startAnimating
{
static NSString *gradientStartPointKey = @"startPoint";
static NSString *gradientEndPointKey = @"endPoint";
CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
if([gradientLayer animationForKey:kAnimationKey] == nil)
{
// 通過不斷改變漸變的起止范圍,來實現(xiàn)光暈效果
CABasicAnimation *startPointAnimation = [CABasicAnimation animationWithKeyPath:gradientStartPointKey];
startPointAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1.0, 0)];
startPointAnimation.timingFunction = [CAMediaTimingFunction functionWithName:_animationPacing];
CABasicAnimation *endPointAnimation = [CABasicAnimation animationWithKeyPath:gradientEndPointKey];
endPointAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1 + _haloWidth, 0)];
endPointAnimation.timingFunction = [CAMediaTimingFunction functionWithName:_animationPacing];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[startPointAnimation, endPointAnimation];
group.duration = _haloDuration;
group.timingFunction = [CAMediaTimingFunction functionWithName:_animationPacing];
group.repeatCount = HUGE_VALF;
[gradientLayer addAnimation:group forKey:kAnimationKey];
}
}
/** 結(jié)束動畫 */
- (void)stopAnimating
{
CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
if([gradientLayer animationForKey:kAnimationKey])
[gradientLayer removeAnimationForKey:kAnimationKey];
}
2谷徙、具體怎么使用呢
// @property (weak, nonatomic) IBOutlet CLHaloLabel *nameL;// 閃爍文字
self.nameL.text = @"開機解鎖";
self.nameL.textColor = [UIColor whiteColor];
self.nameL.haloColor = [UIColor blackColor];
self.nameL.textAlignment = NSTextAlignmentCenter;
self.nameL.font = [UIFont systemFontOfSize:22];
self.nameL.animated = YES;
哦了拒啰,到這里基本就完工了。附上demo地址完慧,有需要進行下載谋旦。