zoukankan      html  css  js  c++  java
  • iPhone自定义多色彩文本 Layer

    http://fei263.blog.163.com/blog/static/9279372420114276519394/

    这个自定义多色彩文本 Layer 的代码例子由 CocoaChina 会员 “带睡帽的青蛙” 分享,无需 three20,可以一个一个单词的定义文本颜色,并且可以调整词间距和行间距,根据 layer 的大小自动换行

    /**
    * @file ColoredTextLayer.h
    * @author
    * @date
    * @version Beta 1.0
    * @description
    * @copyright
    * @brief
    */

    @interface ColoredTextLayer : CALayer{
    NSMutableArray * _textNSArray;
    NSMutableArray * _colorNSArray;
    UIFont * _font;
    NSInteger _lineSpace;
    NSInteger _wordSpace;
    }

    @property (nonatomic, retain) NSMutableArray * textNSArray;
    @property (nonatomic, retain) NSMutableArray * colorNSArray;
    @property (nonatomic, retain) UIFont * font;
    @property (nonatomic, assign) NSInteger lineSpace;
    @property (nonatomic, assign) NSInteger wordSpace;

    /*!
    * set the layer colored text
    */
    - (void)setTexts:(NSArray *)texts inColors:(NSArray *)colors withFont:(UIFont *)font lineSpace:(NSInteger)lSpace wordSpace:(NSInteger)wSpace;

    /*!
    * separate the string to words
    */
    - (void)initTextsColorsArray:(NSArray *)texts inColors:(NSArray *)colors;

    @end
    /**
    * @file ColoredTextLayer.m
    * @author
    * @date
    * @version Beta 1.0
    * @description
    * @copyright
    * @brief
    */

    #import "ColoredTextLayer.h"
    #import "ColoredTextLayerDelegate.h"

    @implementation ColoredTextLayer

    @synthesize textNSArray = _textNSArray;
    @synthesize colorNSArray = _colorNSArray;
    @synthesize font = _font;
    @synthesize lineSpace = _lineSpace;
    @synthesize wordSpace = _wordSpace;

    /*!
    * @override
    */
    - (id)init
    {
    self = [super init];
    if (self) {
    [self.delegate release];
    self.delegate = nil;
    ColoredTextLayerDelegate * delegate = [[ColoredTextLayerDelegate alloc] init];
    self.delegate = delegate;


    _textNSArray = [[NSMutableArray alloc] initWithCapacity:0];

    _colorNSArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return self;
    }

    /*!
    * set the layer colored text
    * @param texts texts array
    * @param colors colors array
    * @param font font
    * @param lSpace space size between two lines
    * @param wSpace space size between two words
    * @return null
    */
    - (void)setTexts:(NSArray *)texts inColors:(NSArray *)colors withFont:(UIFont *)font lineSpace:(NSInteger)lSpace wordSpace:(NSInteger)wSpace
    {
    [self initTextsColorsArray:texts inColors:colors];
    _font = font;
    _lineSpace = lSpace;
    _wordSpace = wSpace;
    [self setNeedsDisplay];
    }

    /*!
    * separate the string to words
    * @param texts texts array
    * @param colors colors array
    * @return null
    */
    - (void)initTextsColorsArray:(NSArray *)texts inColors:(NSArray *)colors
    {
    for (int i = 0; i < texts.count; i++) {
    NSString * string = [texts objectAtIndex:i];
    UIColor * color = [colors objectAtIndex:i];
    NSArray * array = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF != ''"]];
    for (int i = 0; i < array.count; i++) {
    [_textNSArray addObject:[array objectAtIndex:i]];
    [_colorNSArray addObject:color];
    }
    }
    }

    /*!
    * @override
    */
    - (void)dealloc
    {
    [_textNSArray release];
    [_colorNSArray release];
    [super dealloc];
    }

    @end

    /**
    * @file ColoredTextLayer.h
    * @author
    * @date
    * @version Beta 1.0
    * @description
    * @copyright
    * @brief
    */

    @interface ColoredTextLayerDelegate : NSObject {

    CGPoint _cursor;
    }

    @end
    /**
    * @file ColoredTextLayer.h
    * @author
    * @date
    * @version Beta 1.0
    * @description
    * @copyright
    * @brief
    */

    #import "ColoredTextLayerDelegate.h"

    #import "ColoredTextLayer.h"

    @implementation ColoredTextLayerDelegate

    /*!
    * @override
    */
    - (id)init
    {
    self = [super init];
    if (self) {
    _cursor = CGPointMake(0, 0);
    }
    return self;
    }

    /*!
    * @override
    */
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
    {
    UIGraphicsPushContext(ctx);
    if ([layer isKindOfClass:[ColoredTextLayer class]]) {
    ColoredTextLayer * coloredLayer = (ColoredTextLayer *)layer;
    for (int i = 0; i < coloredLayer.textNSArray.count; i++) {
    NSString * string = [coloredLayer.textNSArray objectAtIndex:i];
    UIColor * color = [coloredLayer.colorNSArray objectAtIndex:i];
    CGSize size = [string sizeWithFont:coloredLayer.font];
    NSInteger restWidth = coloredLayer.frame.size.width - _cursor.x;
    NSInteger restHeight = coloredLayer.frame.size.height - _cursor.y;

    [color setFill];
    if (size.width <= restWidth && size.height < restHeight) { //can draw in current line
    CGSize stringSize = [string drawAtPoint:_cursor withFont:coloredLayer.font];
    _cursor.x = _cursor.x + stringSize.width + coloredLayer.wordSpace;
    } else if (size.width > restWidth && size.height * 2 < restHeight) { //can draw in next line
    _cursor.x = 0;
    _cursor.y = _cursor.y + size.height + coloredLayer.lineSpace;
    CGSize stringSize = [string drawAtPoint:_cursor withFont:coloredLayer.font];
    _cursor.x = _cursor.x + stringSize.width + coloredLayer.wordSpace;
    } else {
    return;
    }
    }
    }
    UIGraphicsPopContext();
    }

    @end






  • 相关阅读:
    NOIP201208同余方程
    NOIP模拟赛 最佳组合
    NOIP模拟赛 拓展
    CF1253E Antenna Coverage(DP)
    LOJ6033「雅礼集训 2017 Day2」棋盘游戏 (博弈论,二分图,匈牙利算法)
    CF582E Boolean Function(DP,状态压缩,FMT)
    CF750G New Year and Binary Tree Paths(DP)
    Codeforces Round 596 题解
    AGC008E Next or Nextnext(组合计数,神奇思路)
    ARC082E ConvexScore(神奇思路)
  • 原文地址:https://www.cnblogs.com/ligun123/p/2223671.html
Copyright © 2011-2022 走看看