1

В моем приложении я использовал для просмотра таблицы. В моем списке у меня есть несколько ярлыков ярлыков ячеек, поэтому я хочу добавить View more или read more кнопку или ярлык после текста ярлыка. Пожалуйста, предложите мне, как я могу это сделать.UILabel или UIButton что-то типа «посмотреть больше», когда длина текста UILabel больше

Я сделал с ним. Это successuflly, но он не работает 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. 
} 

Я не знаю, что случилось здесь. Если что-то не так в этом коде, поэтому, пожалуйста, помогите мне

+0

увидеть это, как только это поможет вам http://stackoverflow.com/questions/32309247/add-read-more-to-the-end-of-uilabel –

+0

Да мой код был сделан с этой ссылкой –

+0

Имеет readMoreDidClickedGesture: метод был вызван? –

ответ

0

Все в порядке с этим кодом ,, он отлично работает ,, Может быть, вы не добавили в cellForRowAtIndexPath: method ?? вот простой код, он отлично работает

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

Да, он работает нормально, но сначала сначала прочитайте мой полный вопрос. У меня проблема в Read more ... label. Его ярлык и добавьте жест выделения. поэтому, когда tap читать больше ярлык, он не работает –

 Смежные вопросы

  • Нет связанных вопросов^_^