自定义的Label,可以设置同一Label字体的大小不同,颜色不一

在iOS开发中,我们搭建UI的时候常常会看到这样的设计,一小段文字,但字体大小、颜色却是不同的,本来一个UILabel就搞定的问题,却要多写好几行代码,我遇到很多这种设计,为了解决这个麻烦的东西就自己做个小小的封装。

首先,创建CLabel继承UILabel;

CLabel.h

#import 

typedef enum : NSUInteger {
    VerticalLocationTop = 0,
    VerticalLocationMiddle,
    VerticalLocationBottom
} VerticalLocation;

@interface CLabel : UILabel
@property (nonatomic, assign) VerticalLocation verticalLocation;  //文字在垂直方向的位置
@property (nonatomic, assign) CGFloat lineSpace;     //行间距

- (NSMutableAttributedString *)setLabelText:(NSString *)text frontTextFontSize:(CGFloat)frontSize frontColor:(UIColor *)frontColor andFrontRange:(NSRange)frontRange behindTextFontSize:(CGFloat)behindSize behindColor:(UIColor *)behindColor andBehindRange:(NSRange)behindRange;

@end

CLabel.m

#import "CLabel.h"

@implementation CLabel

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.verticalLocation = VerticalLocationMiddle;
    }
    return self;
}

- (void)setVerticalLoaction:(VerticalLocation)verticalLoaction
{
    _verticalLocation = verticalLoaction;
    [self setNeedsDisplay];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    switch (self.verticalLocation) {
        case VerticalLocationTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case VerticalLocationBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
        case VerticalLocationMiddle:
            
        default:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
    }
    return textRect;
}

-(void)drawTextInRect:(CGRect)requestedRect
{
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    [super drawTextInRect:actualRect];
}

- (NSMutableAttributedString *)setLabelText:(NSString *)text frontTextFontSize:(CGFloat)frontSize frontColor:(UIColor *)frontColor andFrontRange:(NSRange)frontRange behindTextFontSize:(CGFloat)behindSize behindColor:(UIColor *)behindColor andBehindRange:(NSRange)behindRange
{
    if (!text || [text isEqualToString:@""]) {
        return nil;
    }
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
    
    if (self.lineSpace) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:self.lineSpace];
        [attributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    }
    if (frontRange.length) {
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:frontSize] range:frontRange];
        if (frontColor) {
            [attributeString addAttribute:NSForegroundColorAttributeName value:frontColor range:frontRange];
        }
    }
    if (behindRange.length) {
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:behindSize] range:behindRange];
        if (behindColor) {
            [attributeString addAttribute:NSForegroundColorAttributeName value:behindColor range:behindRange];
        }
    }
    return attributeString;
}


@end


typedef enum : NSUInteger {
    VerticalLocationTop = 0,
    VerticalLocationMiddle,
    VerticalLocationBottom
} VerticalLocation;

这个枚举值是用来设置文字垂直方向上的位置,default为垂直居中


示例演示:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CLabel *label = [[CLabel alloc] initWithFrame:CGRectMake(20, 100, 300, 30)];
    label.backgroundColor = [UIColor grayColor];
    label.text = @"我是一只小码农,我骄傲我自豪。";
    label.textColor = [UIColor greenColor];
    [self.view addSubview:label];
    
    label.attributedText = [label setLabelText:label.text frontTextFontSize:24 frontColor:[UIColor redColor] andFrontRange:NSMakeRange(0, 4) behindTextFontSize:20 behindColor:[UIColor orangeColor] andBehindRange:NSMakeRange(8, 3)];
}

首先我们用正常的方式创建好CLabel后,再调用我们的方法,并将返回值赋给
label.attributedText 

这里,对象label的背景设为gray便于色彩对比,label默认字体大小是系统默认值,字体颜色设置green。接着使用我们的方法将下标为0长度为4的字段的字体大小设为24,颜色为red;将下标为8长度为3的字段字体大小设为12,颜色为orange,运行效果如下

自定义的Label,可以设置同一Label字体的大小不同,颜色不一_第1张图片


CLabel还有一个属性lineSpace,可以设置行间距(多行)

自定义的Label,可以设置同一Label字体的大小不同,颜色不一_第2张图片


好了,看看这个是不是非常简单,但如果没有封装那就要多写很多重复代码了,作为初学者,多封装些自己的东西,哪怕很简单也是一种成长。

附上源代码,欢迎大家指正。(Xcode8.1)

https://github.com/CNXwu/CustomLabel.git

你可能感兴趣的:(自定义的Label,可以设置同一Label字体的大小不同,颜色不一)