通过使用自定义表格单元格视图并实现setBackgroundStyle
,可以在基于视图的NSTableView中更改文本颜色:
- (void)setBackgroundStyle: (NSBackgroundStyle)backgroundStyle {
[super setBackgroundStyle: backgroundStyle];
UICoverageElement *element = self.objectValue;
if (backgroundStyle == NSBackgroundStyleEmphasized) {
self.textField.textColor = NSColor.highlightColor;
} else {
if ([element.value isEqualToString: @"<no name>"]) {
self.textField.textColor = NSColor.tertiaryLabelColor;
} else if ([element.value hasPrefix: @"UI"]) {
self.textField.textColor = typeColor;
} else if ([element.value hasPrefix: @"["] || [element.value hasPrefix: @"{"]) {
self.textField.textColor = objectColor;
} else {
self.textField.textColor = NSColor.textColor;
}
}
}
效果很好:
但是在编辑单元格时会引起麻烦。在这种情况下,字段编辑器显然会采用当前手动设置的文本颜色(选定行为白色),并在带有白色背景的字段编辑器中显示该颜色:
现在的问题是:在编辑单元格视图时如何设置正确的文本颜色?
编辑开始时未调用 setBackgroundStyle
,因此无法在此功能中解决该问题。我尝试了各种方法来指示编辑过程的开始,但是没有一个被调用(但是对于独立文本字段,是被调用)。如果我没有设置highlightColor,那么编辑器颜色是正确的,但是所选行的突出显示颜色是错误的。
答案 0 :(得分:1)
老实说,这是您认为非常简单明了的事情之一,但不幸的是事实并非如此。
在字段编辑器中影响颜色的唯一方法是:
a)在调用NSCell的selectWithFrame:...方法之前,将文本字段的颜色设置为所需的颜色 b)更改selectWithFrame:...调用后置于字段编辑器中的文本的颜色。
一般来说:
a)子类化NSTextFieldCell并将字段的文本颜色设置回通常的默认设置 之前。
#Import packages
import urllib.request
from bs4 import BeautifulSoup
import pandas as pd
import csv
#For loop to scrap details of power plants
gas_lst=[]
for i in range(1,46624):
pid=str(i)
url="http://www.globalenergyobservatory.com/form.php?pid=" + pid
page=urllib.request.urlopen(url)
soup=BeautifulSoup(page,'html.parser')
#Distinguish power plants to different types of primary fuel
types=soup.find(id="Type")
power_types=types["value"]
###Breakdown of different units
if power_types=="Gas":
i = 1
while True:
if soup.find(id="unitcheck" + str(i)) == None:
break
else:
gas_unit=soup.find(id="unitcheck" + str(i))
gas_unit_values=gas_unit["value"]
gas_capacity=soup.find(id="Capacity_(MWe)_nbr_" + str(i))
gas_capacity_values=gas_capacity["value"]
gas_commissioned=soup.find(id="Date_Commissioned_dt_" + str(i))
gas_commissioned_date=gas_commissioned["value"]
gas_decommissioned=soup.find(id="Decommission_Date_dt_" + str(i))
gas_decommissioned_date=gas_decommissioned["value"]
gas_HRSG=soup.find(id="Boiler/HRSG_Manufacturer_" + str(i))
gas_HRSG_OEM=gas_HRSG["value"]
gas_turbine=soup.find(id="Turbine_Manufacturer_" + str(i))
gas_turbine_OEM=gas_turbine["value"]
gas_generator=soup.find(id="Generator_Manufacturer_" + str(i))
gas_generator_OEM=gas_generator["value"]
i = i+1
else:
continue
#Gas units breakdowns
gas_lst.append([gas_unit_values,gas_capacity_values,gas_commissioned_date,gas_decommissioned_date,gas_HRSG_OEM,gas_turbine_OEM,gas_generator_OEM])
gas_df=pd.DataFrame(gas_lst)
gas_df.columns=['Unit','Capacity','Date_commissioned','Date_decommissioned','HRSG_manufacturer','Turbine_manufacturer','Generator_manufacturer']
print(pid)
#Convert to csv file
gas_df.to_csv('gas_units_breakdowns.csv',index=False)
b)直接更改字段编辑器
- (void)selectWithFrame:(NSRect)rect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)delegate start:(NSInteger)selStart length:(NSInteger)selLength
{
self.textColorWhenNotEditing = self.textColor;
self.textColor = NSColor.controlTextColor;
[super selectWithFrame:rect inView:controlView editor:textObj delegate:delegate start:selStart length:selLength];
}
- (void)endEditing:(NSText *)textObj
{
[super endEditing:textObj];
self.textColor = self.textColorWhenNotEditing;
}
答案 1 :(得分:0)
我以前回答过一个相关的问题。不确定是否应将其标记为重复项: https://stackoverflow.com/a/54217318/217306
要点在于,文本编辑模式由称为field editor
的单独对象处理。您应该创建一个新实例,并在编辑表时使用它来自定义外观。
NSWindow
的 windowWillReturnFieldEditor
委托方法询问使用哪个编辑器来编辑client
。您只需为表格创建一次这样的编辑器,然后在委托人为您的表格请求编辑器时将其返回。
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)client {
if (/* client is a textfield or subview in your table */) {
// Create customEditor elsewhere once
// Get row number or data that corresponds to client view
// Cusomize customEditor colors accordingly
return customEditor;
}
return nil; // Use default editor
}