0

I have an array of CGPoints. I need a personal label for every fourth point in array, so I need to create several NSTextFields programmatically. I can add points with mouse clicks and can create as many points as I wish. Labels for these points must be all active to show text for user simultaneously. How can I do it?

(macOS, Xcode 7, Swift 2)

Here's my code:

import Cocoa
@NSApplicationMain

class AppDelegate: NSObject, NSApplicationDelegate {
    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var drawView: DrawView!
    @IBOutlet weak var view: NSView!
    let label = NSTextField(frame: NSMakeRect(0,0,100,50))
    var pointsArray: [CGPoint] = []

    func applicationWillUpdate(aNotification: NSNotification) {
        label.backgroundColor = NSColor.clearColor()
        label.bezeled = false
        label.stringValue = "\(pointsArray.count/4)"


        var multiple = (1...25).map { _ in label }


        for index in 0..<(pointsArray.count/4) {

            let point = CGPoint(x: pointsArray[index*4].x, y: pointsArray[index*4].y)
            label.frame = CGRect(origin: point, size: CGSize(width: label.bounds.width, height: label.bounds.height))

            let sticker = multiple[index]
            view.addSubview(sticker)
        }
    }
}

At runtime I see only one label but I need to see several labels simultaneously (on every fourth CGPoint). If I have 100 CGPoints I must have 25 labels.

2 Answers 2

2

I see only one label

Now that I've straightened out your curly braces and indentation, it's easy to see why. Your loop is incorrectly constructed, so that you create one label and change its frame four times. You need to create four separate labels with four separate frames.

Sign up to request clarification or add additional context in comments.

3 Comments

Think how you would tell a child to do it. Make a label, put it in the interface, give it a frame. Loop again: make another label, put it in the interface, give it a frame — a different frame. Loop again... Do you see? The one loop needs to embrace that entire set of things.
Then don't use a loop at first. Just practice by making one text field and putting it in the interface. Then make one text field and put it in the interface and then make another test field and put it in the interface. In other words, just make two text fields and put them both in the interface, in different places. If you can learn to do that, it will be obvious how to re-express that as a loop, because you can see what is in common and what differs.
Thanx Matt, I made it.
0

Code for creating several NSTextFields:

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var drawView: DrawView!
    var pointsArray: [CGPoint] = []

    var label1 = NSTextField(frame: NSMakeRect(0,0,100,50))
    var label2 = NSTextField(frame: NSMakeRect(0,0,100,50))
    var label3 = NSTextField(frame: NSMakeRect(0,0,100,50))
    // ....................................................
    var label25 = NSTextField(frame: NSMakeRect(0,0,100,50))

    override func awakeFromNib() {
        super.awakeFromNib()
    
        var labelArray = [label1, label2, label3, ....., label25]
    
        for i in 0 ..< (pointsArray.count / 4) {

            labelArray[i].backgroundColor = NSColor.clearColor()
            labelArray[i].bezeled = false
            labelArray[i].stringValue = "\(i + 1)"
        
            let point = CGPoint(x: (pointsArray[i * 4].x), 
                                y: (pointsArray[i * 4].y))
        
            var originPoint: [CGPoint] = []
            originPoint.append(point)
        
            labelArray[i].frame = .init(origin: originPoint[0],
                            size: CGSize(width: labelArray[i].bounds.width,
                                        height: labelArray[i].bounds.height))
            self.view.addSubview(labelArray[i])
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.