From fd6b44ec7392e03c2e34f5b977682ece8222fb48 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 29 Jul 2017 15:15:22 -0400 Subject: [PATCH] Support increasing / decreasing text size --- writeas.xcodeproj/project.pbxproj | 4 ++++ writeas/Base.lproj/Main.storyboard | 12 ++++++++++++ writeas/Preferences.swift | 15 +++++++++++++++ writeas/ViewController.swift | 22 +++++++++++++++++++--- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 writeas/Preferences.swift diff --git a/writeas.xcodeproj/project.pbxproj b/writeas.xcodeproj/project.pbxproj index 606f88f..83ae6bb 100644 --- a/writeas.xcodeproj/project.pbxproj +++ b/writeas.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 33071D332D5799D36FBC1DDD /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3307150C0D2CA1C8A71C5E26 /* Preferences.swift */; }; C98EE9AC1F29532F002F646E /* writeas in Resources */ = {isa = PBXBuildFile; fileRef = C98EE9AB1F29532F002F646E /* writeas */; }; C9953E7D1F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600.ttf in Resources */ = {isa = PBXBuildFile; fileRef = C9953E791F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600.ttf */; }; C9953E7E1F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600italic.ttf in Resources */ = {isa = PBXBuildFile; fileRef = C9953E7A1F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600italic.ttf */; }; @@ -23,6 +24,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 3307150C0D2CA1C8A71C5E26 /* Preferences.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Preferences.swift; path = writeas/Preferences.swift; sourceTree = ""; }; C98EE9AB1F29532F002F646E /* writeas */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = writeas; sourceTree = ""; }; C9953E791F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600.ttf"; sourceTree = ""; }; C9953E7A1F2D07A7008ED235 /* open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600italic.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "open-sans-v14-cyrillic-ext_vietnamese_cyrillic_latin-ext_latin_greek-ext_greek-600italic.ttf"; sourceTree = ""; }; @@ -71,6 +73,7 @@ children = ( C99B910B1C33841E00A91990 /* writeas */, C99B910A1C33841E00A91990 /* Products */, + 3307150C0D2CA1C8A71C5E26 /* Preferences.swift */, ); sourceTree = ""; }; @@ -180,6 +183,7 @@ files = ( C99B910F1C33841E00A91990 /* ViewController.swift in Sources */, C99B910D1C33841E00A91990 /* AppDelegate.swift in Sources */, + 33071D332D5799D36FBC1DDD /* Preferences.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/writeas/Base.lproj/Main.storyboard b/writeas/Base.lproj/Main.storyboard index 0c4ff9e..e13aaa8 100644 --- a/writeas/Base.lproj/Main.storyboard +++ b/writeas/Base.lproj/Main.storyboard @@ -244,6 +244,18 @@ DQ + + + + + + + + + + + + diff --git a/writeas/Preferences.swift b/writeas/Preferences.swift new file mode 100644 index 0000000..3063196 --- /dev/null +++ b/writeas/Preferences.swift @@ -0,0 +1,15 @@ +// +// Created by Matt Baer on 7/29/17. +// Copyright (c) 2017 A Bunch Tell. All rights reserved. +// + +import Foundation + +class Preferences { + + class func getFontSize() -> CGFloat { + let size = Int(UserDefaults.standard.string(forKey: "editor_text_size") ?? "16") ?? 16 + + return CGFloat(size) + } +} diff --git a/writeas/ViewController.swift b/writeas/ViewController.swift index b34c5ca..a2490d2 100644 --- a/writeas/ViewController.swift +++ b/writeas/ViewController.swift @@ -19,7 +19,7 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe loadDocument() - writerText.font = NSFont(name: "Lora", size: 16) + writerText.font = NSFont(name: "Lora", size: Preferences.getFontSize()) writerText.isHorizontallyResizable = false writerText.textContainerInset = NSSize(width: 16, height: 16) writerText.isAutomaticQuoteSubstitutionEnabled = false @@ -34,8 +34,7 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe override func viewDidAppear() { super.viewDidAppear() - // Fit textview to window size - writerText.setFrameSize(NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height)) + configureWindow() // Style the window self.view.window!.title = "Write.as" @@ -45,6 +44,11 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe self.view.window!.backgroundColor = NSColor.white } + func configureWindow() { + // Fit textview to window size + writerText.setFrameSize(NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height)) + } + override var representedObject: Any? { didSet { // Update the view, if already loaded. @@ -132,5 +136,17 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe self.writerText.textColor = NSColor.black } } + + func adjustTextSize(increment: Bool) { + var size = Preferences.getFontSize() + if increment { + size += 2 + } else { + size -= 1 + } + self.writerText.font = NSFont(name: "Lora", size: size) + UserDefaults.standard.set(String(Int(size)), forKey: "editor_text_size") + configureWindow() + } }