How to make a grid background pattern for NSView? - objective-c

is there an easy way to get this grid background? Or do I have to do it something like this [NSColor colorWithPatternImage:[NSImage ...]]?
I don't want complete code. I just want to know if there is an easy way to do that and if yes how.

This is my solution:
- (void)drawRect:(NSRect)dirtyRect
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
[[NSColor whiteColor] setFill];
CGContextFillRect(context, dirtyRect);
for (int i = 1; i < [self bounds].size.height / 10; i++) {
if (i % 10 == 0) {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.3] set];
} else if (i % 5 == 0) {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.2] set];
} else {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.1] set];
}
[NSBezierPath strokeLineFromPoint:NSMakePoint(0, i * 10 - 0.5) toPoint:NSMakePoint([self bounds].size.width, i * 10 - 0.5)];
}
for (int i = 1; i < [self bounds].size.width / 10; i++) {
if (i % 10 == 0) {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.3] set];
} else if (i % 5 == 0) {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.2] set];
} else {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.1] set];
}
[NSBezierPath strokeLineFromPoint:NSMakePoint(i * 10 - 0.5, 0) toPoint:NSMakePoint(i * 10 - 0.5, [self bounds].size.height)];
}
}

The same but for Swift 4
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
if let context = NSGraphicsContext.current?.cgContext {
NSColor.white.setFill()
context.fill(dirtyRect)
context.flush()
}
for i in 1...(Int(self.bounds.size.height) / 10) {
if i % 10 == 0 {
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
}else if i % 5 == 0 {
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
}else{
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLine(from: CGPoint(x: 0, y: CGFloat(i) * 10 - 0.5), to: CGPoint(x: self.bounds.size.width, y: CGFloat(i) * 10 - 0.5))
}
for i in 1...(Int(self.bounds.size.width) / 10) {
if i % 10 == 0 {
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
}else if i % 5 == 0 {
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
}else{
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLine(from: CGPoint(x: CGFloat(i) * 10 - 0.5, y:0), to: CGPoint(x: CGFloat(i) * 10 - 0.5, y: self.bounds.size.height))
}
}

I don't think a pattern color is a good solution here, especially since you need to vary the lines. Instead use an NSBezierPath and moveToPoint/lineToPoint pairs for each horizontal and vertical line. You can then draw the grid in one call. Do extra steps for those lines with a different color (alpha) and/or width (i.e. don't add the thicker lines to the main grid path but create a separate one for them).

Just have a look at the Sketch example that comes with every Xcode installation or is available as a separate download, too.
It features a grid implementation (and many other useful demonstrations of Cocoa techniques..) allowing for zooming, etc.

Porting Methanol's solution for swift and resolving error in MacOS 10.10 onwards.
A simple static function could be used and called from drawRect()
static func makeGridBackground(dirtyRect: NSRect, view: NSView){
//view.print("WMEditorUtils: initiated drawing")
//Fill background with white color
if let context = NSGraphicsContext.currentContext()?.CGContext {
NSColor.whiteColor().setFill()
CGContextFillRect(context, dirtyRect)
CGContextFlush(context)
}
//Draw Lines: Horizontal
for var i:Int = 1; i < (Int)(view.bounds.size.height / 10); i++ {
if (i % 10 == 0) {
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.3).set()
}
else if (i % 5 == 0) {
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.2).set()
}
else{
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLineFromPoint(NSMakePoint(0, (CGFloat)(i * 10) - 0.5), toPoint: NSMakePoint(view.bounds.size.width, (CGFloat)(i * 10) - 0.5))
}
//Draw Lines: Vertical
for var i:Int = 1; i < (Int)(view.bounds.size.width / 10); i++ {
if (i % 10 == 0) {
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.3).set()
}
else if (i % 5 == 0) {
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.2).set()
}
else{
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLineFromPoint(NSMakePoint((CGFloat)(i * 10) - 0.5, 0), toPoint: NSMakePoint((CGFloat)(i * 10) - 0.5, view.bounds.size.width))
}
}

The same of "sad tuna" but in Swift 3
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
if let context = NSGraphicsContext.currentContext()?.CGContext {
NSColor.whiteColor().setFill()
CGContextFillRect(context, dirtyRect)
CGContextFlush(context)
}
for i in 1...(Int(self.bounds.size.height) / 10) {
if i % 10 == 0 {
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
}else if i % 5 == 0 {
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
}else{
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLineFromPoint(CGPointMake(0, CGFloat(i) * 10 - 0.5), toPoint: CGPointMake(self.bounds.size.width, CGFloat(i) * 10 - 0.5))
}
for i in 1...(Int(self.bounds.size.width) / 10) {
if i % 10 == 0 {
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
}else if i % 5 == 0 {
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
}else{
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLineFromPoint(CGPointMake(CGFloat(i) * 10 - 0.5, 0), toPoint: CGPointMake(CGFloat(i) * 10 - 0.5, self.bounds.size.height))
}
}

Related

CGContextSetFill with gradient

I currently have a function where I draw a QR code
+ (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size {}
Currently, I can set the color including the colorspace using the following:
CGContextSetFillColorWithColor(ctx, [theColor colorUsingColorSpaceName:NSCalibratedWhiteColorSpace].CGColor);
I am trying to implement the ability to create a gradient I've converted two NSColors to an NSGradient - there doesn't appear to be any way of getting a gradient as an NSColor so I'm wondering what would be the best way of setting the fill of the context with a gradient?
Thanks in advance for any suggestions!
CGContext has drawLinearGradient
https://developer.apple.com/documentation/coregraphics/cgcontext/1454782-drawlineargradient
and drawRadialGradient
https://developer.apple.com/documentation/coregraphics/cgcontext/1455923-drawradialgradient
To use them to fill a shape, put the shape's path into the context, then use the shape as a clipping path before drawing the gradient.
Here's a drawRect from a view that demonstrates the technique:
- (void) drawRect: (CGRect) rect {
CGColorRef myColors[3] = {
[[UIColor redColor] CGColor],
[[UIColor yellowColor] CGColor],
[[UIColor blueColor] CGColor] };
CGFloat locations[3] = { 0.0, 0.25, 1.0 };
CGRect circleRect = CGRectInset([self bounds], 20, 20);
CGFloat circleRadius = circleRect.size.width / 2.0;
CGContextRef cgContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(cgContext);
CGContextSetFillColorWithColor(cgContext, [[UIColor blackColor] CGColor]);
CGContextFillRect(cgContext, [self bounds]);
CGContextRestoreGState(cgContext);
CGContextSaveGState(cgContext);
CGContextAddEllipseInRect(cgContext, circleRect);
CGContextClip(cgContext);
CGContextTranslateCTM(cgContext, CGRectGetMidX(circleRect), CGRectGetMidY(circleRect));
CGContextRotateCTM(cgContext, M_PI / 3.0);
CFArrayRef colors = CFArrayCreate(nil, (const void**) myColors, 3, &kCFTypeArrayCallBacks);
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colors, locations);
CFRelease(colors);
CFRelease(colorSpace);
CGContextDrawLinearGradient(cgContext, gradient, CGPointMake(-circleRadius, 0),
CGPointMake(circleRadius, 0), kCGGradientDrawsAfterEndLocation + kCGGradientDrawsBeforeStartLocation);
CFRelease(gradient);
CGContextRestoreGState(cgContext);
}
Here's an equivalent playground in Swift.
import UIKit
import PlaygroundSupport
class CircleGradientView : UIView {
override func draw(_ rect: CGRect) {
let colors = [
UIColor.red.cgColor,
UIColor.yellow.cgColor, // Yellow
UIColor.blue.cgColor // Blue
]
let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
let locations : [CGFloat] = [0.0, 0.25, 1.0]
let gradient = CGGradient(colorsSpace: colorSpace,
colors: colors as CFArray,
locations: locations)
if let context = UIGraphicsGetCurrentContext() {
let circleRect = self.bounds.insetBy(dx: 20, dy: 20)
let circleRadius = circleRect.width / 2.0
context.saveGState()
context.addEllipse(in: circleRect)
context.clip()
context.translateBy(x: circleRect.midX, y: circleRect.midY)
context.rotate(by: .pi / 3.0)
context.drawLinearGradient(gradient!,
start: CGPoint(x: -circleRadius, y: 0),
end: CGPoint(x: circleRadius, y: 0),
options: [.drawsAfterEndLocation, .drawsBeforeStartLocation])
context.restoreGState()
}
}
}
let myView = CircleGradientView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
PlaygroundSupport.PlaygroundPage.current.liveView = myView

Convert Mojave Accent colour to RGBA

At a project with 10.14 as macOS deployment target try the following code:
let colour = NSColor.controlAccentColor
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
colour.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
print("RGBA = (\(red), \(green), \(blue), \(alpha))")
The call to getRed freezes and print never gets control.
Need conversion to RGB colour space:
let colour = NSColor.controlAccentColor
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
colour.usingColorSpace(.sRGB)!.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
print("RGBA = (\(red), \(green), \(blue), \(alpha))") // Works OK

Two routines one in objective C, the other in Swift same code essentially different results

iOS 10.2 Swift 3.0
Trying to convert this code in objective C to Swift 3.0 and don't know the core graphics library too well.
#interface SPGripViewBorderView : UIView
#end
#implementation SPGripViewBorderView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Clear background to ensure the content view shows through.
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// (1) Draw the bounding box.
CGContextSetLineWidth(context, 1.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextAddRect(context, CGRectInset(self.bounds, kSPUserResizableViewInteractiveBorderSize/2, kSPUserResizableViewInteractiveBorderSize/2));
CGContextStrokePath(context);
// (2) Calculate the bounding boxes for each of the anchor points.
CGRect upperLeft = CGRectMake(0.0, 0.0, kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize);
CGRect upperRight = CGRectMake(self.bounds.size.width - kSPUserResizableViewInteractiveBorderSize, 0.0, kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize);
CGRect lowerRight = CGRectMake(self.bounds.size.width - kSPUserResizableViewInteractiveBorderSize, self.bounds.size.height - kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize);
CGRect lowerLeft = CGRectMake(0.0, self.bounds.size.height - kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize);
CGRect upperMiddle = CGRectMake((self.bounds.size.width - kSPUserResizableViewInteractiveBorderSize)/2, 0.0, kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize);
CGRect lowerMiddle = CGRectMake((self.bounds.size.width - kSPUserResizableViewInteractiveBorderSize)/2, self.bounds.size.height - kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize);
CGRect middleLeft = CGRectMake(0.0, (self.bounds.size.height - kSPUserResizableViewInteractiveBorderSize)/2, kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize);
CGRect middleRight = CGRectMake(self.bounds.size.width - kSPUserResizableViewInteractiveBorderSize, (self.bounds.size.height - kSPUserResizableViewInteractiveBorderSize)/2, kSPUserResizableViewInteractiveBorderSize, kSPUserResizableViewInteractiveBorderSize);
// (3) Create the gradient to paint the anchor points.
CGFloat colors [] = {
0.4, 0.8, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0
};
CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
CGColorSpaceRelease(baseSpace), baseSpace = NULL;
// (4) Set up the stroke for drawing the border of each of the anchor points.
CGContextSetLineWidth(context, 1);
CGContextSetShadow(context, CGSizeMake(0.5, 0.5), 1);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
// (5) Fill each anchor point using the gradient, then stroke the border.
CGRect allPoints[8] = { upperLeft, upperRight, lowerRight, lowerLeft, upperMiddle, lowerMiddle, middleLeft, middleRight };
for (NSInteger i = 0; i < 8; i++) {
CGRect currPoint = allPoints[i];
CGContextSaveGState(context);
CGContextAddEllipseInRect(context, currPoint);
CGContextClip(context);
CGPoint startPoint = CGPointMake(CGRectGetMidX(currPoint), CGRectGetMinY(currPoint));
CGPoint endPoint = CGPointMake(CGRectGetMidX(currPoint), CGRectGetMaxY(currPoint));
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGContextStrokeEllipseInRect(context, CGRectInset(currPoint, 1, 1));
}
CGGradientRelease(gradient), gradient = NULL;
CGContextRestoreGState(context);
}
#end
Which I re-coded as this ...
import UIKit
class GripViewBorderView: UIView {
let kUserResizableViewInteractiveBorderSize:CGFloat = 10.0
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func draw(_ rect:CGRect) {
let context = UIGraphicsGetCurrentContext();
context!.saveGState();
// (1) Draw the bounding box.
context!.setLineWidth(1.0);
context!.setStrokeColor(UIColor.blue.cgColor)
//CGContextAddRect(context!, CGRectInset(self.bounds, (kSPUserResizableViewInteractiveBorderSize / 2.0), (kSPUserResizableViewInteractiveBorderSize / 2.0)));
context!.addRect(self.bounds.insetBy(dx: (kUserResizableViewInteractiveBorderSize / 2.0), dy: (kUserResizableViewInteractiveBorderSize / 2.0)))
context!.strokePath();
// (2) Calculate the bounding boxes for each of the anchor points.
let upperLeft = CGRect(x: 0.0, y: 0.0, width: kUserResizableViewInteractiveBorderSize, height: kUserResizableViewInteractiveBorderSize)
let upperRight = CGRect(x: self.bounds.size.width - kUserResizableViewInteractiveBorderSize, y: 0.0, width: kUserResizableViewInteractiveBorderSize, height: kUserResizableViewInteractiveBorderSize);
let lowerRight = CGRect(x: self.bounds.size.width - kUserResizableViewInteractiveBorderSize, y: self.bounds.size.height - kUserResizableViewInteractiveBorderSize, width: kUserResizableViewInteractiveBorderSize, height: kUserResizableViewInteractiveBorderSize);
let lowerLeft = CGRect(x: 0.0, y: self.bounds.size.height - kUserResizableViewInteractiveBorderSize, width: kUserResizableViewInteractiveBorderSize, height: kUserResizableViewInteractiveBorderSize);
let upperMiddle = CGRect(x: (self.bounds.size.width - kUserResizableViewInteractiveBorderSize)/2, y: 0.0, width: kUserResizableViewInteractiveBorderSize, height: kUserResizableViewInteractiveBorderSize);
let lowerMiddle = CGRect(x: (self.bounds.size.width - kUserResizableViewInteractiveBorderSize)/2, y: self.bounds.size.height - kUserResizableViewInteractiveBorderSize, width: kUserResizableViewInteractiveBorderSize, height: kUserResizableViewInteractiveBorderSize);
let middleLeft = CGRect(x: 0.0, y: (self.bounds.size.height - kUserResizableViewInteractiveBorderSize)/2, width: kUserResizableViewInteractiveBorderSize, height: kUserResizableViewInteractiveBorderSize);
let middleRight = CGRect(x: self.bounds.size.width - kUserResizableViewInteractiveBorderSize, y: (self.bounds.size.height - kUserResizableViewInteractiveBorderSize)/2, width: kUserResizableViewInteractiveBorderSize, height: kUserResizableViewInteractiveBorderSize);
// (3) Create the gradient to paint the anchor points.
let baseSpace = CGColorSpaceCreateDeviceRGB()
//let gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2)
let colors = [UIColor.red.cgColor,
UIColor.yellow.cgColor]
let gradient = CGGradient(colorsSpace: baseSpace, colors: colors as CFArray, locations: nil)
// (4) Set up the stroke for drawing the border of each of the anchor points.
context!.setLineWidth(1)
context!.setShadow(offset: CGSize(width: 0.5, height: 0.5), blur: 1)
context!.setStrokeColor(UIColor.white.cgColor)
// (5) Fill each anchor point using the gradient, then stroke the border.
let allPoints:[CGRect] = [ upperLeft, upperRight, lowerRight, lowerLeft, upperMiddle, lowerMiddle, middleLeft, middleRight ];
for i in 0 ... 7 {
let currPoint = allPoints[i]
context!.saveGState()
context!.addEllipse(in: currPoint)
context!.clip()
let startPoint = CGPoint(x: currPoint.midX, y: currPoint.minY);
let endPoint = CGPoint(x: currPoint.midX, y: currPoint.maxY);
context!.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: .init(rawValue: 0))
context!.saveGState()
context!.strokeEllipse(in: currPoint.insetBy(dx: 1, dy: 1))
}
context!.restoreGState()
}
}
But I missed something cause the OOC is on the left and my new Swift on the right. For reasons I cannot figure I get only a single dot drawn in my version, the OOC version draws 8 dots [correctly].
You have written context!.saveGState() at the end of the for loop in place of CGContextRestoreGState(context). It should, of course, be context!.restoreGState(). Simple typo!
Also, please remove all of the semi-colons, and use guard let context = UIGraphicsGetCurrentContext() else { return } to get rid of all those forced unwrappings!

Objective-C | NSBezierPath draw and color on action

I have a custom view class with a tracking area. I want that, when the mouse enters the tracking area, a bezier is drawn with a color, and when the mouse exits the area the bezier disappear. In order to make it disappear I read that the only way is to change its color with the window background color.
I managed to add the tracking area, but I don't know how to draw the bezier. If I put the code in
-(void)drawRect:(NSRect)dirtyRect
it's drawn when the app launches, but I don't want so. I tried with this:
#implementation MSBezier
- (void) viewWillMoveToWindow:(NSWindow *)newWindow {
// Setup a new tracking area when the view is added to the window.
NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:NSMakeRect(164.5, 17.5, 270, 65) options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}
- (void) mouseEntered:(NSEvent*)theEvent {
NSLog(#"Entered");
color = [NSColor colorWithCalibratedRed: 0.044 green: 0.813 blue: 0.044 alpha: 0.441];
CGFloat rectangleCornerRadius = 31;
NSRect rectangleRect = NSMakeRect(164.5, 17.5, 270, 65);
NSRect rectangleInnerRect = NSInsetRect(rectangleRect, rectangleCornerRadius, rectangleCornerRadius);
rectanglePath = NSBezierPath.bezierPath;
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 180 endAngle: 270];
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 270 endAngle: 360];
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMaxY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 0 endAngle: 90];
[rectanglePath lineToPoint: NSMakePoint(NSMinX(rectangleRect), NSMaxY(rectangleRect))];
[rectanglePath closePath];
[color setStroke];
[rectanglePath setLineWidth: 3];
[rectanglePath stroke];
}
- (void) mouseExited:(NSEvent*)theEvent {
NSLog(#"Exited");
color = [NSColor colorWithCalibratedRed: 0.949 green: 0.949 blue: 0.949 alpha: 1];
CGFloat rectangleCornerRadius = 31;
NSRect rectangleRect = NSMakeRect(164.5, 17.5, 270, 65);
NSRect rectangleInnerRect = NSInsetRect(rectangleRect, rectangleCornerRadius, rectangleCornerRadius);
rectanglePath = NSBezierPath.bezierPath;
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 180 endAngle: 270];
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 270 endAngle: 360];
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMaxY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 0 endAngle: 90];
[rectanglePath lineToPoint: NSMakePoint(NSMinX(rectangleRect), NSMaxY(rectangleRect))];
[rectanglePath closePath];
[color setStroke];
[rectanglePath setLineWidth: 3];
[rectanglePath stroke];
}
#end
But the bezier isn't drawn.
Thanks for your help!
EDIT
#uchuugaka
This is the code so far, which doesn't seem to do anything:
#implementation MSBezier
bool shouldDrawMyPath = YES;
NSBezierPath *rectanglePath;
- (void)viewWillDraw {
if (shouldDrawMyPath == YES) {
NSColor *color = [NSColor colorWithCalibratedRed: 0.044 green: 0.813 blue: 0.044 alpha: 0.441];
CGFloat rectangleCornerRadius = 31;
NSRect rectangleRect = NSMakeRect(164.5, 17.5, 270, 65);
NSRect rectangleInnerRect = NSInsetRect(rectangleRect, rectangleCornerRadius, rectangleCornerRadius);
rectanglePath = NSBezierPath.bezierPath;
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 180 endAngle: 270];
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMinY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 270 endAngle: 360];
[rectanglePath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(rectangleInnerRect), NSMaxY(rectangleInnerRect)) radius: rectangleCornerRadius startAngle: 0 endAngle: 90];
[rectanglePath lineToPoint: NSMakePoint(NSMinX(rectangleRect), NSMaxY(rectangleRect))];
[rectanglePath closePath];
[color setStroke];
[rectanglePath setLineWidth: 3];
} else {
rectanglePath = nil;
}
}
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor clearColor] set];
NSRectFill(self.bounds);
if (shouldDrawMyPath == YES) {
[rectanglePath stroke];
}
}
- (void) viewWillMoveToWindow:(NSWindow *)newWindow {
NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:NSMakeRect(164.5, 17.5, 270, 65) options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways) owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}
- (void) mouseEntered:(NSEvent*)theEvent {
NSLog(#"Entered");
shouldDrawMyPath = YES;
[self setNeedsDisplay:YES];
}
- (void) mouseExited:(NSEvent*)theEvent {
NSLog(#"Exited");
shouldDrawMyPath = NO;
[self setNeedsDisplay:YES];
}
#end
I'm sure I'm doing something wrong.
EDIT 2
I just needed to set the color in drawRect:. So:
-(void)drawRect:(NSRect)dirtyRect {
if (shouldDrawMyPath == YES) {
NSColor *color = [NSColor colorWithCalibratedRed: 0.044 green: 0.813 blue: 0.044 alpha: 0.441];
[color setStroke];
[rectanglePath stroke];
}
}
So this is pretty simple.
You need to rearrange things.
First create a BOOL property like shouldDrawMyPath
this should default to NO.
Next, in mouseEntered:
Set that to YES
Call self setNeedsDisplay:YES
Next, mouseExited:
Set that to NO
Call self setNeedsDisplay:YES
Add an NSBezierPath property.
In viewWillDraw
If shouldDrawMyPath == YES
Set up your bezier path.
Else set it to nil
( I am assuming your view might resize any time)
If your view never resizes, you can create the path once.
In drawRect
First clear the board, especially if your view could resize.
[[NSColor clearColor] set];
NSRectFill(self.bounds);
Draw anything always there if anything.
If shouldDrawMyPath == YES
Fill and/or stroke your path.
Else
Do any other drawing without the path.
As long as you set up your tracking area correctly this should get you going.
Depending on specifics there are always optimizations.
Never draw outside of drawRect unless you know what you're doing.
Keep your drawRect code simple.
Only draw what you need to when you need to. (You're not there yet, but there are a lot of optimizations that can be done to invalidate and only draw particular rects)

Generate a random UIColor

I try to get a random color for UILabel...
- (UIColor *)randomColor
{
int red = arc4random() % 255 / 255.0;
int green = arc4random() % 255 / 255.0;
int blue = arc4random() % 255 / 255.0;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
NSLog(#"%#", color);
return color;
}
And use it:
[mat addAttributes:#{NSForegroundColorAttributeName : [self randomColor]} range:range];
But color is always black. What is wrong?
[UIColor colorWithHue:drand48() saturation:1.0 brightness:1.0 alpha:1.0];
or in Swift:
UIColor(hue: CGFloat(drand48()), saturation: 1, brightness: 1, alpha: 1)
Feel free to randomise or adjust saturation and brightness to your liking.
Because you have assigned the colour values to int variables. Use float
(or CGFloat) instead. Also (as #stackunderflow's said), the remainder must
be taken modulo 256 in order to cover the whole range 0.0 ... 1.0:
CGFloat red = arc4random() % 256 / 255.0;
// Or (recommended):
CGFloat red = arc4random_uniform(256) / 255.0;
Here is a swift version, made into a UIColor extension:
extension UIColor {
class func randomColor(randomAlpha: Bool = false) -> UIColor {
let redValue = CGFloat(arc4random_uniform(255)) / 255.0;
let greenValue = CGFloat(arc4random_uniform(255)) / 255.0;
let blueValue = CGFloat(arc4random_uniform(255)) / 255.0;
let alphaValue = randomAlpha ? CGFloat(arc4random_uniform(255)) / 255.0 : 1;
return UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: alphaValue)
}
}
Swift solution using a class var random:
extension UIColor {
class var random: UIColor {
return UIColor(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1), alpha: 1.0)
}
}
Use just like any other in-built UIColor class variable (.red, .blue, .white, etc.), for example:
view.backgroundColor = .random
try this
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
The following works on iOS 12
NSInteger aRedValue = arc4random()%255;
NSInteger aGreenValue = arc4random()%255;
NSInteger aBlueValue = arc4random()%255;
UIColor *randColor = [UIColor colorWithRed:aRedValue/255.0f green:aGreenValue/255.0f blue:aBlueValue/255.0f alpha:1.0f];
// RGB
UIColor *randomRGBColor = [[UIColor alloc] initWithRed:arc4random()%256/256.0
green:arc4random()%256/256.0
blue:arc4random()%256/256.0
alpha:1.0];
// HSB
UIColor *randomHSBColor = [[UIColor alloc] initWithHue:arc4random()%256/256.0
saturation:(arc4random()%128/256.0)+0.5
brightness:(arc4random()%128/256.0)+0.5
alpha:1.0];
arc4random() % 255 / 255.0 will always be truncated to 0 because arc4random()%255 will be an integer between 0 and 254 inclusive, and dividing by 255.0 and casting to an int will always result in 0. You should save the result as a float instead.
(Also you should use arc4random()%256 if you wish to select randomly from all possible colors.)
Here is a snippet:
CGFloat redLevel = rand() / (float) RAND_MAX;
CGFloat greenLevel = rand() / (float) RAND_MAX;
CGFloat blueLevel = rand() / (float) RAND_MAX;
self.view.backgroundColor = [UIColor colorWithRed: redLevel
green: greenLevel
blue: blueLevel
alpha: 1.0];
Removes duplication of the 3 arc4random lines :)
static func randomColor() -> UIColor {
let random = {CGFloat(arc4random_uniform(255)) / 255.0}
return UIColor(red: random(), green: random(), blue: random(), alpha: 1)
}