Skip to content

Commit 82c54b8

Browse files
Use AttributedString instead of NSAttributedString (#4)
* Set iOS minimum deployment version to v15 * Use AttributedString instead of NSAttributedString
1 parent 1922743 commit 82c54b8

File tree

10 files changed

+30
-28
lines changed

10 files changed

+30
-28
lines changed

README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Bindings
163163
<tr></tr>
164164
<tr>
165165
<td><code>.attributedTitle(for: UIControl.State)</code></td>
166-
<td><code>NSAttributedString</code></td>
166+
<td><code>AttributedString</code></td>
167167
</tr>
168168
<tr></tr>
169169
<tr>
@@ -207,7 +207,7 @@ Bindings
207207
<tr></tr>
208208
<tr>
209209
<td><code>.attributedText</code></td>
210-
<td><code>NSAttributedString</code></td>
210+
<td><code>AttributedString</code></td>
211211
</tr>
212212
<tr></tr>
213213
<tr>
@@ -280,7 +280,7 @@ Bindings
280280
<tr></tr>
281281
<tr>
282282
<td><code>.attributedText</code></td>
283-
<td><code>NSAttributedString</code></td>
283+
<td><code>AttributedString</code></td>
284284
</tr>
285285
<tr></tr>
286286
<tr>
@@ -386,7 +386,7 @@ Extension Methods
386386
<tr></tr>
387387
<tr>
388388
<td><code>.attributedTextPublisher()</code></td>
389-
<td><code>AnyPublisher&lt;NSAttributedString, Never&gt;</code></td>
389+
<td><code>AnyPublisher&lt;AttributedString, Never&gt;</code></td>
390390
</tr>
391391
<tr></tr>
392392
<tr>
@@ -442,7 +442,7 @@ Extension Methods
442442

443443
```swift
444444
func title(for state: UIControl.State) -> Binding<String>
445-
func attributedTitle(for state: UIControl.State) -> Binding<NSAttributedString>
445+
func attributedTitle(for state: UIControl.State) -> Binding<AttributedString>
446446
```
447447

448448
### Extension Method
@@ -470,7 +470,7 @@ Just("Title")
470470
.bind(to: button.bindable.title(for: .normal))
471471
.store(in: &cancellables)
472472

473-
Just(NSAttributedString("Title"))
473+
Just(AttributedString("Title"))
474474
.bind(to: button.bindable.attributedTitle(for: .normal))
475475
.store(in: &cancellables)
476476

@@ -717,7 +717,7 @@ swipe
717717
```swift
718718
var isEnabled: Binding<Bool>
719719
var text: Binding<String>
720-
var attributedText: Binding<NSAttributedString>
720+
var attributedText: Binding<AttributedString>
721721
```
722722

723723
### Code Example
@@ -1225,7 +1225,7 @@ Just(true)
12251225
// Properties
12261226

12271227
var text: AnyPublisher<String, Never>
1228-
var attributedText: AnyPublisher<NSAttributedString, Never>
1228+
var attributedText: AnyPublisher<AttributedString, Never>
12291229

12301230
// UITextFieldDelegate
12311231

@@ -1238,15 +1238,15 @@ var didChangeSelection: AnyPublisher<Void, Never>
12381238

12391239
```swift
12401240
var text: Binding<String>
1241-
var attributedText: Binding<NSAttributedString>
1241+
var attributedText: Binding<AttributedString>
12421242
```
12431243

12441244
### Extension Methods
12451245

12461246
```swift
12471247
func textPublisher() -> AnyPublisher<String, Never>
12481248

1249-
func attributedTextPublisher() -> AnyPublisher<NSAttributedString, Never>
1249+
func attributedTextPublisher() -> AnyPublisher<AttributedString, Never>
12501250
```
12511251

12521252
### Code Example
@@ -1277,7 +1277,7 @@ Just("Text")
12771277
.bind(to: textField.bindable.text)
12781278
.store(in: &cancellables)
12791279

1280-
Just(NSAttributedString("Text"))
1280+
Just(AttributedString("Text"))
12811281
.bind(to: textField.bindable.attributedText)
12821282
.store(in: &cancellables)
12831283

Sources/CombineUI/UIKit/Bindings/Bindable+UIButton.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension Bindable where Target: UIButton {
1515

1616
@preconcurrency
1717
@MainActor
18-
public func attributedTitle(for state: UIControl.State) -> Binding<NSAttributedString> {
19-
Binding(self) { $0.setAttributedTitle($1, for: state) }
18+
public func attributedTitle(for state: UIControl.State) -> Binding<AttributedString> {
19+
Binding(self) { $0.setAttributedTitle(NSAttributedString($1), for: state) }
2020
}
2121
}

Sources/CombineUI/UIKit/Bindings/Bindable+UILabel.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension Bindable where Target: UILabel {
1515
Binding(self, for: \.text)
1616
}
1717

18-
@preconcurrency @MainActor public var attributedText: Binding<NSAttributedString> {
19-
Binding(self, for: \.attributedText)
18+
@preconcurrency @MainActor public var attributedText: Binding<AttributedString> {
19+
Binding(self) { $0.attributedText = NSAttributedString($1) }
2020
}
2121
}

Sources/CombineUI/UIKit/Bindings/Bindable+UITextField.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension Bindable where Target: UITextField {
1111
Binding(self, for: \.text)
1212
}
1313

14-
@preconcurrency @MainActor public var attributedText: Binding<NSAttributedString> {
15-
Binding(self, for: \.attributedText)
14+
@preconcurrency @MainActor public var attributedText: Binding<AttributedString> {
15+
Binding(self) { $0.attributedText = NSAttributedString($1) }
1616
}
1717
}

Sources/CombineUI/UIKit/Controls/TextField/TextField.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ extension UITextField {
3535

3636
@preconcurrency
3737
@MainActor
38-
public func attributedTextPublisher() -> AnyPublisher<NSAttributedString, Never> {
38+
public func attributedTextPublisher() -> AnyPublisher<AttributedString, Never> {
3939
publisher(for: .allEditingEvents)
4040
.compactMap(bindable)
4141
.map(\.attributedText)
4242
.prepend(attributedText)
43-
.map { $0 ?? NSAttributedString(string: "") }
43+
.map { $0.flatMap { AttributedString($0) } ?? AttributedString("") }
4444
.removeDuplicates()
4545
.eraseToAnyPublisher()
4646
}

Sources/CombineUI/UIKit/Controls/TextField/TextFieldInterface.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct TextFieldInterface<T: UITextField> {
1414
.share()
1515
.eraseToAnyPublisher()
1616

17-
public private(set) lazy var attributedText: AnyPublisher<NSAttributedString, Never> = textField
17+
public private(set) lazy var attributedText: AnyPublisher<AttributedString, Never> = textField
1818
.attributedTextPublisher()
1919
.share()
2020
.eraseToAnyPublisher()

Tests/CombineUITests/UIKit/BindingsTests/UIButtonTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class UIButtonTests: XCTestCase {
1818
func testAttributedTitle() {
1919
let button: UIButton = .init()
2020
expect(button.attributedTitle(for: .normal)?.string) == nil
21-
button.bindable.attributedTitle(for: .normal).receiveValue(NSAttributedString("Attributed Title"))
21+
button.bindable.attributedTitle(for: .normal).receiveValue(AttributedString("Attributed Title"))
2222
expect(button.attributedTitle(for: .normal)?.string) == "Attributed Title"
2323
}
2424
}

Tests/CombineUITests/UIKit/BindingsTests/UILabelTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class UILabelTests: XCTestCase {
2525
func testAttributedText() {
2626
let label: UILabel = .init()
2727
expect(label.attributedText) == nil
28-
label.bindable.attributedText.receiveValue(NSAttributedString("Attributed Text"))
28+
label.bindable.attributedText.receiveValue(AttributedString("Attributed Text"))
2929
expect(label.attributedText?.string) == "Attributed Text"
3030
}
3131
}

Tests/CombineUITests/UIKit/BindingsTests/UITextFieldTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class UITextFieldTests: XCTestCase {
1818
func testAttributedText() {
1919
let textField: UITextField = .init()
2020
expect(textField.attributedText?.string.isEmpty) == true
21-
textField.bindable.attributedText.receiveValue(NSAttributedString("Attributed Text"))
21+
textField.bindable.attributedText.receiveValue(AttributedString("Attributed Text"))
2222
expect(textField.attributedText?.string) == "Attributed Text"
2323
}
2424
}

Tests/CombineUITests/UIKit/ControlsTests/TextFieldTests.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ final class TextFieldTests: XCTestCase {
2424
targets.values.forEach { $0.editingChanged() }
2525
}
2626

27-
func invoke(_ attributedText: NSAttributedString) {
28-
self.attributedText = attributedText
27+
func invoke(_ attributedText: AttributedString) {
28+
self.attributedText = NSAttributedString(attributedText)
2929
targets.values.forEach { $0.editingChanged() }
3030
}
3131

@@ -73,14 +73,16 @@ final class TextFieldTests: XCTestCase {
7373
viewController
7474
.$textField
7575
.attributedText
76-
.map(\.string)
76+
.map(\.characters)
77+
.map { $0[...] }
78+
.map(String.init)
7779
.sink { receivedValues.append($0) }
7880
.store(in: &cancellables)
7981
expect(textField.targets.count) == 1
8082
expect(receivedValues) == [""]
81-
textField.invoke(NSAttributedString("Hello"))
83+
textField.invoke(AttributedString("Hello"))
8284
expect(receivedValues) == ["", "Hello"]
83-
textField.invoke(NSAttributedString("World"))
85+
textField.invoke(AttributedString("World"))
8486
expect(receivedValues) == ["", "Hello", "World"]
8587
}
8688

0 commit comments

Comments
 (0)