Skip to content

Commit

Permalink
Extend styling
Browse files Browse the repository at this point in the history
  • Loading branch information
theolampert committed Nov 3, 2023
1 parent 7b8c4dd commit f5b43e7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
29 changes: 25 additions & 4 deletions Sources/Quilt/NSMutableAttributedString+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import AppKit
import Foundation

public extension NSMutableAttributedString {
func setTextAttribute(_ key: Key, to newValue: Any, at range: NSRange) {
let range = safeRange(for: range)
let safeRange = safeRange(for: range)
guard length > 0, range.location >= 0 else { return }
beginEditing()
enumerateAttribute(key, in: range, options: .init()) { _, range, _ in
addAttribute(key, value: newValue, range: range)
fixAttributes(in: range)
removeAttribute(key, range: safeRange)
addAttribute(key, value: newValue, range: safeRange)
endEditing()
}

func setAttributes(_ attrs: [NSAttributedString.Key: Any], range: NSRange) {
let safeRange = self.safeRange(for: range)
guard length > 0, safeRange.location >= 0 else { return }
beginEditing()
attrs.forEach { key, value in
removeAttribute(key, range: safeRange)
addAttribute(key, value: value, range: safeRange)
}
endEditing()
}

func makeBold(range: NSRange) {
let boldFont = NSFont.boldSystemFont(ofSize: NSFont.systemFontSize)
setTextAttribute(.font, to: boldFont, at: range)
}

func makeItalic(range: NSRange) {
let italicFont = NSFont.systemFont(ofSize: NSFont.systemFontSize)
italicFont.fontDescriptor.withSymbolicTraits([.italic])
setTextAttribute(.font, to: italicFont, at: range)
}
}
13 changes: 7 additions & 6 deletions Sources/Quilt/QuiltString+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ extension QuiltString {
private func applyFormatting(string: NSMutableAttributedString) -> NSMutableAttributedString {
for operation in quilt.operations {
if case let .addMark(type, start, end) = operation.type {
let range = NSRange(
location: getSpanMarkerIndex(marker: start),
length: getSpanMarkerIndex(marker: end) + 1
)
switch type {
case .underline:
string.setTextAttribute(
.underlineStyle, to: true, at: NSRange(
location: getSpanMarkerIndex(marker: start),
length: getSpanMarkerIndex(marker: end) + 1
)
.underlineStyle, to: true, at: range
)
case .bold:
print("TODO bold")
string.makeBold(range: range)
case .italic:
print("TODO italic")
string.makeItalic(range: range)
}
}
}
Expand Down
26 changes: 11 additions & 15 deletions Tests/QuiltTests/QuiltStringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ final class QuiltStringTests: XCTestCase {
clientA.set(newText: "Hello World")
clientA.addMark(mark: .underline, fromIndex: 0, toIndex: 10)

let exptected = NSMutableAttributedString("Hello World")
exptected.setTextAttribute(
let expected = NSMutableAttributedString("Hello World")
expected.setTextAttribute(
.underlineStyle, to: true, at: NSRange(location: 0, length: 11)
)
let fontBase = NSFont.systemFont(ofSize: 16)
exptected.setTextAttribute(
expected.setTextAttribute(
.font, to: fontBase, at: NSRange(location: 0, length: 11)
)
XCTAssertTrue(clientA.attString.isEqual(to: exptected))
XCTAssertTrue(clientA.attString.isEqual(to: expected))
}

func testRemoveUnderlineAttributedString() throws {
Expand All @@ -89,11 +89,9 @@ final class QuiltStringTests: XCTestCase {
clientA.addMark(mark: .bold, fromIndex: 0, toIndex: 10)

let exptected = NSMutableAttributedString("Hello World")
// exptected.setTextAttribute(
// .underlineStyle, to: true, at: NSRange(location: 0, length: 11)
// )
// print(clientA.attString)
// XCTAssertTrue(clientA.attString.isEqual(to: exptected))
exptected.makeBold(range: NSRange(location: 0, length: 11))

XCTAssertTrue(clientA.attString.isEqual(to: exptected))
}

func testItalicAttributedString() throws {
Expand All @@ -102,12 +100,10 @@ final class QuiltStringTests: XCTestCase {
clientA.set(newText: "Hello World")
clientA.addMark(mark: .italic, fromIndex: 0, toIndex: 10)

let exptected = NSMutableAttributedString("Hello World")
// exptected.setTextAttribute(
// .underlineStyle, to: true, at: NSRange(location: 0, length: 11)
// )
let expected = NSMutableAttributedString("Hello World")
expected.makeItalic(range: NSRange(location: 0, length: 11))

// print(clientA.attString)
// XCTAssertTrue(clientA.attString.isEqual(to: exptected))
print(clientA.attString)
XCTAssertTrue(clientA.attString.isEqual(to: expected))
}
}

0 comments on commit f5b43e7

Please sign in to comment.