当 UILabel 文本长度较大时,UILabel 或 UIButton 类似于“查看更多”

Posted

技术标签:

【中文标题】当 UILabel 文本长度较大时,UILabel 或 UIButton 类似于“查看更多”【英文标题】:UILabel or UIButton something like "view more" when UILabel text length bigger 【发布时间】:2016-04-29 05:12:35 【问题描述】:

在我的应用程序中,我已经习惯了表格视图。在我的列表中,我有一些单元格标签的文本更大,所以我想在标签文本之后添加 View moreread more 按钮或标签。请建议我该怎么做。

我已经完成了。这是成功的,但它不起作用read more 手势

- (void)addReadMoreStringToUILabel:(UILabel*)label

    NSString *readMoreText = @"...Read More";
    NSInteger lengthForString = label.text.length;
    if (lengthForString >= 50)
    
        NSInteger lengthForVisibleString = [self fitString:label.text intoLabel:label];
        NSMutableString *mutableString = [[NSMutableString alloc] initWithString:label.text];
        NSString *trimmedString = [mutableString stringByReplacingCharactersInRange:NSMakeRange(lengthForVisibleString, (label.text.length - lengthForVisibleString)) withString:@""];
        NSInteger readMoreLength = readMoreText.length;
        NSString *trimmedForReadMore = [trimmedString stringByReplacingCharactersInRange:NSMakeRange((trimmedString.length - readMoreLength), readMoreLength) withString:@""];
        NSMutableAttributedString *answerAttributed = [[NSMutableAttributedString alloc] initWithString:trimmedForReadMore attributes:@NSFontAttributeName : label.font];

        NSMutableAttributedString *readMoreAttributed = [[NSMutableAttributedString alloc] initWithString:readMoreText attributes:@NSFontAttributeName :[UIFont fontWithName:@"Roboto-Light" size:13.0] ,NSForegroundColorAttributeName :[UIColor blueColor]];

        [answerAttributed appendAttributedString:readMoreAttributed];
        label.attributedText = answerAttributed;
        label.userInteractionEnabled = YES;

        UITapGestureRecognizer *readMoreGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(readMoreDidClickedGesture:)];
        readMoreGesture.numberOfTapsRequired = 1;
        [label addGestureRecognizer:readMoreGesture];
    
    else 
        NSLog(@"No need for 'Read More'...");
    

- (NSUInteger)fitString:(NSString *)string intoLabel:(UILabel *)label

    UIFont *font           = label.font;
    NSLineBreakMode mode   = label.lineBreakMode;

    CGFloat labelWidth     = label.frame.size.width;
    CGFloat labelHeight    = label.frame.size.height;
    CGSize  sizeConstraint = CGSizeMake(labelWidth, CGFLOAT_MAX);

    NSDictionary *attributes = @ NSFontAttributeName : font ;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributes];
    CGRect boundingRect = [attributedText boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    
        if (boundingRect.size.height > labelHeight)
        
            NSUInteger index = 0;
            NSUInteger prev;
            NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
            do
            
                prev = index;
                if (mode == NSLineBreakByCharWrapping)
                    index++;
                else
                    index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location;
            

            while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height <= labelHeight);

            return prev;
        
    


    /*if (SYSTEM_VERSION_GREATER_THAN(ios7))
    

    
    else
    
        if ([string sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height > labelHeight)
        
            NSUInteger index = 0;
            NSUInteger prev;
            NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];

            do
            
                prev = index;
                if (mode == NSLineBreakByCharWrapping)
                    index++;
                else
                    index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location;
            

            while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height <= labelHeight);

            return prev;
        
    */

    return [string length];

- (void)readMoreDidClickedGesture:(UITapGestureRecognizer*)sender

    UIView *view = sender.view;
    NSLog(@"%ld", (long)view.tag); //By tag, you can find out where you had tapped.

我不知道这里发生了什么。如果此代码有任何问题,请帮助我

【问题讨论】:

看到这个对你有帮助的时候***.com/questions/32309247/… 是的,我的代码是用这个参考完成的 是否调用了 readMoreDidClickedGesture: 方法? 点击readmore时不会调用它 【参考方案1】:

这段代码一切正常,运行良好,, 可能是您没有添加 cellForRowAtIndexPath: 方法?这是简单的代码,它工作正常

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 -

    TableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];


   cell.cellText.text = @"Label  Label Label Label  Label LabelLabel  Label LabelLabel  Label LabelLabel  Label LabelLabel  Label LabelLabel  Label Label Label  Label Label Label  Label Label Label  Label Label Label  Label Label Label  Label Label Label  Label Label Label  Label Label vLabel  Label Label Label  Label Label Label  Label Label Label  Label Label Label  Label Label Label  Label Label Label  Label Label Label  Label Label";

[self addReadMoreStringToUILabel:cell.cellText];

    return cell;

【讨论】:

是的,它工作正常,但请先阅读我的完整问题。我在阅读更多...标签中有问题。它是标签并添加点击手势。所以当点击阅读更多标签时它不起作用

以上是关于当 UILabel 文本长度较大时,UILabel 或 UIButton 类似于“查看更多”的主要内容,如果未能解决你的问题,请参考以下文章

当视图宽度发生变化时,如何使 UILabel 正确调整其尺寸?

如何找出截断的 UILabel 文本的宽度

当字符长度发生变化时,保持 UILabel 在 UIView 中居中。 iOS,目标 C

如何根据文本长度计算 UILabel 宽度?

当大小不成比例时如何在 UILabel 中适合文本

如何检查 UILabel 是不是被截断?