OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMStrokeTextFieldCell.m
1 //
2 //  HMStrokeTextFieldCell.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/04/19.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMStrokeTextFieldCell.h"
10
11 const CGFloat boarderWidth = 2.0;
12
13 @interface HMStrokeTextFieldCell ()
14 @property (strong, nonatomic) NSLayoutManager *layoutManager;
15 @property (strong, nonatomic) NSTextContainer *textContainer;
16 @end
17
18 @implementation HMStrokeTextFieldCell
19
20 - (id)initWithCoder:(NSCoder *)aDecoder
21 {
22         self = [super initWithCoder:aDecoder];
23         if(self) {
24                 _layoutManager = [NSLayoutManager new];
25                 _textContainer = [NSTextContainer new];
26                 [self.layoutManager addTextContainer:self.textContainer];
27         }
28         return self;
29 }
30
31 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
32 {
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];
39                 return;
40         }
41         
42         NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:attributedString.string attributes:attribute];
43         [textStorage addLayoutManager:self.layoutManager];
44         
45         NSRange range = [self.layoutManager glyphRangeForTextContainer:self.textContainer];
46         NSGlyph glyph[range.length];
47         NSUInteger glyphLength = [self.layoutManager getGlyphs:glyph range:range];
48         
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;
54         }
55         
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];
65         }
66         
67         [[NSColor blackColor] set];
68         [path stroke];
69         
70         [forgroundColor set];
71         [path fill];
72 }
73 @end