首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在iOS7中UILabel有一个奇怪的灰色顶线/边框,我如何删除它?

在iOS7中UILabel有一个奇怪的灰色顶线/边框,我如何删除它?
EN

Stack Overflow用户
提问于 2014-01-12 02:06:04
回答 10查看 4.3K关注 0票数 20

我有一个在UITableViewCell中使用的基本UILabel。我遇到了一些奇怪的行为,在一些单元格(不是所有)上,UILabel的顶部边框是灰色的,见下图。我不知道该怎么解决它。

我这样创建我的UILabel:

代码语言:javascript
复制
if (self.postText == nil) {
    CGRect postTextRect = CGRectMake(self.profileImage.frame.origin.x + self.profileImage.frame.size.width + 5, self.username.frame.origin.y + self.username.frame.size.height, frame.size.width - self.profileImage.frame.size.width - self.profileImage.frame.origin.y -10, self.frame.size.height - self.username.frame.size.height - self.username.frame.origin.y + 40);
    self.postText = [[UILabel alloc] initWithFrame:postTextRect];
    self.postText.backgroundColor = [UIColor whiteColor];
    self.postText.textColor = [UIColor darkGrayColor];
    self.postText.userInteractionEnabled = NO;
    self.postText.numberOfLines = 0;
    [self.containerView addSubview:self.postText];
}

有什么办法解决这个问题吗?

更新

我的cellForRowAtIndexPath看起来像这样:

代码语言:javascript
复制
- (id)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [...]
    static NSString *postCellIdentifier = @"PostCell";
    PostCell *cell = [tableView dequeueReusableCellWithIdentifier:postCellIdentifier];
    if (cell == nil) {
        cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:postCellIdentifier];
    }

    [self configureCell:cell atIndexPath:indexPath forPost:cellPost];
    return cell;
}

configureCell的相关par如下:

代码语言:javascript
复制
- (void)configureCell:(PostCell *)cell atIndexPath:(NSIndexPath *)indexPath forPost:(Post *)post
{
    [...]

    cell.username.text = cellUser.username;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    CGSize maximumLabelSize = CGSizeMake(cell.postText.frame.size.width, FLT_MAX);
    CGSize expectedLabelSize = [cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
    CGRect newFrame = cell.postText.frame;
    newFrame.size.height = expectedLabelSize.height;
    cell.postText.frame = newFrame;

    CGRect containerRect = CGRectMake(5, 5, cell.containerView.frame.size.width, cell.postText.frame.size.height + cell.username.frame.origin.y + cell.username.frame.size.height + 10);
    if (containerRect.size.height < 65) {
        containerRect.size.height = 65;
    }

    cell.containerView.frame = containerRect;
    [...] 
}
EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2014-04-15 20:00:57

我也有同样的问题,并通过四舍五入的高度解决了它:

代码语言:javascript
复制
newFrame.size.height = ceilf(expectedLabelSize.height);
票数 40
EN

Stack Overflow用户

发布于 2014-02-27 20:32:08

得到了同样的问题

通过将边框颜色和宽度分配给UILabel来解决

nameLabel.layer.borderColor = [[UIColor whiteColor]CGColor]; nameLabel.layer.borderWidth = 2.0f;

票数 4
EN

Stack Overflow用户

发布于 2015-03-06 15:46:37

一定是iOS的bug。该错误由标签的浮动高度值触发。因此,解决该问题的简单且无副作用的方法是将浮动高度值转换为整数值:

代码语言:javascript
复制
(int)(label.frame.size.height)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21065921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档