在我的一个聊天应用程序中,我使用了标签和textview。 UITextView是中心对齐,另外一个标签添加到子视图并根据正常工作的文本调整位置..
但我的需要是使它类似于whatsapp应用程序。如果文字很小而且标签可以在同一行内调整,那么气泡不应该扩展到两行并调整为单行。
我附上的图片将清除要求。
提前致谢。
答案 0 :(得分: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];
答案 1 :(得分:0)
以下是您的解决方案:
- (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];
}
}