From 75cc4aad53f5e3b6cae751c716d38edc9a00d24a Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 3 Sep 2023 14:58:01 +0800 Subject: [PATCH] NSAttributedString // Hardcode default kern value in macOS 14. --- .../NSAttributedTextView.swift | 1 + .../CandidateCellData_Core.swift | 8 +++++--- .../CandidatePool_CocoaImpl.swift | 18 ++++++++++++------ .../VwrCandidateTDK_SwiftUI.swift | 3 ++- .../Sources/NotifierUI/NotifierUI.swift | 6 ++++-- .../PopupCompositionBuffer.swift | 6 ++++++ 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Packages/Fuziki_NSAttributedTextView/Sources/NSAttributedTextView/NSAttributedTextView.swift b/Packages/Fuziki_NSAttributedTextView/Sources/NSAttributedTextView/NSAttributedTextView.swift index d7d4d71f..ae4030f5 100644 --- a/Packages/Fuziki_NSAttributedTextView/Sources/NSAttributedTextView/NSAttributedTextView.swift +++ b/Packages/Fuziki_NSAttributedTextView/Sources/NSAttributedTextView/NSAttributedTextView.swift @@ -99,6 +99,7 @@ public class NSAttributedTextView: NSView { public var backgroundColor: NSColor = .controlBackgroundColor public var attributes: [NSAttributedString.Key: Any] = [ + .kern: 0, .verticalGlyphForm: true, .font: NSFont.systemFont(ofSize: NSFont.systemFontSize), .foregroundColor: NSColor.textColor, diff --git a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidateCellData_Core.swift b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidateCellData_Core.swift index f2f226c3..8b7f5bfc 100644 --- a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidateCellData_Core.swift +++ b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidateCellData_Core.swift @@ -146,8 +146,9 @@ public class CandidateCellData: Hashable { public func attributedString( noSpacePadding: Bool = true, withHighlight: Bool = false, isMatrix: Bool = false ) -> NSAttributedString { - let attrSpace: [NSAttributedString.Key: AnyObject] = [ + let attrSpace: [NSAttributedString.Key: Any] = [ .font: phraseFont(size: size), + .kern: 0, .paragraphStyle: Self.sharedParagraphStyle, ] let result: NSMutableAttributedString = { @@ -172,7 +173,8 @@ public class CandidateCellData: Hashable { } public var attributedStringHeader: NSAttributedString { - let attrKey: [NSAttributedString.Key: AnyObject] = [ + let attrKey: [NSAttributedString.Key: Any] = [ + .kern: 0, .font: selectionKeyFont(size: fontSizeKey), .paragraphStyle: Self.sharedParagraphStyle, .foregroundColor: fontColorKey, @@ -182,7 +184,7 @@ public class CandidateCellData: Hashable { } public func attributedStringPhrase(isMatrix: Bool = false) -> NSAttributedString { - var attrCandidate: [NSAttributedString.Key: AnyObject] = [ + var attrCandidate: [NSAttributedString.Key: Any] = [ .font: phraseFont(size: size), .paragraphStyle: Self.sharedParagraphStyle, .foregroundColor: fontColorCandidate, diff --git a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidatePool_CocoaImpl.swift b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidatePool_CocoaImpl.swift index ee28f6be..5c3ba073 100644 --- a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidatePool_CocoaImpl.swift +++ b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/CandidatePool_CocoaImpl.swift @@ -158,7 +158,8 @@ extension CandidatePool { private var attributedDescriptionHorizontal: NSAttributedString { let paragraphStyle = sharedParagraphStyle - let attrCandidate: [NSAttributedString.Key: AnyObject] = [ + let attrCandidate: [NSAttributedString.Key: Any] = [ + .kern: 0, .font: Self.blankCell.phraseFont(size: Self.blankCell.size), .paragraphStyle: paragraphStyle, ] @@ -197,7 +198,8 @@ extension CandidatePool { private var attributedDescriptionVertical: NSAttributedString { let paragraphStyle = sharedParagraphStyle - let attrCandidate: [NSAttributedString.Key: AnyObject] = [ + let attrCandidate: [NSAttributedString.Key: Any] = [ + .kern: 0, .font: Self.blankCell.phraseFont(size: Self.blankCell.size), .paragraphStyle: paragraphStyle, ] @@ -262,7 +264,8 @@ extension CandidatePool { : NSColor(white: 0.9, alpha: 0.7) let positionCounterColorText = NSColor.controlTextColor let positionCounterTextSize = max(ceil(CandidateCellData.unifiedSize * 0.7), 11) - let attrPositionCounter: [NSAttributedString.Key: AnyObject] = [ + let attrPositionCounter: [NSAttributedString.Key: Any] = [ + .kern: 0, .font: Self.blankCell.phraseFontEmphasized(size: positionCounterTextSize), .backgroundColor: positionCounterColorBG, .foregroundColor: positionCounterColorText, @@ -275,7 +278,8 @@ extension CandidatePool { private var attributedDescriptionTooltip: NSAttributedString { let positionCounterTextSize = max(ceil(CandidateCellData.unifiedSize * 0.7), 11) - let attrTooltip: [NSAttributedString.Key: AnyObject] = [ + let attrTooltip: [NSAttributedString.Key: Any] = [ + .kern: 0, .font: Self.blankCell.phraseFontEmphasized(size: positionCounterTextSize), .foregroundColor: NSColor.textColor, ] @@ -287,11 +291,13 @@ extension CandidatePool { private var attributedDescriptionReverseLookp: NSAttributedString { let reverseLookupTextSize = max(ceil(CandidateCellData.unifiedSize * 0.6), 9) - let attrReverseLookup: [NSAttributedString.Key: AnyObject] = [ + let attrReverseLookup: [NSAttributedString.Key: Any] = [ + .kern: 0, .font: Self.blankCell.phraseFont(size: reverseLookupTextSize), .foregroundColor: NSColor.textColor, ] - let attrReverseLookupSpacer: [NSAttributedString.Key: AnyObject] = [ + let attrReverseLookupSpacer: [NSAttributedString.Key: Any] = [ + .kern: 0, .font: Self.blankCell.phraseFont(size: reverseLookupTextSize), ] let result = NSMutableAttributedString(string: "", attributes: attrReverseLookupSpacer) diff --git a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates/VwrCandidateTDK_SwiftUI.swift b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates/VwrCandidateTDK_SwiftUI.swift index 2185deb6..9df4374e 100644 --- a/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates/VwrCandidateTDK_SwiftUI.swift +++ b/Packages/vChewing_CandidateWindow/Sources/CandidateWindow/TDKCandidates/VwrCandidateTDK_SwiftUI.swift @@ -211,7 +211,8 @@ extension VwrCandidateTDK { /// - Returns: 測量出來的字串渲染寬度,經過向上取整之處理。 func getTextWidth(text: String, fontSize: CGFloat, isBold: Bool) -> CGFloat? { guard !text.isEmpty else { return nil } - let attributes: [NSAttributedString.Key: AnyObject] = [ + let attributes: [NSAttributedString.Key: Any] = [ + .kern: 0, .font: NSFont.systemFont(ofSize: fontSize, weight: isBold ? .bold : .regular), .paragraphStyle: CandidateCellData.sharedParagraphStyle, ] diff --git a/Packages/vChewing_NotifierUI/Sources/NotifierUI/NotifierUI.swift b/Packages/vChewing_NotifierUI/Sources/NotifierUI/NotifierUI.swift index c0022615..d87f9843 100644 --- a/Packages/vChewing_NotifierUI/Sources/NotifierUI/NotifierUI.swift +++ b/Packages/vChewing_NotifierUI/Sources/NotifierUI/NotifierUI.swift @@ -64,13 +64,15 @@ public class Notifier: NSWindowController { let paraStyle = NSMutableParagraphStyle() paraStyle.setParagraphStyle(NSParagraphStyle.default) paraStyle.alignment = .center - let attrTitle: [NSAttributedString.Key: AnyObject] = [ + let attrTitle: [NSAttributedString.Key: Any] = [ + .kern: 0, .foregroundColor: NSColor.controlTextColor, .font: NSFont.boldSystemFont(ofSize: kLargeFontSize), .paragraphStyle: paraStyle, ] let attrString = NSMutableAttributedString(string: messageArray[0], attributes: attrTitle) - let attrAlt: [NSAttributedString.Key: AnyObject] = [ + let attrAlt: [NSAttributedString.Key: Any] = [ + .kern: 0, .foregroundColor: NSColor.secondaryLabelColor, .font: NSFont.systemFont(ofSize: kSmallFontSize), .paragraphStyle: paraStyle, diff --git a/Packages/vChewing_PopupCompositionBuffer/Sources/PopupCompositionBuffer/PopupCompositionBuffer.swift b/Packages/vChewing_PopupCompositionBuffer/Sources/PopupCompositionBuffer/PopupCompositionBuffer.swift index e9ed0853..33b70075 100644 --- a/Packages/vChewing_PopupCompositionBuffer/Sources/PopupCompositionBuffer/PopupCompositionBuffer.swift +++ b/Packages/vChewing_PopupCompositionBuffer/Sources/PopupCompositionBuffer/PopupCompositionBuffer.swift @@ -64,6 +64,7 @@ public class PopupCompositionBuffer: NSWindowController { let attrString: NSMutableAttributedString = .init(string: state.displayedTextConverted) let attrPCBHeader: NSMutableAttributedString = .init(string: " ") let verticalAttributes: [NSAttributedString.Key: Any] = [ + .kern: 0, .verticalGlyphForm: true, .paragraphStyle: { let newStyle = NSMutableParagraphStyle() @@ -88,6 +89,7 @@ public class PopupCompositionBuffer: NSWindowController { let markerAttributes: [NSAttributedString.Key: Any] = { var result: [NSAttributedString.Key: Any] = [ + .kern: 0, .backgroundColor: NSApplication.isDarkMode ? NSColor.systemRed : NSColor.systemYellow, .markedClauseSegment: 0, ] @@ -135,6 +137,10 @@ public class PopupCompositionBuffer: NSWindowController { attrString.insert(attrPCBHeader, at: 0) attrString.insert(attrPCBHeader, at: attrString.length) + attrString.setAttributes( + [.kern: 0], range: NSRange(location: 0, length: attrString.length) + ) + textShown = attrString messageTextField.maximumNumberOfLines = 1 if let editor = messageTextField.currentEditor() {