2 // HMStrokeTextFieldCell.m
5 // Created by Hori,Masaki on 2014/04/19.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMStrokeTextFieldCell.h"
11 const CGFloat boarderWidth = 2.0;
13 @interface HMStrokeTextFieldCell ()
14 @property (strong, nonatomic) NSLayoutManager *layoutManager;
15 @property (strong, nonatomic) NSTextContainer *textContainer;
18 @implementation HMStrokeTextFieldCell
20 - (id)initWithCoder:(NSCoder *)aDecoder
22 self = [super initWithCoder:aDecoder];
24 _layoutManager = [NSLayoutManager new];
25 _textContainer = [NSTextContainer new];
26 [self.layoutManager addTextContainer:self.textContainer];
31 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
33 NSAttributedString *attributedString = self.attributedStringValue;
34 NSDictionary *attribute = [attributedString attributesAtIndex:0 effectiveRange:NULL];
35 NSColor *forgroundColor = [attribute objectForKey:NSForegroundColorAttributeName];
36 if(!forgroundColor) return;
37 if([forgroundColor isEqual:[NSColor controlTextColor]]) {
38 [super drawInteriorWithFrame:cellFrame inView:controlView];
42 NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:attributedString.string attributes:attribute];
43 [textStorage addLayoutManager:self.layoutManager];
45 NSRange range = [self.layoutManager glyphRangeForTextContainer:self.textContainer];
46 NSGlyph glyph[range.length];
47 NSUInteger glyphLength = [self.layoutManager getGlyphs:glyph range:range];
49 NSFont *font = [attribute objectForKey:NSFontAttributeName];
50 NSPoint point = {boarderWidth,0};
51 point.y -= [font descender];
52 if([controlView isFlipped]) {
53 point.y -= [controlView frame].size.height;
56 NSBezierPath *path = [NSBezierPath new];
57 [path moveToPoint:point];
58 [path appendBezierPathWithGlyphs:glyph count:glyphLength inFont:font];
59 [path setLineWidth:boarderWidth];
60 [path setLineJoinStyle:NSRoundLineJoinStyle];
61 if([controlView isFlipped]) {
62 NSAffineTransform *affineTransform = [NSAffineTransform transform];
63 [affineTransform scaleXBy:1 yBy:-1];
64 [path transformUsingAffineTransform:affineTransform];
67 [[NSColor blackColor] set];