From: masakih Date: Sun, 1 Jan 2017 13:43:16 +0000 (+0900) Subject: HMStrokeTextFieldCellをSwiftに変換した X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=6edbf3b1ccdd2d7498d54160a70c9660ecab9527;p=kcd%2FKCD.git HMStrokeTextFieldCellをSwiftに変換した --- diff --git a/KCD.xcodeproj/project.pbxproj b/KCD.xcodeproj/project.pbxproj index 3b93018d..74b6f58d 100644 --- a/KCD.xcodeproj/project.pbxproj +++ b/KCD.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ F402D5371BE3AFAB008D5881 /* HMSlotResetCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F402D5361BE3AFAB008D5881 /* HMSlotResetCommand.m */; }; F403893E1CAEA711002A264B /* HMMemberRequireInfoCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F403893D1CAEA711002A264B /* HMMemberRequireInfoCommand.m */; }; F4048A9D1E18D75E008ACFD6 /* HMDamageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4048A9C1E18D75E008ACFD6 /* HMDamageView.swift */; }; + F4048A9F1E19308A008ACFD6 /* HMStrokeTextFieldCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4048A9E1E19308A008ACFD6 /* HMStrokeTextFieldCell.swift */; }; F40ADA021CE6317500AE29B0 /* HMIgnoreCommand.plist in Resources */ = {isa = PBXBuildFile; fileRef = F40ADA011CE6317500AE29B0 /* HMIgnoreCommand.plist */; }; F4131ECF190F5C08004F7F9A /* HMStoreCreateSlotItemHistoryCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4131ECE190F5C08004F7F9A /* HMStoreCreateSlotItemHistoryCommand.m */; }; F4131ED5190F63D8004F7F9A /* HMUpdateSlotItemCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4131ED4190F63D8004F7F9A /* HMUpdateSlotItemCommand.m */; }; @@ -110,7 +111,6 @@ F495462E1B1894DD00E0D8D0 /* Bookmark.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = F495462C1B1894DD00E0D8D0 /* Bookmark.xcdatamodeld */; }; F49546311B18991300E0D8D0 /* HMBookmarkDataStore.m in Sources */ = {isa = PBXBuildFile; fileRef = F49546301B18991300E0D8D0 /* HMBookmarkDataStore.m */; }; F49688671B3EA16800D3CEDE /* RemoveDatabaseFileAndRestart.app in Resources */ = {isa = PBXBuildFile; fileRef = F49688661B3EA16800D3CEDE /* RemoveDatabaseFileAndRestart.app */; }; - F49828FC1902A226000D6BBE /* HMStrokeTextFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F49828FB1902A226000D6BBE /* HMStrokeTextFieldCell.m */; }; F499E8901B5D4E7F00189029 /* HMPortNotifyCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F499E88F1B5D4E7F00189029 /* HMPortNotifyCommand.m */; }; F49AABE61DFC48F300E0B24F /* MainTouchBar.xib in Resources */ = {isa = PBXBuildFile; fileRef = F49AABE51DFC48F300E0B24F /* MainTouchBar.xib */; }; F49AABE81DFD33FB00E0B24F /* HistoryWindowTouchBar.xib in Resources */ = {isa = PBXBuildFile; fileRef = F49AABE71DFD33FB00E0B24F /* HistoryWindowTouchBar.xib */; }; @@ -329,6 +329,7 @@ F403893C1CAEA711002A264B /* HMMemberRequireInfoCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMMemberRequireInfoCommand.h; sourceTree = ""; }; F403893D1CAEA711002A264B /* HMMemberRequireInfoCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMMemberRequireInfoCommand.m; sourceTree = ""; }; F4048A9C1E18D75E008ACFD6 /* HMDamageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMDamageView.swift; sourceTree = ""; }; + F4048A9E1E19308A008ACFD6 /* HMStrokeTextFieldCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMStrokeTextFieldCell.swift; sourceTree = ""; }; F40ADA011CE6317500AE29B0 /* HMIgnoreCommand.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = HMIgnoreCommand.plist; sourceTree = ""; }; F4131ECD190F5C08004F7F9A /* HMStoreCreateSlotItemHistoryCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMStoreCreateSlotItemHistoryCommand.h; sourceTree = ""; }; F4131ECE190F5C08004F7F9A /* HMStoreCreateSlotItemHistoryCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMStoreCreateSlotItemHistoryCommand.m; sourceTree = ""; }; @@ -518,8 +519,6 @@ F497F63D1D5B7CE600A8F609 /* zh-Hant-TW */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hant-TW"; path = "zh-Hant-TW.lproj/InfoPlist.strings"; sourceTree = ""; }; F497F63E1D5B7CE600A8F609 /* zh-Hant-TW */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "zh-Hant-TW"; path = "zh-Hant-TW.lproj/SlotItemShortName.plist"; sourceTree = ""; }; F497F63F1D5B7CE700A8F609 /* zh-Hant-TW */ = {isa = PBXFileReference; lastKnownFileType = text; name = "zh-Hant-TW"; path = "zh-Hant-TW.lproj/Taihi.txt"; sourceTree = ""; }; - F49828FA1902A226000D6BBE /* HMStrokeTextFieldCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMStrokeTextFieldCell.h; sourceTree = ""; }; - F49828FB1902A226000D6BBE /* HMStrokeTextFieldCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMStrokeTextFieldCell.m; sourceTree = ""; }; F499E88E1B5D4E7F00189029 /* HMPortNotifyCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMPortNotifyCommand.h; sourceTree = ""; }; F499E88F1B5D4E7F00189029 /* HMPortNotifyCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMPortNotifyCommand.m; sourceTree = ""; }; F49AABE51DFC48F300E0B24F /* MainTouchBar.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MainTouchBar.xib; sourceTree = ""; }; @@ -1380,8 +1379,7 @@ F4B1E30419C73B6A00805879 /* HMSuppliesCell.m */, F4B1E30519C73B6A00805879 /* HMSuppliesView.h */, F4B1E30619C73B6A00805879 /* HMSuppliesView.m */, - F49828FA1902A226000D6BBE /* HMStrokeTextFieldCell.h */, - F49828FB1902A226000D6BBE /* HMStrokeTextFieldCell.m */, + F4048A9E1E19308A008ACFD6 /* HMStrokeTextFieldCell.swift */, F4048A9C1E18D75E008ACFD6 /* HMDamageView.swift */, ); name = MainWindow; @@ -1787,6 +1785,7 @@ F4513EE818B77A5300073552 /* HMMemberKDockCommand.m in Sources */, F4F9F87E1C6B5E4A00D0EE76 /* HMKCMasterSType.m in Sources */, F431E9791DF3CD1B00FB56AE /* HMActinKindTransformer.m in Sources */, + F4048A9F1E19308A008ACFD6 /* HMStrokeTextFieldCell.swift in Sources */, F45F3BE11E067A870009434E /* HMHistoryItemCleaner.swift in Sources */, F4E5FF0C1E1645280026868C /* HMScreenshotListViewController.swift in Sources */, F426802B194D0F4800B2C27F /* HMMemberShip2Command.m in Sources */, @@ -1940,7 +1939,6 @@ F424A7001BC8C10C0096C431 /* HMFailedCommand.m in Sources */, F49546261B1355EC00E0D8D0 /* HMBookmarkItem.m in Sources */, F4AC13711E096A7B00851147 /* HMHistoryWindowController.swift in Sources */, - F49828FC1902A226000D6BBE /* HMStrokeTextFieldCell.m in Sources */, F43769D61ADE86B200DE6204 /* HMQuestListCommand.m in Sources */, F4A3AD8B19B9BE7A00FDBF80 /* HMMaskSelectView.m in Sources */, F4B414E918B09A4B00468DAF /* HMMasterMapAreaCommand.m in Sources */, diff --git a/KCD/Base.lproj/HMMediumShipViewController.xib b/KCD/Base.lproj/HMMediumShipViewController.xib index e43e66c2..e6c3c2a4 100644 --- a/KCD/Base.lproj/HMMediumShipViewController.xib +++ b/KCD/Base.lproj/HMMediumShipViewController.xib @@ -49,7 +49,7 @@ - + @@ -74,7 +74,7 @@ - + @@ -96,7 +96,7 @@ - + diff --git a/KCD/Base.lproj/HMResourceViewController.xib b/KCD/Base.lproj/HMResourceViewController.xib index 09f036a7..491b02a9 100644 --- a/KCD/Base.lproj/HMResourceViewController.xib +++ b/KCD/Base.lproj/HMResourceViewController.xib @@ -1,7 +1,9 @@ - - + + - + + + @@ -37,14 +39,13 @@ - + - - - + + @@ -53,6 +54,7 @@ + @@ -65,6 +67,7 @@ + @@ -73,6 +76,7 @@ + @@ -85,6 +89,7 @@ + @@ -93,6 +98,7 @@ + @@ -105,6 +111,7 @@ + @@ -113,6 +120,7 @@ + @@ -125,6 +133,7 @@ + @@ -133,6 +142,7 @@ + @@ -145,6 +155,7 @@ + @@ -153,6 +164,7 @@ + @@ -165,6 +177,7 @@ + @@ -173,6 +186,7 @@ + @@ -185,6 +199,7 @@ + @@ -197,6 +212,7 @@ + @@ -205,7 +221,8 @@ - + + @@ -226,6 +243,7 @@ + @@ -234,6 +252,7 @@ + @@ -254,6 +273,7 @@ + @@ -262,6 +282,7 @@ + @@ -269,6 +290,7 @@ + diff --git a/KCD/Base.lproj/HMShipDetailViewController.xib b/KCD/Base.lproj/HMShipDetailViewController.xib index 1b4b0b98..f0c54336 100644 --- a/KCD/Base.lproj/HMShipDetailViewController.xib +++ b/KCD/Base.lproj/HMShipDetailViewController.xib @@ -117,7 +117,7 @@ - + diff --git a/KCD/HMStrokeTextFieldCell.h b/KCD/HMStrokeTextFieldCell.h deleted file mode 100644 index d7f8e930..00000000 --- a/KCD/HMStrokeTextFieldCell.h +++ /dev/null @@ -1,13 +0,0 @@ -// -// HMStrokeTextFieldCell.h -// KCD -// -// Created by Hori,Masaki on 2014/04/19. -// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. -// - -#import - -@interface HMStrokeTextFieldCell : NSTextFieldCell - -@end diff --git a/KCD/HMStrokeTextFieldCell.m b/KCD/HMStrokeTextFieldCell.m deleted file mode 100644 index cb8d1dd2..00000000 --- a/KCD/HMStrokeTextFieldCell.m +++ /dev/null @@ -1,83 +0,0 @@ -// -// HMStrokeTextFieldCell.m -// KCD -// -// Created by Hori,Masaki on 2014/04/19. -// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. -// - -#import "HMStrokeTextFieldCell.h" - -const CGFloat boarderWidth = 2.0; - -@interface HMStrokeTextFieldCell () -@property (nonatomic, strong) NSLayoutManager *layoutManager; -@property (nonatomic, strong) NSTextContainer *textContainer; -@end - -@implementation HMStrokeTextFieldCell - -- (id)initWithCoder:(NSCoder *)aDecoder -{ - self = [super initWithCoder:aDecoder]; - if(self) { - _layoutManager = [NSLayoutManager new]; - _textContainer = [NSTextContainer new]; - [self.layoutManager addTextContainer:self.textContainer]; - } - return self; -} - -- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView -{ - NSAttributedString *attributedString = self.attributedStringValue; - NSDictionary *attribute = [attributedString attributesAtIndex:0 effectiveRange:NULL]; - NSColor *forgroundColor = [attribute objectForKey:NSForegroundColorAttributeName]; - if(!forgroundColor) return; - if([forgroundColor isEqual:[NSColor controlTextColor]]) { - [super drawInteriorWithFrame:cellFrame inView:controlView]; - return; - } - - NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:attributedString.string attributes:attribute]; - [textStorage addLayoutManager:self.layoutManager]; - - NSRange range = [self.layoutManager glyphRangeForTextContainer:self.textContainer]; - CGGlyph glyph[range.length]; - NSUInteger glyphLength = [self.layoutManager getGlyphsInRange:range - glyphs:glyph - properties:NULL - characterIndexes:NULL - bidiLevels:NULL]; - - NSFont *font = [attribute objectForKey:NSFontAttributeName]; - NSPoint point = {boarderWidth,0}; - point.y -= [font descender]; - if([controlView isFlipped]) { - point.y -= [controlView frame].size.height; - } - - NSGlyph nsGlyph[range.length]; - for(int i = 0; i < range.length; i++) { - nsGlyph[i] = glyph[i]; - } - - NSBezierPath *path = [NSBezierPath new]; - [path moveToPoint:point]; - [path appendBezierPathWithGlyphs:nsGlyph count:glyphLength inFont:font]; - [path setLineWidth:boarderWidth]; - [path setLineJoinStyle:NSRoundLineJoinStyle]; - if([controlView isFlipped]) { - NSAffineTransform *affineTransform = [NSAffineTransform transform]; - [affineTransform scaleXBy:1 yBy:-1]; - [path transformUsingAffineTransform:affineTransform]; - } - - [[NSColor blackColor] set]; - [path stroke]; - - [forgroundColor set]; - [path fill]; - -} -@end diff --git a/KCD/HMStrokeTextFieldCell.swift b/KCD/HMStrokeTextFieldCell.swift new file mode 100644 index 00000000..81619bf4 --- /dev/null +++ b/KCD/HMStrokeTextFieldCell.swift @@ -0,0 +1,66 @@ +// +// HMStrokeTextFieldCell.swift +// KCD +// +// Created by Hori,Masaki on 2017/01/01. +// Copyright © 2017å¹´ Hori,Masaki. All rights reserved. +// + +import Cocoa + +class HMStrokeTextFieldCell: NSTextFieldCell { + + required init(coder: NSCoder) { + layoutManager = NSLayoutManager() + textContainer = NSTextContainer() + super.init(coder: coder) + layoutManager.addTextContainer(textContainer) + } + + private static let boarderWidth: CGFloat = 2.0 + + private let layoutManager: NSLayoutManager + private let textContainer: NSTextContainer + + override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) { + let attributedString = attributedStringValue + let attribute = attributedString.attributes(at: 0, effectiveRange: nil) + guard let forgroundColor = attribute[NSForegroundColorAttributeName] as? NSColor else { return } + if forgroundColor == NSColor.controlTextColor { + super.drawInterior(withFrame: cellFrame, in: controlView) + return + } + guard let font = attribute[NSFontAttributeName] as? NSFont else { return } + + let textStorage = NSTextStorage(string: attributedString.string, attributes: attribute) + textStorage.addLayoutManager(layoutManager) + let range = layoutManager.glyphRange(for: textContainer) + let glyph = UnsafeMutablePointer.allocate(capacity: range.length) + let glyphLength = layoutManager.getGlyphs(in: range, glyphs: glyph, properties: nil, characterIndexes: nil, bidiLevels: nil) + var point = NSMakePoint(HMStrokeTextFieldCell.boarderWidth, 0) + point.y -= font.descender + if controlView.isFlipped { + point.y -= NSHeight(controlView.frame) + } + let nsGlyph = UnsafeMutablePointer.allocate(capacity: range.length) + for i in 0...range.length { + nsGlyph[i] = NSGlyph(glyph[i]) + } + + let path = NSBezierPath() + path.move(to: point) + path.appendGlyphs(nsGlyph, count: glyphLength, in: font) + path.lineWidth = HMStrokeTextFieldCell.boarderWidth + path.lineJoinStyle = .roundLineJoinStyle + if controlView.isFlipped { + var affineTransform = AffineTransform() + affineTransform.scale(x: 1, y: -1) + path.transform(using: affineTransform) + } + + NSColor.black.set() + path.stroke() + forgroundColor.set() + path.fill() + } +}