1

In meiner App habe ich verwendet, um Tabellen anzuzeigen. In meiner Liste habe ich einige Zellenlabel Texte größer, so dass ich View more oder read more Knopf oder Etikett nach Etikett Text hinzufügen möchten. Bitte schlage mir vor, wie ich das machen kann.UILabel oder UIButton etwas wie "mehr anzeigen", wenn UILabel Textlänge größer

Ich habe damit getan. Es ist successuflly, aber es funktioniert nicht read more Geste

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

Ich weiß nicht, was hier passiert. Wenn irgendwas falsch in diesem Code so bitte helfen Sie mir

+0

dies sehen, wenn es Ihnen hilft http://stackoverflow.com/questions/32309247/add-read-more-to-the-end-of-uilabel –

+0

Ja mein Code wurde mit dieser Referenz –

+0

Wurde readMoreDidClickedGesture: Methode aufgerufen? –

Antwort

0

Es ist alles in Ordnung mit diesem Code ,, es funktioniert gut ,, Vielleicht haben Sie nicht in CellForRowAtIndexPath hinzugefügt: Methode ?? hier einfacher Code ist, funktioniert es gut

- (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; 
} 
+0

ja es funktioniert gut, aber bitte zuerst lesen Sie meine vollständige Frage. Ich habe ein Problem in Lesen Sie mehr ... Etikett. Es ist Label und hinzufügen in TAP-Geste. Wenn Sie also mehr Label tippen, funktioniert das nicht –