Skip to content

Commit a60580a

Browse files
Eliulmlukepistrol
andauthored
Make Ruler look more like Xcode by creating custom font. Make ruler font and ruler Inset scale with text font size. (#170)
<!--- IMPORTANT: If this PR addresses multiple unrelated issues, it will be closed until separated. --> ### Description This PR adds a customized SF Pro Font which aims to look like the Xcode Digits font. The ruler font and the ruler insets scale with the text font size. ### Related Issues <!--- REQUIRED: Tag all related issues (e.g. * #123) --> <!--- If this PR resolves the issue please specify (e.g. * closes #123) --> <!--- If this PR addresses multiple issues, these issues must be related to one other --> * closes #58 * closes #157 ### Checklist <!--- Add things that are not yet implemented above --> - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots <!--- REQUIRED: if issue is UI related --> PointSize 13: Xcode left, CodeEdit Right: <img width="73" alt="image" src="https://user-images.githubusercontent.com/82230675/227798735-1041b4a0-7aa9-4e3e-8c73-a8993427e862.png"> PointSize 20: Xcode left, CodeEdit Right: <img width="162" alt="image" src="https://user-images.githubusercontent.com/82230675/227799017-87c08fb7-bac5-4a63-8fb2-b0bf4b0f3f0b.png"> <!--- IMPORTANT: Fill out all required fields. Otherwise we might close this PR temporarily --> --------- Co-authored-by: Lukas Pistrol <l.pistrol@gmail.com>
1 parent 87c96ff commit a60580a

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// swift-tools-version: 5.6
1+
// swift-tools-version: 5.7
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
77
name: "CodeEditTextView",
8-
platforms: [.macOS(.v12)],
8+
platforms: [.macOS(.v13)],
99
products: [
1010
.library(
1111
name: "CodeEditTextView",

Sources/CodeEditTextView/Controller/STTextViewController.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
130130
rulerView.textColor = .secondaryLabelColor
131131
rulerView.drawSeparator = false
132132
rulerView.baselineOffset = baselineOffset
133-
rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular)
133+
rulerView.font = rulerFont
134134
rulerView.selectedLineHighlightColor = theme.lineHighlight
135-
rulerView.rulerInsets = STRulerInsets(leading: 20, trailing: 8)
135+
rulerView.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
136136

137137
if self.isEditable == false {
138138
rulerView.selectedLineTextColor = nil
@@ -255,6 +255,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
255255
rulerView?.selectedLineHighlightColor = theme.lineHighlight
256256
rulerView?.baselineOffset = baselineOffset
257257
rulerView.highlightSelectedLine = isEditable
258+
rulerView?.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
259+
rulerView?.font = rulerFont
258260

259261
if let scrollView = view as? NSScrollView {
260262
scrollView.drawsBackground = useThemeBackground
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// NSFont+RulerFont.swift
3+
//
4+
//
5+
// Created by Elias Wahl on 17.03.23.
6+
//
7+
8+
import Foundation
9+
import AppKit
10+
11+
extension STTextViewController {
12+
var rulerFont: NSFont {
13+
let fontSize: Double = (font.pointSize - 1) + 0.25
14+
let fontAdvance: Double = font.pointSize * 0.49 + 0.1
15+
let fontWeight = NSFont.Weight(rawValue: font.pointSize * 0.00001 + 0.0001)
16+
let fontWidth = NSFont.Width(rawValue: -0.13)
17+
18+
let font = NSFont.systemFont(ofSize: fontSize, weight: fontWeight, width: fontWidth)
19+
20+
/// Set the open four
21+
let alt4: [NSFontDescriptor.FeatureKey: Int] = [
22+
.selectorIdentifier: kStylisticAltOneOnSelector,
23+
.typeIdentifier: kStylisticAlternativesType
24+
]
25+
26+
/// Set alternate styling for 6 and 9
27+
let alt6and9: [NSFontDescriptor.FeatureKey: Int] = [
28+
.selectorIdentifier: kStylisticAltTwoOnSelector,
29+
.typeIdentifier: kStylisticAlternativesType
30+
]
31+
32+
/// Make all digits monospaced
33+
let monoSpaceDigits: [NSFontDescriptor.FeatureKey: Int] = [
34+
.selectorIdentifier: 0,
35+
.typeIdentifier: kNumberSpacingType
36+
]
37+
38+
let features = [alt4, alt6and9, monoSpaceDigits]
39+
let descriptor = font.fontDescriptor.addingAttributes([.featureSettings: features, .fixedAdvance: fontAdvance])
40+
return NSFont(descriptor: descriptor, size: 0) ?? font
41+
}
42+
}

0 commit comments

Comments
 (0)