UILabel 扩展NSTextAlignment

作者: xx明 | 来源:发表于2016-08-28 22:37 被阅读1041次
    /** 返回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 <UIKit/UIKit.h>
    /* 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 <objc/runtime.h>
    
    @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
    

    相关文章

      网友评论

      • 小行为:很好 不错 其实看完文章真不知道是做啥 但是看了评论之后理解哪里用到了 多谢楼主 继续分享
        xx明:@小行为 写这个的初衷只是加深一下对Label内部布局的认识,熟悉runtime的使用。项目中一般用sizeTofit计算label就好了,或者直接用autolayout布局。(当然使用这个分类也没什么问题)
      • 徐大帅:不太明白你这个文章是想做啥 :smile:
        xx明:@徐大帅 举个例子,有个titleLabel,产品要求title最多显示两行,但是不管显示几行都要内容的上端起点都一样。这样你设置一个大Label,把textAline改为我扩展的leftTopAline就OK了。

      本文标题:UILabel 扩展NSTextAlignment

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