UILabel断线截断尾巴

时间:2015-07-15 02:34:33

标签: ios objective-c

我使用故事板设置UILabel的行数(4)和换行符(截断尾部)。

  

这就是我现在所拥有的:

enter image description here

我的问题是如何截断/触发截断?

  

这就是我想要实现的目标:

enter image description here

更新

只是为了让我的问题更清晰。
我不想在UILabel中剪切实际字符串,就像默认UILabel的行为一样。

使用NSLog(@"%@", myLabel.text);时第一张图片生成的日志是分配给它的完整字符串,这就是我想要实现的行为。

在我的例子中:

  

一个小女孩正和她的老师谈论鲸鱼。老师   说鲸鱼吞下一个人身体是不可能的   因为即使它是一个非常大的哺乳动物。

---

  

让我的问题更清楚:

如何在不削减分配给它的实际 NSString 的情况下截断/触发截断?

或者可能会解决这个问题,这可能是对的吗?

2 个答案:

答案 0 :(得分:2)

您可以使用TTTAttributedLabel

例如:

enter image description here

宣言:

@property (strong, nonatomic) IBOutlet TTTAttributedLabel *lblTT;

包含setAttributedTruncationToken的示例代码:

NSDictionary *attr = @{NSForegroundColorAttributeName : [UIColor redColor]};

NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"...Continue Reading" attributes:attr];

[self.lblTT setAttributedTruncationToken:str];

希望它会对你有所帮助。

如果您不想使用现成的课程,请查看以下答案:How to add button to the end of text like Facebook's "Continue reading"?

答案 1 :(得分:1)

我使用Associated ObjectsMethod Swizzling来实现这一点。 在这种情况下,继承可能比类别更好。它取决于你。

的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

注意我不处理短文本条件和属性字符串。