UILabel 扩展NSTextAlignment

  • github地址https://github.com/Charming04/UILabel-TextAlignmentExpand
  • 首先要了解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 
/* Values for NSTextAlignmentExpand */
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 (LMTextAlignmentAdditions)
@end
#import "LMUILabel+TextAlignment.h"
#import 

@implementation UILabel (LMTextAlignmentAdditions)

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

- (CGRect)lm_textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    
    /** 先调用系统的textRect...方法*/
    CGRect textRect = [self lm_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)lm_drawTextInRect:(CGRect)requestedRect {
    
    /** 相当于调用lm_textRect...方法*/
    CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    
    /** 后调用系统的drawTextInRect:方法*/
    [self lm_drawTextInRect:actualRect];
}
@end

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