我正在使用UICollectionView并使用自定义UICollectionViewCell。用户创建的帖子显示在“新闻Feed”屏幕上。根据文本的长度,我增加了单元格的高度。但是在“Say Something”文本字段下会出现奇怪的间距。
当用户按ENTER键输入文字时,它的效果非常好。如果用户在没有按ENTER的情况下输入多行,则会出现问题。
此代码适用于iPhone 5,但不适用于iPhone 6和6 plus。自上个星期以来,我一直坚持这个问题。此问题是否仅与iPhone 6和6相关?
我没有找到关于我的问题的相关问题,这就是为什么要问一个新问题。
- (void)configureCell:(NewsFeedObj *)newsFeed{
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1].CGColor;
self.btnPostComment.enabled = NO;
self.btnPostComment.titleLabel.textColor = [UIColor colorWithRed:191.0/255.0 green:191.0/255.0 blue:191.0/255.0 alpha:1.0];
self.postid = newsFeed.postId;
float yPosition = 0;
//*********************Post Username and tagged users setting*********
self.userName.enabledTextCheckingTypes = NSTextCheckingTypeLink; // Automatically detect links when the label text is subsequently changed
self.userName.shadowColor = [UIColor whiteColor];
self.userName.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)
self.userName.numberOfLines = 4;
self.userName.lineSpacing = 5;
[self settingUserSectionDetailWithNewsFeed:newsFeed];
CGRect frame = self.userName.frame;
frame.size.height = [self.userName sizeThatFits:self.userName.frame.size].height+3;
self.userName.frame = frame;
//set Time and locatin postion accordint to the name lable.
[self.timeLocationView setFrame:CGRectMake(self.timeLocationView.frame.origin.x, (self.userName.frame.origin.y+self.userName.frame.size.height)+3, self.timeLocationView.frame.size.width, self.timeLocationView.frame.size.height)];
[self.userSection setFrame:CGRectMake(self.userSection.frame.origin.x, self.userSection.frame.origin.y, self.userSection.frame.size.width, self.timeLocationView.frame.origin.y+self.timeLocationView.frame.size.height+5)];
//****************************************user Location setting
if (newsFeed.postLocation.length>0) {
self.location.hidden = NO;
self.locationIconImg.hidden = NO;
self.location.text = newsFeed.postLocation;
}else{
self.location.hidden = YES;
self.locationIconImg.hidden = YES;
}
//**********************Check Post Spam/Offensive/Or Post is yourself********************
if (newsFeed.postUser.user_id == [ConnectionManager getInstance].userProfile.user_id) {
[self.btnFlag setImage:[UIImage imageNamed:@"edit-report"] forState:UIControlStateNormal];
}else{
if (newsFeed.postReportFromCurrentUser) {
if ([newsFeed.postReportFromCurrentUser[@"kind"] isEqualToString:@"offensive"] || [newsFeed.postReportFromCurrentUser[@"kind"] isEqualToString:@"spam"]) {
[self.btnFlag setImage:[UIImage imageNamed:@"report_active"] forState:UIControlStateNormal];
}else{
[self.btnFlag setImage:[UIImage imageNamed:@"report_inactive"] forState:UIControlStateNormal];
}
}
}
//post Time setting
self.dateAdded.text = [self timeLeftSinceDate:[self getDateFromString: newsFeed.postCreatedAt]];
//Post Edit long gesture
UILongPressGestureRecognizer *postTextEdit = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(textEditPost:)];
[self.description addGestureRecognizer:postTextEdit];
//***********************************post User profile image setting
NSDictionary *postUserImgDict = newsFeed.postUser.avatar[@"medium"];
NSString *userImgUrl = @"";
if (![postUserImgDict[@"url"] isMemberOfClass:[NSNull class]]) {
userImgUrl = postUserImgDict[@"url"];
}
[self.profileImage sd_setImageWithURL:[NSURL URLWithString:userImgUrl] placeholderImage:[UIImage imageNamed:@"avatar"]];
[self.profileImage setupImageViewer];
//*************************************Post text setting here
self.description.text = newsFeed.postBody;
CGSize rect;
if (![newsFeed.postBody isEqualToString:@""]) {
/*NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:14.0f]};
NSAttributedString *descString = [[NSAttributedString alloc] initWithString:self.description.text attributes:attributes];
self.description.attributedText = descString;*/
[self.description setAttributes: [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Arial" size:14.0f] forKey:NSFontAttributeName]];
rect =[self.description suggestedFrameSizeToFitEntireStringConstrainedToWidth:self.userSection.frame.size.width-20];
//rect = [self.description sizeThatFits:CGSizeMake(self.userSection.frame.size.width-20, CGFLOAT_MAX)];
/*if (self.userSection.height>60) {
[description setFrame:CGRectMake(self.userSection.frame.origin.x+10, self.userSection.frame.origin.y+self.userSection.frame.size.height+10, self.userSection.frame.size.width-20, rect.height+10)];
}else{
[description setFrame:CGRectMake(self.userSection.frame.origin.x+10, self.userSection.frame.origin.y+self.userSection.frame.size.height+10, self.userSection.frame.size.width-20, rect.height+10)];
}*/
[description setFrame:CGRectMake(self.userSection.frame.origin.x+10, self.userSection.frame.origin.y+self.userSection.frame.size.height+10, self.userSection.frame.size.width-20, rect.height+10)];
if(IS_IPHONE_5) {
yPosition = self.description.frame.origin.y+self.description.frame.size.height;
}
else {
yPosition = self.description.frame.origin.y + (rect.height + 10);
}
}else{
yPosition = self.userSection.frame.origin.y+self.userSection.frame.size.height+10;
}
//*************************************************************************
//post Image setting here
if(![newsFeed.postImageUrl isMemberOfClass:[NSNull class]] && ![newsFeed.postImageUrl isEqualToString:@""]){
[self.postedImage sd_setImageWithURL:[NSURL URLWithString:newsFeed.postImageUrl] placeholderImage:nil];
self.postedImage.hidden = NO;
self.postedImage.frame = CGRectMake(0, yPosition, self.postedImage.frame.size.width, self.postedImage.frame.size.height);
yPosition = self.postedImage.frame.origin.y+self.postedImage.frame.size.height;
}else{
self.postedImage.hidden = YES;
}
//***************************Rating Section ************************************
self.ratingSection.frame = CGRectMake(self.ratingSection.frame.origin.x, yPosition, self.ratingSection.frame.size.width, self.ratingSection.frame.size.height);
yPosition = self.ratingSection.frame.origin.y+self.ratingSection.frame.size.height;
//***************************RatingCOunt Section ************************************
self.countSection.frame = CGRectMake(self.countSection.frame.origin.x, yPosition, self.countSection.frame.size.width, self.countSection.frame.size.height);
if (newsFeed.postRatingAction.count>0) {
self.lblAwesome.text = [NSString stringWithFormat:@"%ld .",(long)[newsFeed.postRatingAction[0] integerValue]];
self.lblWhatevs.text = [NSString stringWithFormat:@"%ld .",(long)[newsFeed.postRatingAction[1] integerValue]];
self.lblNoBueno.text = [NSString stringWithFormat:@"%ld .",(long)[newsFeed.postRatingAction[2] integerValue]];
if (newsFeed.postReactionFromCurrentUser.count>0) {
if ([newsFeed.postReactionFromCurrentUser[@"kind"] isEqualToString:@"awesome"]) {
self.btnAwesome.selected = YES;
self.btnWhatEvs.selected = NO;
self.btnNoBueno.selected = NO;
}else if ([newsFeed.postReactionFromCurrentUser[@"kind"] isEqualToString:@"whatevs"]) {
self.btnAwesome.selected = NO;
self.btnWhatEvs.selected = YES;
self.btnNoBueno.selected = NO;
}else if ([newsFeed.postReactionFromCurrentUser[@"kind"] isEqualToString:@"nobueno"]){
self.btnAwesome.selected = NO;
self.btnWhatEvs.selected = NO;
self.btnNoBueno.selected = YES;
}
}else{
self.btnAwesome.selected = NO;
self.btnWhatEvs.selected = NO;
self.btnNoBueno.selected = NO;
}
}
yPosition = self.countSection.frame.origin.y+self.countSection.frame.size.height;
//***************************Comment Section ************************************
//if comment against post not empty
if (newsFeed.commentsArray.count>0) {
self.commentSectionContainer.hidden = NO;
float commenty=0;
self.lblComments.text = [NSString stringWithFormat:@"%lu",(unsigned long)newsFeed.commentsArray.count];
for (postComment *comment in newsFeed.commentsArray) {
commentSection *commentView = [[[NSBundle mainBundle] loadNibNamed:@"TextComment"
owner:nil options:nil] objectAtIndex:0];
commentView.editCommentView.txtComment.delegate = self;
commentView.tag = 1000+[newsFeed.commentsArray indexOfObject:comment];
commentView.userInteractionEnabled = YES;
[commentView.profileImg setupImageViewer];
commentView.profileName.text = comment.UserName;
[self commentNameSettingWithLabel:(TTTAttributedLabel *)commentView.profileName];
CGSize stringsize = [comment.UserName sizeWithFont:[UIFont boldSystemFontOfSize:14]];
[commentView.profileName setFrame:CGRectMake(commentView.profileName.frame.origin.x, commentView.profileName.frame.origin.y, stringsize.width+5, commentView.profileName.frame.size.height)];
commentView.profileName.shadowColor = [UIColor clearColor];
commentView.lblComentTime.frame = CGRectMake(commentView.profileName.frame.origin.x+commentView.profileName.frame.size.width, commentView.lblComentTime.frame.origin.y, commentView.lblComentTime.frame.size.width, commentView.lblComentTime.frame.size.height);
//Set comment time here
commentView.lblComentTime.text = [self timeLeftSinceDate:[self getDateFromString:comment.created_at]];
[commentView.profileImg sd_setImageWithURL:[NSURL URLWithString:comment.UserProfileImg] placeholderImage:[UIImage imageNamed:@"avatar"]];
if (comment.isEditComment) {
//Add edit view for edit comment
commentView.editCommentView.txtComment.layer.borderColor = [UIColor lightGrayColor].CGColor;
commentView.editCommentView.txtComment.layer.borderWidth = 0.5;
commentView.lblCommentText.hidden = YES;
commentView.editCommentView.hidden = NO;
commentView.editCommentView.txtComment.editable = YES;
commentView.lblCommentText.text = comment.text;
commentView.editCommentView.txtComment.text = comment.text;
CGSize comentRect =[commentView.lblCommentText suggestedFrameSizeToFitEntireStringConstrainedToWidth:commentView.frame.size.width];
[commentView.editCommentView.txtComment setFrame:CGRectMake(commentView.editCommentView.txtComment.frame.origin.x, commentView.editCommentView.txtComment.frame.origin.y, commentView.editCommentView.txtComment.frame.size.width, comentRect.height+15)];
[commentView.editCommentView setFrame:CGRectMake(commentView.editCommentView.frame.origin.x, commentView.editCommentView.frame.origin.y, commentView.editCommentView.frame.size.width, commentView.editCommentView.txtComment.frame.origin.y+commentView.editCommentView.txtComment.frame.size.height)];
[commentView setFrame:CGRectMake(self.sharingSection.frame.origin.x, commenty, self.frame.size.width, commentView.editCommentView.frame.origin.y+commentView.editCommentView.frame.size.height+10)];
commentView.editCommentView.userInteractionEnabled = YES;
if(commentView.editCommentView.txtComment.isEditable){
[commentView.editCommentView.txtComment becomeFirstResponder];
}
}else{
commentView.lblCommentText.hidden = NO;
commentView.editCommentView.hidden = YES;
commentView.lblCommentText.text = comment.text;
CGSize comentRect =[commentView.lblCommentText suggestedFrameSizeToFitEntireStringConstrainedToWidth:commentView.frame.size.width-20];
//coment Frame setting
[commentView.lblCommentText setFrame:CGRectMake(commentView.lblCommentText.frame.origin.x, commentView.lblCommentText.frame.origin.y, commentView.lblCommentText.frame.size.width, comentRect.height)];
[commentView setFrame:CGRectMake(self.sharingSection.frame.origin.x, commenty, self.frame.size.width, commentView.lblCommentText.frame.origin.y+commentView.lblCommentText.frame.size.height+10)];
}
commentView.userInteractionEnabled = YES;
if (comment.commentUserid == [ConnectionManager getInstance].userProfile.user_id) {
//long Press for edit or deleted comment
UILongPressGestureRecognizer *cmttap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(commentEditOrDelete:)];
[commentView addGestureRecognizer:cmttap];
}
commenty = commentView.frame.origin.y+commentView.frame.size.height;
commentView.sepratorView.frame = CGRectMake(0, commentView.frame.size.height-1, commentView.frame.size.width, 1);
[self.commentSectionContainer addSubview:commentView];
self.commentSectionContainer.frame = CGRectMake(self.commentSectionContainer.frame.origin.x, yPosition, self.frame.size.width, commentView.frame.origin.y+commentView.frame.size.height);
[self setCommentLabelFontwitLabel:commentView.lblCommentText];
}
yPosition = self.commentSectionContainer.frame.origin.y+self.commentSectionContainer.frame.size.height;
}else{
self.commentSectionContainer.hidden = YES;
self.lblComments.text = @"0";
}
//***************************Post Comment Section ************************************
self.txtComentView.text = @"";
//self.postCommentSection.layer.borderWidth = 1.0;
//self.postCommentSection.layer.borderColor = [UIColor redColor].CGColor;
self.postCommentSection.frame = CGRectMake(self.postCommentSection.frame.origin.x, yPosition, self.postCommentSection.frame.size.width, self.postCommentSection.frame.size.height);
yPosition = self.postCommentSection.frame.origin.y+self.postCommentSection.frame.size.height;
self.cellTotalHeight = yPosition;
[self setTweetLabelFont];
}
答案 0 :(得分:0)
最后通过为iPhone 5,6和6 Plus创建3个不同的xib文件解决了这个问题。