Created
June 16, 2011 05:35
-
-
Save micahbrich/1028727 to your computer and use it in GitHub Desktop.
Awesome Text Fields in MacRuby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaddedTextFieldCell < NSTextFieldCell | |
def drawingRectForBounds(cellFrame) | |
super | |
result = cellFrame | |
padding_left = (result.size.width / 16).round # to make sure text is clear | |
padding_top = (result.size.height / 4).round | |
result.origin.x += padding_left | |
result.origin.y += padding_top | |
result.size.width -= (padding_left * 2) | |
result.size.height -= (padding_top * 2) | |
return result | |
end | |
def titleRectForBounds(cellRect) | |
result = cellRect | |
padding_left = (result.size.width / 16).round | |
padding_top = (result.size.height / 4).round | |
result.origin.x += padding_left | |
result.origin.y += padding_top | |
result.size.width -= (padding_left * 2) | |
result.size.height -= (padding_top * 2) | |
return result | |
end | |
def editWithFrame(aRect, inView:controlView, editor:textObj, delegate:anObject, event:theEvent) | |
textFrame = self.titleRectForBounds(aRect) | |
super | |
end | |
def selectWithFrame(aRect, inView:controlView, editor:textObj, delegate:anObject, start:selStart, length:selLength) | |
textFrame = self.titleRectForBounds(aRect) | |
super | |
end | |
def drawsBackground | |
false | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SextTextField < NSTextField | |
def drawRect(frame) | |
# Step 1. Draw a rectangle. | |
rect = NSMakeRect(0.0, 0.0, frame.size.width, frame.size.height - 1.0) | |
path = NSBezierPath.bezierPathWithRoundedRect(rect, xRadius:3.0, yRadius:3.0) | |
# Step 2. Add a shadow. | |
dropShadow = NSShadow.alloc.initWithColor(NSColor.colorWithCalibratedWhite(1.0, alpha:0.35), offset:NSMakeSize(0.0, -1.0), blurRadius:0.0) | |
NSGraphicsContext.saveGraphicsState | |
dropShadow.set | |
path.fill | |
NSGraphicsContext.restoreGraphicsState | |
# Step 3. Background gradient! | |
gradient = NSGradient.alloc.initWithStartingColor(NSColor.colorWithCalibratedWhite(0.506, alpha:1.0), endingColor:NSColor.colorWithCalibratedWhite(0.376, alpha:1.0)) | |
gradient.drawInBezierPath(path, angle:-90) | |
# Step 4. Stroke. | |
strokeColor = NSColor.colorWithCalibratedWhite(0.26, alpha:1.0) | |
strokeColor.setStroke | |
path.strokeInside | |
# Step 5. Inner shadows. | |
innerShadow1 = NSShadow.alloc.initWithColor(NSColor.colorWithCalibratedWhite(0.0, alpha:0.75), offset:NSZeroSize, blurRadius:3.0) | |
innerShadow2 = NSShadow.alloc.initWithColor(NSColor.colorWithCalibratedWhite(0.0, alpha:0.55), offset:NSMakeSize(0.0, -2.0), blurRadius:8.0) | |
path.fillWithInnerShadow(innerShadow1) | |
path.fillWithInnerShadow(innerShadow2) | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a useful reference for customizing an NSTextfield (and it's contained NSTextField Cell). The former is necessary for drawing custom gradients & bezels and whatnot, while the latter is necessary to adding the all-important padding.