【问题标题】:Overlapping Text in UITableViewHeaderFooterViewUITableViewHeaderFooterView 中的重叠文本
【发布时间】:2016-01-20 11:29:01
【问题描述】:

在这里我提到了我写的代码,在 UITableViewHeaderFooterView 中滚动和第二次调用文本的方法重叠(滚动多少时间文本正在添加),这里我提到屏幕截图:http://prntscr.com/9sgq7x

 _imgViewUser.imageURL   = [NSURL URLWithString:modelData.user_image_big];
    _lblUserName.text       = modelData.posted_by;
    _lblTime.text           = modelData.posted_time;

    if ([modelData.commentArray count])
    {
        _imgViewUser.imageURL = [NSURL URLWithString:[[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_user_small"]];
        NSString *nameStr = @"";
        if ([GETVALUE(kUSERID) isEqualToString:[[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_userid"]]) {
            nameStr = @"You";
        }else {
            nameStr = [[modelData.commentArray objectAtIndex:0] valueForKey:@"full_name"];
        }
        _lblUserName.text     = nameStr;
        _lblTime.text         = [[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_post_time"];
    }


    NSString *OriginalFrontendText    = [[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_text"];
    UIFont *font                      = [UIFont fontWithName:@"Arial" size:13.0];
    CGFloat descriptionHeight         = [OriginalFrontendText boundingRectWithSize:CGSizeMake(180, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil].size.height;

    [[self viewWithTag:tag]removeFromSuperview];

    STTweetUserName *commentLbl;
    if ([modelData.comments_count intValue] > 1) {
        commentLbl   = [[STTweetUserName alloc]initWithFrame:CGRectMake(0, 39, self.frame.size.width-10, descriptionHeight) withFont:9.0 attributeTextColor:[UIColor darkGrayColor] name:@"See More"];
       commentLbl.text = [NSString stringWithFormat:@"%@See More",[[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_text"]];
    }else
    {
        commentLbl   = [[STTweetUserName alloc]initWithFrame:CGRectMake(0, 39, self.frame.size.width-10, descriptionHeight) withFont:9.0 attributeTextColor:[UIColor darkGrayColor] name:nil];
        commentLbl.text              = [[modelData.commentArray objectAtIndex:0] valueForKey:@"cmt_text"];
    }
    commentLbl.tag = tag;

    if (commentLbl.tag == tag) {
        [commentLbl removeFromSuperview];
    }
    [self addSubview:commentLbl];

    commentLbl.detectionBlock = ^(STTweetHotUserName hotWord, NSString *string, NSString *protocol, NSRange range){
        showAlert(string, nil, @"OK", nil);
        //[self SeeMoreClicked];
    };
    self.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, 150);

i am getting overlopping while scrolling

【问题讨论】:

  • 您在哪里设置表的页脚视图?是否在某处的 viewWillAppear 方法中?
  • 不,在侧面 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  • 您只是想在页脚视图中设置一个带有一些文本的标签。不是吗?
  • 是文本,但我想在文本结尾添加选项作为查看更多,查看更多它将可触摸

标签: ios objective-c xcode6 xcode7


【解决方案1】:

使用以下代码制作页脚视图

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

   UIView * v = [[UIView alloc] init];
   v.frame = CGRectMake(0, 0, tableView.frame.size.width, 44);

   UILabel * label = [[UILabel alloc] init];
   label.frame = CGRectMake(20, 0, tableView.frame.size.width/2, 44);
   label.text = @"Some text";
   [v addSubview:label];

   UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
   btn.frame = CGRectMake(label.frame.size.width+label.frame.origin.x, 0, tableView.frame.size.width/2, 44);
  [btn addTarget:self action:@selector(seeMore) forControlEvents:UIControlEventTouchUpInside];
  btn.titleLabel.text = @"See more";
  [v addSubview:btn];

   return v;
}

根据需要自定义上述代码。这不会让您的页脚视图与文本重叠。

获取NSString文字的宽度

- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
    CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
    size = CGSizeMake(frame.size.width, frame.size.height+20.0f);
}
return size;
}

【讨论】:

  • 抱歉,Bharath 先生,我没有得到准确的结果。我试过你的代码。我期望从那里完成的文本按钮开始的地方请参考:prntscr.com/9shbta
  • 你用上面的代码解决了你的重叠问题吗?
  • 该代码没有重叠。但我无法设置但准确地设置框架
  • 你能发一些截图添加上面的代码后的样子吗?
  • 所以这里只有按钮放置问题。不是吗?如果是这种情况,请使用 btn.frame 'x' 值。那会帮助你。另请参阅我更新的答案,我添加了一种新方法来查找字符串文本的 CGSize。使用此方法计算页脚视图中标签的宽度。
猜你喜欢
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 2018-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多