1

In my app i have used to table view. In my list i have some cell label's texts bigger so i want to add View more or read more button or label after label text. Please suggest me how can i do that.

I have done with it. It is successuflly but it doesn't work read more gesture

- (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.
}

I don't know what happen here. If any thing wrong in this code so please help me

Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34

1 Answers1

0

It's everything ok with this code,, it works fine,, May be you did not add in cellForRowAtIndexPath: method ?? here is simple code, it works fine

- (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;
}
Rob
  • 26,989
  • 16
  • 82
  • 98
  • yes it's working fine but please first you read my full question. I have problem in Read more...label. Its is label and add in tap gesture. so when tap read more label it doesn't work – Kamlesh Shingarakhiya May 30 '16 at 03:45