iOS 添加自定義字體父泳、仿開機解鎖的文字閃爍

最近項目需要用自己的字體般哼,針對這個做一個記錄。首先我們拿到字體庫


比如這個格式的

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地址完慧,有需要進行下載谋旦。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市屈尼,隨后出現(xiàn)的幾起案子册着,更是在濱河造成了極大的恐慌,老刑警劉巖脾歧,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件甲捏,死亡現(xiàn)場離奇詭異,居然都是意外死亡鞭执,警方通過查閱死者的電腦和手機司顿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來兄纺,“玉大人大溜,你說我怎么就攤上這事」来啵” “怎么了钦奋?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長疙赠。 經(jīng)常有香客問我付材,道長,這世上最難降的妖魔是什么圃阳? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任厌衔,我火速辦了婚禮,結(jié)果婚禮上捍岳,老公的妹妹穿的比我還像新娘葵诈。我一直安慰自己,他們只是感情好祟同,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著理疙,像睡著了一般晕城。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上窖贤,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天砖顷,我揣著相機與錄音贰锁,去河邊找鬼。 笑死滤蝠,一個胖子當(dāng)著我的面吹牛豌熄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播物咳,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼锣险,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了览闰?” 一聲冷哼從身側(cè)響起芯肤,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎压鉴,沒想到半個月后崖咨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡油吭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年击蹲,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片婉宰。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡歌豺,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出芍阎,到底是詐尸還是另有隱情世曾,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布谴咸,位于F島的核電站轮听,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏岭佳。R本人自食惡果不足惜血巍,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望珊随。 院中可真熱鬧述寡,春花似錦、人聲如沸叶洞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽衩辟。三九已至螟炫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間艺晴,已是汗流浹背昼钻。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工掸屡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人然评。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓仅财,卻偏偏與公主長得像,于是被迫代替她去往敵國和親碗淌。 傳聞我的和親對象是個殘疾皇子盏求,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容