Skip to content

Commit 1f47ccc

Browse files
committed
refactor: paragraphStyle extension
1 parent bbd9e1f commit 1f47ccc

4 files changed

Lines changed: 28 additions & 12 deletions

File tree

Sources/ReerKit/Foundation/NSAttributedString+REExtensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,20 @@ public extension Reer where Base: NSAttributedString {
216216
/// ReerKit: Set paragraph style for the attributed string using a builder closure.
217217
/// - Parameter builder: A closure that creates and configures the paragraph style.
218218
/// - Returns: A mutable attributed string with paragraph style applied.
219+
@available(*, deprecated, message: "Use paragraphStyle(_: (NSMutableParagraphStyle) -> Void) instead.")
219220
func paragraphStyle(_ builder: () -> NSParagraphStyle) -> NSMutableAttributedString {
220221
return paragraphStyle(builder())
221222
}
222223

224+
/// ReerKit: Set paragraph style for the attributed string using a configuration closure.
225+
/// - Parameter configure: A closure that configures the provided mutable paragraph style.
226+
/// - Returns: A mutable attributed string with paragraph style applied.
227+
func paragraphStyle(_ configure: (NSMutableParagraphStyle) -> Void) -> NSMutableAttributedString {
228+
let style = NSMutableParagraphStyle()
229+
configure(style)
230+
return paragraphStyle(style)
231+
}
232+
223233
/// ReerKit: Set underline style for the attributed string.
224234
/// - Parameter style: The underline style to apply.
225235
/// - Returns: A mutable attributed string with underline applied.

Sources/ReerKit/StandardLibrary/String+REExtensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,10 +1374,20 @@ public extension Reer where Base == String {
13741374
/// ReerKit: Create an attributed string with paragraph style using a builder closure.
13751375
/// - Parameter builder: A closure that creates and configures the paragraph style.
13761376
/// - Returns: A mutable attributed string with paragraph style applied.
1377+
@available(*, deprecated, message: "Use paragraphStyle(_: (NSMutableParagraphStyle) -> Void) instead.")
13771378
func paragraphStyle(_ builder: () -> NSParagraphStyle) -> NSMutableAttributedString {
13781379
return paragraphStyle(builder())
13791380
}
13801381

1382+
/// ReerKit: Create an attributed string with paragraph style using a configuration closure.
1383+
/// - Parameter configure: A closure that configures the provided mutable paragraph style.
1384+
/// - Returns: A mutable attributed string with paragraph style applied.
1385+
func paragraphStyle(_ configure: (NSMutableParagraphStyle) -> Void) -> NSMutableAttributedString {
1386+
let style = NSMutableParagraphStyle()
1387+
configure(style)
1388+
return NSMutableAttributedString(string: base, attributes: [.paragraphStyle: style])
1389+
}
1390+
13811391
/// ReerKit: Create an attributed string with underline style.
13821392
/// - Parameter style: The underline style to apply.
13831393
/// - Returns: A mutable attributed string with underline applied.

Tests/FoundationTests/NSAttributedStringExtensionsTests.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,11 @@ final class NSAttributedStringExtensionsTests: XCTestCase {
242242
XCTAssertNotNil(attributes[.paragraphStyle])
243243
XCTAssertEqual((attributes[.paragraphStyle] as! NSParagraphStyle).alignment, .center)
244244

245-
// Test paragraphStyle with builder method
246-
let withParagraphStyleBuilder = attributedString.re.paragraphStyle {
247-
let style = NSMutableParagraphStyle()
245+
// Test paragraphStyle with configure method
246+
let withParagraphStyleConfigure = attributedString.re.paragraphStyle { style in
248247
style.alignment = .right
249-
return style
250248
}
251-
attributes = withParagraphStyleBuilder.attributes(at: 0, effectiveRange: nil)
249+
attributes = withParagraphStyleConfigure.attributes(at: 0, effectiveRange: nil)
252250
XCTAssertNotNil(attributes[.paragraphStyle])
253251
XCTAssertEqual((attributes[.paragraphStyle] as! NSParagraphStyle).alignment, .right)
254252

Tests/StandardLibraryTests/StringExtensionsTests.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ final class StringExtensionsTests: XCTestCase {
10921092
}
10931093

10941094
func testStringChainableMethods() {
1095-
#if !os(Linux) && canImport(UIKit)
1095+
#if !os(Linux) && canImport(UIKit)
10961096
let testString = "Hello World!"
10971097

10981098
// Test font method
@@ -1132,14 +1132,12 @@ final class StringExtensionsTests: XCTestCase {
11321132
XCTAssertNotNil(attributes[.paragraphStyle])
11331133
XCTAssertEqual((attributes[.paragraphStyle] as! NSParagraphStyle).alignment, .center)
11341134

1135-
// Test paragraphStyle with builder method
1136-
let withParagraphStyleBuilder = testString.re.paragraphStyle {
1137-
let style = NSMutableParagraphStyle()
1135+
// Test paragraphStyle with configure method
1136+
let withParagraphStyleConfigure = testString.re.paragraphStyle { style in
11381137
style.alignment = .right
1139-
return style
11401138
}
1141-
XCTAssertEqual(withParagraphStyleBuilder.string, testString)
1142-
attributes = withParagraphStyleBuilder.attributes(at: 0, effectiveRange: nil)
1139+
XCTAssertEqual(withParagraphStyleConfigure.string, testString)
1140+
attributes = withParagraphStyleConfigure.attributes(at: 0, effectiveRange: nil)
11431141
XCTAssertNotNil(attributes[.paragraphStyle])
11441142
XCTAssertEqual((attributes[.paragraphStyle] as! NSParagraphStyle).alignment, .right)
11451143

0 commit comments

Comments
 (0)