gpt4 book ai didi

macos - 当图像插入一行时,NSTextField 在 NSTableView 中变得模糊

转载 作者:行者123 更新时间:2023-12-03 16:35:09 25 4
gpt4 key购买 nike

在我基于 View 的 NSTableView 中,每个 View (除其他外)都有一个 NSTextField 和一个位于文本字段正下方的 NSImageView。

有时,当我在表格顶部插入新行,并且该行的 NSImageView 中有图像时,其他行中的文本会变得模糊/降级。

滚动几次后文字又恢复正常。

不好:

enter image description here

好的:

enter image description here

仅在带有图像的行之后中的文本模糊的示例:

enter image description here

这确实让我认为这是来自 .insertRowsnoteHeightOfRows 的问题。

所有元素都在 IB 中设置了自动布局约束。

ScrollView 在IB中设置了CoreAnimation层。我在准备单元时也使用了该层:

cell.layer?.isOpaque = true
cell.textLabel.layer?.isOpaque = true

有时

cell.layer?.borderColor = someColor
cell.layer?.borderWidth = someWidth

enter image description here

插入行:

// update the model, then:
NSAnimationContext.runAnimationGroup({ (context) in
context.allowsImplicitAnimation = true
self.table.insertRows(at: index, withAnimation: [.effectGap])
})

用图像更新行:

// in tableViewCell, while populating the table
cell.postImage.image = img
cell.postImage.needsDisplay = true

// once the image is downloaded
table.reloadData(forRowIndexes: ..., columnIndexes: ...)
table.noteHeightOfRows(withIndexesChanged: ...)

如何避免这个问题?调试起来很困难,因为它并不总是发生,而且当它发生时我看不出它发生的原因是什么。

最佳答案

由于只有在插入图像时文本才会变得模糊,我的第一个想法是 NSTextField 的框架不是“整数”(其像素值不是整数)。这是文本模糊的众所周知的原因。

您正在使用 Cocoa 的自动布局功能,因此我的建议是重写基于 View 的表格 View 单元格内的 void layout() 方法。

调用它的 super 方法(让 Cocoa 进行所有需要的布局计算),并将 textFiled 的框架设置为相同的框架,但是是完整的。

<小时/>

附录:虽然文本字段本身可能有一个完整的框架 - 它仍然可以放置在“半像素”(在真实屏幕坐标中)上,这仍然会导致模糊。

TextField 是单元格的 subview ,因此它的框架不在屏幕坐标中,而是在单元格坐标中。

示例:假设单元格的框架为 (0.5, 0.5 , 100, 100),该框架不是整数,而 textField 的框架为 (10,10 , 50, 50) 无论如何都是积分!

但是,当文本字段在屏幕上布局时,它将具有以下框架:(10.5, 10.5, 50, 50),这不是完整的 - 并将导致在“半像素”(非整数)会导致文本模糊。

因此,在您的情况下,单元格本身必须布局在一个完整的框架以及文本字段上,以确保文本字段位于屏幕坐标的完整框架上。

<小时/>

在你的tableViewCell子类中:

void layout()
{
[super layout];

//**** We have to ensure that the cell is also layed on an integral frame ****//

[self setFrame:NSIntegralRect(self.frame)];

//At this point after calling the [super layout]; myTextField will have a valid (autolayoutwise) frame so all you have to do is to ensure that it is indeed an integral frame doing so by calling NSIntegralRect(NSRect rect); method of Cocoa framework.
[myTetxField setFrame:NSIntegralRect(myTextField.frame)];

//Maybe optionally you will want to set everything to the integral rect :-)
/*
for(NSView * view in self.subViews)
{
[view setFrame:NSIntegralRect(view.frame)];
}
*/
}

关于macos - 当图像插入一行时,NSTextField 在 NSTableView 中变得模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42486060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com