【问题标题】:Want to add timestamp to Center align textview with adjusting label to Right align想要将时间戳添加到中心对齐文本视图,并将标签调整为右对齐
【发布时间】:2014-10-07 04:40:11
【问题描述】:

在我的一个聊天应用程序中,我使用了标签和文本视图。 UITextView 是 center Align 和一个标签添加到子视图并根据工作正常的文本调整那里的位置..

但我的需要是让它类似于 whatsapp 应用程序。如果文本很小并且标签可以在同一行内调整,则气泡不应扩展为两行并在单行内调整..

我附上了可以清除要求的图片。

提前致谢。

【问题讨论】:

    标签: ios objective-c ios7 textview ios8


    【解决方案1】:

    我可以在 Textkit-exclusionPaths 的帮助下执行此操作。

        CGSize sizelbl = _lbl_Timestamp.frame.size;
    [_lbl_Timestamp
     setFrame:CGRectMake(CGRectGetMaxX(_txtView.frame) - sizelbl.width - 5,
                         CGRectGetMaxY(_txtView.frame) - sizelbl.height - 5 - 3,
                         sizelbl.width, sizelbl.height)];
    
    
    CGRect ovalFrame = [_txtView convertRect:_lbl_Timestamp.bounds fromView:_lbl_Timestamp];
    
    // Since text container does not know about the inset, we must shift the frame to container coordinates
    ovalFrame.origin.x -= _txtView.textContainerInset.left;
    ovalFrame.origin.y -= _txtView.textContainerInset.top;
    
    // Simply set the exclusion path
    UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect: ovalFrame];
    _txtView.textContainer.exclusionPaths = @[ovalPath];
    

    【讨论】:

      【解决方案2】:

      这是您的解决方案:

      - (void) updateLabelFrame {
      
          /* self.label.frame is your required label */
      
          CGRect lblRect = self.label.frame;
          CGSize maxSize = CGSizeMake(self.label.frame.size.width, MAXFLOAT);
      
          /* Get exptected rect for label */
          CGRect labelRect = [self.label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label.font} context:nil];
      
          /* Caculate number of lines for label */
          CGFloat labelHeight = labelRect.size.height;
          int lines = labelHeight / 16; /* 16 is Default label height, Change with your pre-set label height */ 
          [self.label setNumberOfLines:lines];
      
          /* Set height of your label */
          lblRect.size.height = labelHeight;
          [self.label setFrame:lblRect];
      
          /* Change here your view height according to label height */
      
          NSString *trimmedText = [self.label.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
      
          if ([trimmedText length] > 0) {
      
              CGFloat viewHeight = self.label.frame.origin.y + self.label.frame.size.height + /* ADD PADDING SPACE */ 20;
      
              CGRect viewRect = self.frame; /* SET VIEW FRAME */
              viewRect.size.height = viewHeight;
              [self setFrame:viewRect];
          }
      }
      

      【讨论】:

      • 感谢您的回答,但实际上我的问题是,如果您参考上面的图像支持黑色文本是 UItextview 而浅绿色时间戳是我的标签。如何使该文本在全文视图中填充最右侧的区域,因此我将标签更新到底部位置。否则我不会为标签提供额外的空间并在 textview 上方添加。
      猜你喜欢
      • 1970-01-01
      • 2012-01-27
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多