UILabel line breaks tail truncation
I am using storyboard to set number of lines (4) UILabel
and line break (trim tails).
This is what I have right now:
My question is how to trim / trigger to truncate?
This is what I want to achieve:
Update:
Just to make my question clearer.
I don't want to strip the actual line in UILabel
as shown by default UILabel
.
The log generated by the first image when used NSLog(@"%@", myLabel.text);
is the full line assigned to it and this is the behavior I am trying to achieve.
In my example, this is:
A little girl talked to her teacher about whales. Master said that it was physically impossible for a whale to swallow a human because, although it was a very large mammal.
---
Modify my question:
How to truncate / cause truncation without truncating the actual NSString
assigned to it?
or maybe work around that will lead to this, is that perhaps correct?
source to share
I am using Linked Objects and Swizzling Method to implement that. Inheritance might be better than category in this case. It depends on 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 am not handling short text condition and attribute string.
source to share
You can use TTTAttributedLabel library
For example:
Declaration:
@property (strong, nonatomic) IBOutlet TTTAttributedLabel *lblTT;
Sample code with setAttributedTruncationToken
:
NSDictionary *attr = @{NSForegroundColorAttributeName : [UIColor redColor]};
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"...Continue Reading" attributes:attr];
[self.lblTT setAttributedTruncationToken:str];
Hope this helps you.
If you don't want to use a prebuilt class, please check this answer: How to add a button to the end of a text like Facebook "Continue Reading"?
source to share