I use Associated Objects and Method Swizzling to implement that.
Inheritance maybe better than category in this case. It's up to you.
UILabel+Display.h:
#import <UIKit/UIKit.h>
@interface UILabel (Display)
@end
UILabel+Display.m:
#import "UILabel+Display.h"
#import <objc/runtime.h>
@implementation UILabel (Display)
+ (void)initialize
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(setText:);
SEL swizzledSelector = @selector(setDisplayText:);
[self swizzleeClass:class selector:originalSelector replaceSeletor:swizzledSelector];
SEL textSelector = @selector(text);
SEL swizzledTextSelector = @selector(originText);
[self swizzleeClass:class selector:textSelector replaceSeletor:swizzledTextSelector];
});
}
- (NSString *)originText
{
return objc_getAssociatedObject(self, @selector(originText));
}
- (void)setDisplayText:(NSString *)text
{
objc_setAssociatedObject(self, @selector(originText), text, OBJC_ASSOCIATION_COPY);
UIFont *font = self.font;
NSDictionary *attributes = @{NSFontAttributeName:font};
CGRect bounds = self.bounds;
CGFloat width = CGRectGetWidth(bounds);
CGFloat height = CGRectGetHeight(bounds);
CGSize maxSize = CGSizeMake(width, CGFLOAT_MAX);
NSStringDrawingOptions drawOptions = NSStringDrawingUsesLineFragmentOrigin;
CGRect rect = [text boundingRectWithSize:maxSize
options:drawOptions
attributes:attributes
context:nil];
CGFloat needHeight = CGRectGetHeight(rect);
NSString *tipText = @" ... continue reading";
NSUInteger index = text.length-1;
NSString *displayText = text;
while (needHeight>height) {
displayText = [[text substringToIndex:index]stringByAppendingString:tipText];
rect = [displayText boundingRectWithSize:maxSize
options:drawOptions
attributes:attributes
context:nil];
needHeight = CGRectGetHeight(rect);
//needHeight = [displayText sizeWithFont:font constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping].height; //NS_DEPRECATED_IOS(2_0, 7_0)
index--;
}
[self setDisplayText:displayText];
}
+ (void)swizzleeClass:(Class)class selector:(SEL)originalSelector replaceSeletor:(SEL)swizzledSelector
{
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@end
Note I don't handle short text condition and attributed string.