美文网首页
iOS 添加自定义字体、仿开机解锁的文字闪烁

iOS 添加自定义字体、仿开机解锁的文字闪烁

作者: Mr_Zhou | 来源:发表于2022-04-28 12:23 被阅读0次

最近项目需要用自己的字体,针对这个做一个记录。首先我们拿到字体库


比如这个格式的

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];

添加字体库到这里就结束了.....

接下来讲一下文字闪烁的实现

1、首先创建一个继承UILable的类,方便后续使用,.h代码如下:

#import <UIKit/UIKit.h>

/** 拥有光晕扫过效果的Label */
@interface CLHaloLabel : UILabel

/** 光晕循环一次的持续时间,默认循环时间为3秒 */
@property (nonatomic, assign) CGFloat haloDuration;
/** 光晕宽度占Label宽度的百分比,默认0.5 */
@property (nonatomic, assign) CGFloat haloWidth;
/** 光晕颜色,默认白色 */
@property (nonatomic, strong) UIColor *haloColor;
/** 是否执行动画,默认为NO */
@property (nonatomic, assign, getter = isAnimated) BOOL animated;

@end

.m代码如下

#import "CLHaloLabel.h"
#import <CoreText/CoreText.h>


/** 默认光晕循环一次的持续时间 */
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;

/** 动画步调 */
@property (nonatomic, copy) NSString *animationPacing;

/** 动画步调可选的选项
kCAMediaTimingFunctionLinear    // 线性动画
kCAMediaTimingFunctionEaseIn    // 快速进入动画
kCAMediaTimingFunctionEaseOut   // 快速出来动画
kCAMediaTimingFunctionEaseInEaseOut // 快速进入出来动画
kCAMediaTimingFunctionDefault   // 默认动画是curve动画,也就是曲线动画
*/

@end



@implementation CLHaloLabel

@synthesize animated = _animated;


#pragma mark - 初始化

/** 初始化方法,用于从代码中创建的类实例 */
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        [self defaultInit];
    }
    return self;
}

/** 初始化方法,用于从代码中创建的类实例 */
- (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
{
    // 设置默认的光晕颜色、光晕持续时间、光晕宽度
    _haloColor    = kHaloColor;
    _haloDuration = kHaloDuration;
    _haloWidth    = kHaloWidth;
    _animationPacing = kCAMediaTimingFunctionEaseInEaseOut;
    
    // 设置渐变层参数
    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]];
    
    // 设置文字层参数
    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;
    
    // 设置Label参数,针对从xib文件中载入的Label,需要调用self的属性设置方法
    [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];
}

/** 光晕持续时间 */
- (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;
    
    // 重设渐变层背景色和渐变层颜色表
    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)
    {
        // 通过不断改变渐变的起止范围,来实现光晕效果
        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];
    }
}

/** 结束动画 */
- (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地址,有需要进行下载。

相关文章

  • iOS 添加自定义字体、仿开机解锁的文字闪烁

    最近项目需要用自己的字体,针对这个做一个记录。首先我们拿到字体库 1、先将字体库拖入到项目中,然后到 Target...

  • iOS在应用中添加自定义字体

    iOS在应用中添加自定义字体 一、在应用中添加自定义字体的步骤 1、网上提供的字体库有很多,下载完成后,将其导入工...

  • 动态注册字体

    项目中如果只是使用几种固定的字体,可以本地添加自定义字体,可以参考iOS加入自定义字体库[https://www....

  • iOS开发之自定义字体

    iOS 开发中文字默认使用的是系统的字体,但如果需要自定义字体,方法也很简单。 实现步骤 下载需要的字体,并将字体...

  • 关于导航栏(UINavigationBar)的一些操作

    -- 项目中各种对于自定义导航栏的需求, 有添加 loadingView, 去除返回按钮字体, 添加轮播文字视图,...

  • FBShimmering文字闪烁效果实现解析

    iOS设备解锁时的文字闪烁效果非常得好看,在Github上搜索了下,发现facebook的FBShimmering...

  • 2019-01-15 关于iOS系统字体

    中文字体: iOS 9 以后中文字体是"苹方"。 iOS 8 中文字体是"常州华文的黑体-简"。 英文字体:iOS...

  • Swift实现文本的逐渐显示

    可以实现横向或者竖向的文字逐渐显示,通过添加自定义字体库,即可实现有趣字体的逐字显示 源文件地址:https://...

  • iOS添加自定义字体

    如何在iOS中添加自定义的字体 第一步:导入字体库,就是哪个ttf后缀的文件 第二步:在plist中配置字体库 第...

  • 多种中文字体

    功能介绍 使用动态下载中文字体的 API 可以动态地向 iOS 系统中添加字体文件,这些字体文件都是下载到系统的目...

网友评论

      本文标题:iOS 添加自定义字体、仿开机解锁的文字闪烁

      本文链接:https://www.haomeiwen.com/subject/mwzryrtx.html