How do you activate the Printer Panel using a toolbar printer Icon in a macOS app like you get when I select File -> Print (CMD-P)? This is in a tableView view based screen.
I have connected the first responder to "print:" thinking this might work but the printer Icon in the toolbar remains grayed-out. Also, tried connecting it to a @IBAction func but it still is grayed out.
I am using Xcode 13 and 14 beta
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I would like customize the nstableview alternate row color as light blue instead og light gray.
How is this done?
thanks
Environment: Xcode 14.1 beta 3 on macOS Ventura Beta 9.
No longer need this, I found example of this.
struct LocationsChart: View {
var body: some View {
Chart {
ForEach(seriesData, id: \.city) { series in
ForEach(series.data, id: \.weekday) {
BarMark(
x: .value("Weekday", $0.weekday, unit: .day),
y: .value("Sales", $0.sales)
)
}
.foregroundStyle(by: .value("City", series.city))
.position(by: .value("City", series.city))
}
}
}
let seriesData = [
(
city: "Cupertino", data: [
(weekday: date(year: 2022, month: 5, day: 2), sales: 54),
(weekday: date(year: 2022, month: 5, day: 3), sales: 42),
(weekday: date(year: 2022, month: 5, day: 4), sales: 88),
(weekday: date(year: 2022, month: 5, day: 5), sales: 49),
(weekday: date(year: 2022, month: 5, day: 6), sales: 42),
(weekday: date(year: 2022, month: 5, day: 7), sales: 125),
(weekday: date(year: 2022, month: 5, day: 8), sales: 67)
]
),
(
city: "San Francisco", data: [
(weekday: date(year: 2022, month: 5, day: 2), sales: 81),
(weekday: date(year: 2022, month: 5, day: 3), sales: 90),
(weekday: date(year: 2022, month: 5, day: 4), sales: 52),
(weekday: date(year: 2022, month: 5, day: 5), sales: 72),
(weekday: date(year: 2022, month: 5, day: 6), sales: 84),
(weekday: date(year: 2022, month: 5, day: 7), sales: 84),
(weekday: date(year: 2022, month: 5, day: 8), sales: 137)
]
)
]
}
func date(year: Int, month: Int, day: Int = 1) -> Date {
Calendar.current.date(from: DateComponents(year: year, month: month, day: day)) ?? Date()
}
The find and replace in project is not working for me in Xcode 14.2 on Ventura 13.2. When I enter the find text as well as the replace text the "Replace" and "Replace All" buttons remained grayed out.
Is this a bug with Xcode or am I missing something?
Please advise.
Anybody else having problems with app icon on Xcode 12 not showing up for a macOS app?
I have the following code in a viewController:
extension InvoicesDetailController: NSTableViewDelegate {
fileprivate enum CellIdentifiers {
static let lineItemLabelCell = "lineItemLabelCell"
static let lineItemDescCell = "lineItemDescCell"
static let lineItemAmountCell = "lineItemAmountCell"
static let lineItemQtyCell = "lineItemQtyCell"
}
fileprivate enum fieldIdentifiers {
static let lineItemLabel = "lineItemLabel"
static let lineItemDesc = "lineItemDesc"
static let lineItemAmount = "lineItemAmount"
static let lineItemQty = "lineItemQty"
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) - NSView? {
let currentItem = invoiceItems[row]
var text: String = ""
var cellIdentifier: String = ""
let currentColumn = tableColumn?.identifier.rawValue
if currentColumn == fieldIdentifiers.lineItemLabel {
text = currentItem.lineItemLabel!
cellIdentifier = CellIdentifiers.lineItemLabelCell
}
if currentColumn == fieldIdentifiers.lineItemDesc {
text = currentItem.lineItemDesc!
cellIdentifier = CellIdentifiers.lineItemDescCell
}
if currentColumn == fieldIdentifiers.lineItemAmount {
text = currentItem.lineItemDesc!
cellIdentifier = CellIdentifiers.lineItemAmountCell
}
if currentColumn == fieldIdentifiers.lineItemQty {
text = currentItem.lineItemQty!
cellIdentifier = CellIdentifiers.lineItemQtyCell
}
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellIdentifier), owner: self) as? NSTableCellView {
print(cell.textField?.stringValue)
cell.textField?.stringValue = text
print(text, cell.textField?.stringValue)
return cell
}
return nil
}
I have used this code (similar code in other places and it works fine, however in this case the tableView Cells are not being populated. When I look at this code using debugger the line
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellIdentifier), owner: self) as? NSTableCellView
always sets cell to nil? Since I don't see anything wrong with code I suspect there is an issue with the IB?
Any ideas how to fix or how to debug further?
Is there a way with code I can in a general routine, which gets activated with CMD+P that shows the name of the window that was previously in focus? If so, how is this done?
I have created programmatically a NSTableView with a couple of columns, this table view has 3 columns and I want to set headers for the columns to accomplish the same thing as you get when you selected headers in IB.
I looked for some examples, found a few things but not really work for me. How is this done. It seems like this should be easy to code.
This is an example of one of the examples I found.
let headerCell = CustomTableHeaderCell.init(textCell: "123123")
// let font = NSFont(name: "Arial", size: 22.0)
// headerCell.stringValue = "firstname"
// headerCell.font = font self.tableView.tableColumns[0].headerCell = headerCell
But CustomTableHeaderCell is not found.
How is this done?
Environment is macOS Monterey Xcode 13.
Is there a way to change the behavior of a macOS NSTableView to have the row height change based on the contents of a column. As an example when populating the column data and the width of the content exceeds the width of the column to have the content wrap to an additional line, which of course the row height would have to change then as well.
If so, how can this be done?
Thanks
I am using the code below to learn more about drawing but I am getting an ambiguous error on the line not sure how to resolve this. Please advise how to code this.
Thanks
let context = NSGraphicsContext.current()?.cgContext
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
** let context = NSGraphicsContext.current()?.cgContext**
let length = CGPoint(x: 100, y: 100)
let p1 = CGPoint(x: 200, y: 200)
let shape = "square"
context!.beginPath()
context!.move(to: p1)
if shape == "line" {
let pointEnd = CGPoint(x: p1.x + length.x, y: p1.y + length.y)
context!.addLine(to: pointEnd)
} else if shape == "square" {
let p2 = CGPoint(x: p1.x + length.x, y: p1.y)
let p3 = CGPoint(x: p1.x + length.x, y: p1.y + length.y)
let p4 = CGPoint(x: p1.x, y: p1.y + length.y)
context!.addLine(to: p2)
context!.addLine(to: p3)
context!.addLine(to: p4)
context!.addLine(to: p1)
}
context!.setStrokeColor(red: 1, green: 1, blue: 1, alpha: 1.0)
context!.setFillColor(red: 0, green: 1, blue: 0, alpha: 1)
context!.setLineWidth(2.0)
context!.strokePath()
let textColor = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 1.0)
let textColorB = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 0.0)
let rect = CGRect(x: 200, y: 200, width: 30, height: 130)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attr = [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.backgroundColor: textColorB, NSAttributedString.Key.font:NSFont.init(name: "HelveticaNeue-Thin", size: 14)]
let q: NSString = "hello, world"
let center = CGPoint(x: 0, y: 0)
q.draw(at: center, withAttributes: attr as [NSAttributedString.Key : Any])
}
}
I have the test code below which draws a box with some text in it. I would like to add an image to the drawing. The function getCompanyLogo returns an NSImage and I would like to then add this image to the drawing. I have read apple's archive doc at the Drawing with COCOA but do not see how to complete this.
How is this done?
Thanks
import Cocoa
@IBDesignable class invoiceSectionTop: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let context = NSGraphicsContext.current?.cgContext
let length = CGPoint(x: 200, y: 50)
let p1 = CGPoint(x: 0, y: 50)
let shape = "square"
context!.beginPath()
context!.move(to: p1)
if shape == "line" {
let pointEnd = CGPoint(x: p1.x + length.x, y: p1.y + length.y)
context!.addLine(to: pointEnd)
} else if shape == "square" {
let p2 = CGPoint(x: p1.x + length.x, y: p1.y)
let p3 = CGPoint(x: p1.x + length.x, y: p1.y + length.y)
let p4 = CGPoint(x: p1.x, y: p1.y + length.y)
context!.addLine(to: p2)
context!.addLine(to: p3)
context!.addLine(to: p4)
context!.addLine(to: p1)
}
context!.setStrokeColor(red: 1, green: 1, blue: 1, alpha: 1.0)
context!.setFillColor(red: 0, green: 1, blue: 0, alpha: 1)
context!.setLineWidth(2.0)
context!.strokePath()
let image = getCompanyLogo() // getCompanyLogo returns a NSImage object
// NSGraphicsContext.current?.NSIm.draw(cgimage!, in: dirtyRect)
let textColor = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 1.0)
let textColorB = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 0.0)
let rect = CGRect(x: 200, y: 200, width: 30, height: 130)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attr = [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.backgroundColor: textColorB, NSAttributedString.Key.font:NSFont.init(name: "HelveticaNeue-Thin", size: 14)]
let q: NSString = "hello, world"
let center = CGPoint(x: 0, y: 50)
q.draw(at: center, withAttributes: attr as [NSAttributedString.Key : Any])
}
}
I have a date column in a macOS NSTableView column in YYYY-MM-DD format and when using the code below the sort does not sort way I want. When sorting ascending. It has the year correct but the month is in reverse order. See image below. How can I fix this?
func sortInvoices(invoiceColumn: String, ascending: Bool) {
enum SortOrder {
static let Date = "invoiceDateCreated"
static let Number = "invoicenumber"
static let Customer = "invoicecustomer"
static let Status = "invoicestatus"
}
switch invoiceColumn {
case SortOrder.Date:
invoices.sort { (p1, p2) -> Bool in
guard let id1 = p1.invoiceDateBilled, let id2 = p2.invoiceDateBilled else { return true }
if ascending {
return id1 < id2
} else {
return id2 < id1
}
}
Environment : macOS Ventura Xcode beta 14.1:
I have the following chart I was using to learn about SwifUI Charts:
I wanted to have different scales on the leading and trailing yaxis as well as have the first 3 bars to use leading scale and the last two bars use the trailing scale.
Can this be done? If so, how?
Thanks
I am getting the following error on this line of code
@Query(sort: \.id, order: .reverse) private var artList: [ArtInventory]
Cannot infer key path type from context; consider explicitly specifying a root type
So, I select fix and then the line of code then look like this
@Query(sort: \<#Root#>.id, order: .reverse) private var artList: [ArtInventory]
with this error
Invalid component of Swift key path
Some seems to have changed with the @Query macro in beta 5, which now needs a root type, but not sure how to proceed.
Any ideas how to fix?
I created a SwiftChart as below and I would like to have two YAxis, one for amount and the second for count. So, the amount YAxis is a different scale then the count YAxis.
Does anybody have an example of this or shed some light on coding two different YAxis?
Thanks
ForEach(seriesArt) { series in
ForEach(series.chartSeries.chartEntry) {
BarMark(
x: .value("Tier", $0.tier),
y: .value("Price", $0.keyValue)
)
}
.foregroundStyle(by: .value("Count", series.chartCategory))
.position(by: .value("Price", series.chartCategory))
}
}
.frame(width: 400, height: 200)
.chartXAxis {
AxisMarks(position: .bottom, values: .automatic) {
AxisValueLabel()
.foregroundStyle(Color.white)
}
}
.chartYAxis {
AxisMarks(position: .leading, values: .automatic) { value in
AxisGridLine(centered: true, stroke: StrokeStyle(lineWidth: 1))
AxisValueLabel() {
if let intValue = value.as(Int.self) {
Text("\(intValue)")
.font(.system(size: 10))
.foregroundColor(.white)
}
}
}
.chartYAixs - for count sum by tier which needs to be a different scale from the amount YAxis
}
}
}