/** 返回UILabel中內(nèi)容所占的rect*/
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
/** 在一定區(qū)域內(nèi)顯示出來UILabel的內(nèi)容*/
-(void)drawTextInRect:(CGRect)rect;
- 使用起來最簡單的方式就是為UILabel寫個分類鸭限,這就需要runtime的一些知識(通過類別實現(xiàn)的關(guān)鍵就是要通過自定的方法與UILabel中的方法進(jìn)行交換茁帽,來達(dá)到方法的override效果)
/** 獲取某個類中的某個方法*/
OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name);
/** 交換兩個方法*/
OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2)
#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 {
/** 先調(diào)用系統(tǒng)的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 {
/** 相當(dāng)于調(diào)用lm_textRect...方法*/
CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
/** 后調(diào)用系統(tǒng)的drawTextInRect:方法*/
[self lm_drawTextInRect:actualRect];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者