UILabel 扩展NSTextAlignment

作者:xx明
链接:https://www.jianshu.com/p/a90c47b8d26c
來源:
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

首先要了解UILabel的用来设置和显示内部文本的方法,只需根据textAlignment的值,来动态的修改textRect的位置来达到目的(下面是UILabel需要重写的两个主要方法)

/** 返回UILabel中内容所占的rect*/
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
/** 在一定区域内显示出来UILabel的内容*/
-(void)drawTextInRect:(CGRect)rect;

使用起来最简单的方式就是为UILabel写个分类,这就需要runtime的一些知识(通过类别实现的关键就是要通过自定的方法与UILabel中的方法进行交换,来达到方法的override效果)

/** 获取某个类中的某个方法*/
OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name);
/** 交换两个方法*/
OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2)

下面是对UILbel分类具体的实现

#import 

typedef NS_ENUM(NSInteger, NSTextAlignmentExpand) {
    NSTextAlignmentTop           = 5,     // Visually top aligned
    NSTextAlignmentBottom        = 6,     // Visually bottom aligned
    NSTextAlignmentLeftTop       = 7,     // Visually left and top aligned
    NSTextAlignmentRightTop      = 8,     // Visually right and top aligned
    NSTextAlignmentLeftBottom    = 9,     // Visually left and bottom aligned
    NSTextAlignmentRightBottom   = 10,    // Visually right and bottom aligned
} NS_ENUM_AVAILABLE_IOS(6_0);

@interface UILabel (TextAlignmentAdditions)
#import "UILabel+TextAlignmentAdditions.h"
#import 

@implementation UILabel (TextAlignmentAdditions)

+(void)load {
    
    /** 获取原始的 textRectForBounds:limitedToNumberOfLines:方法*/
    
    Method originalTextRectForBounds = class_getInstanceMethod([self class], @selector(textRectForBounds:limitedToNumberOfLines:));
    Method exchageTextRectForBounds = class_getInstanceMethod([self class], @selector(ll_textRectForBounds:limitedToNumberOfLines:));
    method_exchangeImplementations(originalTextRectForBounds, exchageTextRectForBounds);
    
    /** 获取原始的 drawTextInRect:方法*/
    
    Method originalDrawTextInRect = class_getInstanceMethod([self class], @selector(drawTextInRect:));
    Method exchageDrawTextInRect = class_getInstanceMethod([self class], @selector(ll_drawTextInRect:));
    method_exchangeImplementations(originalDrawTextInRect, exchageDrawTextInRect);
    
}

- (CGRect)ll_textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    
    /** 先调用系统的textRect...方法*/
    CGRect textRect = [self ll_textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    
    switch ((NSTextAlignmentExpand)self.textAlignment) {
        case NSTextAlignmentTop:
            textRect.origin.y = bounds.origin.y;
            textRect.origin.x = 0.5 *(bounds.size.width - textRect.size.width);
            break;
        case NSTextAlignmentLeftTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case NSTextAlignmentRightTop:
            textRect.origin.y = bounds.origin.y;
            textRect.origin.x = bounds.size.width - textRect.size.width;
            break;
        case NSTextAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            textRect.origin.x = 0.5 *(bounds.size.width - textRect.size.width);
            break;
        case NSTextAlignmentLeftBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
        case NSTextAlignmentRightBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            textRect.origin.x = bounds.size.width - textRect.size.width;
            break;
        default:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
    }
    return textRect;
}

-(void)ll_drawTextInRect:(CGRect)requestedRect {
    
    /** 相当于调用ll_textRect...方法*/
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    
    /** 后调用系统的drawTextInRect:方法*/
    [self ll_drawTextInRect:actualRect];
}

@end

举个例子,有个titleLabel,产品要求title最多显示两行,但是不管显示几行都要内容的上端起点都一样。这样你设置一个大Label,把textAline改为我扩展的leftTopAline就OK了。

你可能感兴趣的:(UILabel 扩展NSTextAlignment)