Skip to content

Commit a01e5f5

Browse files
committed
Add join() as an extension of SequenceType.
1 parent d413589 commit a01e5f5

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

Attributed.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
433A9C0E1BECF169009A3919 /* SequenceType+Join.swift in Sources */ = {isa = PBXBuildFile; fileRef = 433A9C0D1BECF169009A3919 /* SequenceType+Join.swift */; };
11+
433A9C0F1BECF169009A3919 /* SequenceType+Join.swift in Sources */ = {isa = PBXBuildFile; fileRef = 433A9C0D1BECF169009A3919 /* SequenceType+Join.swift */; };
1012
434DBBC81BEC39FA00C11F09 /* NSUnderlineStyle+Attributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 434DBBC71BEC39FA00C11F09 /* NSUnderlineStyle+Attributes.swift */; };
1113
434DBBCA1BEC3AD000C11F09 /* NSShadow+Attributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 434DBBC91BEC3AD000C11F09 /* NSShadow+Attributes.swift */; };
1214
434DBBCB1BEC3AD000C11F09 /* NSShadow+Attributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 434DBBC91BEC3AD000C11F09 /* NSShadow+Attributes.swift */; };
@@ -62,6 +64,7 @@
6264
/* End PBXContainerItemProxy section */
6365

6466
/* Begin PBXFileReference section */
67+
433A9C0D1BECF169009A3919 /* SequenceType+Join.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SequenceType+Join.swift"; sourceTree = "<group>"; };
6568
434DBBC71BEC39FA00C11F09 /* NSUnderlineStyle+Attributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSUnderlineStyle+Attributes.swift"; sourceTree = "<group>"; };
6669
434DBBC91BEC3AD000C11F09 /* NSShadow+Attributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSShadow+Attributes.swift"; sourceTree = "<group>"; };
6770
43C752301BEA525100F928E5 /* ColorType+Attributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ColorType+Attributes.swift"; sourceTree = "<group>"; };
@@ -153,6 +156,7 @@
153156
434DBBC91BEC3AD000C11F09 /* NSShadow+Attributes.swift */,
154157
43C752391BEA601900F928E5 /* NSTextAttachment+Attributes.swift */,
155158
434DBBC71BEC39FA00C11F09 /* NSUnderlineStyle+Attributes.swift */,
159+
433A9C0D1BECF169009A3919 /* SequenceType+Join.swift */,
156160
);
157161
name = Extensions;
158162
sourceTree = "<group>";
@@ -371,6 +375,7 @@
371375
isa = PBXSourcesBuildPhase;
372376
buildActionMask = 2147483647;
373377
files = (
378+
433A9C0E1BECF169009A3919 /* SequenceType+Join.swift in Sources */,
374379
43C993E91BE3BDC000CF90DD /* PlatformTypes.swift in Sources */,
375380
434DBBCA1BEC3AD000C11F09 /* NSShadow+Attributes.swift in Sources */,
376381
43C752401BEA61B000F928E5 /* Ligature+Attributes.swift in Sources */,
@@ -401,6 +406,7 @@
401406
isa = PBXSourcesBuildPhase;
402407
buildActionMask = 2147483647;
403408
files = (
409+
433A9C0F1BECF169009A3919 /* SequenceType+Join.swift in Sources */,
404410
434DBBCC1BEC3AD400C11F09 /* NSUnderlineStyle+Attributes.swift in Sources */,
405411
43C994131BE3DE8900CF90DD /* Functions.swift in Sources */,
406412
43C752411BEA61B000F928E5 /* Ligature+Attributes.swift in Sources */,

Attributed/Functions.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,6 @@
88
// You should have received a copy of the CC0 Public Domain Dedication along with
99
// this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1010

11-
/**
12-
Joins multiple `AttributedStringConvertible` values into one.
13-
14-
- parameter strings: The convertibles to join.
15-
*/
16-
public func Join(strings: AttributedStringConvertible...) -> AttributedStringConvertible
17-
{
18-
return NestedAttributedString(attributes: [:], children: strings)
19-
}
20-
21-
/**
22-
Joins multiple `AttributedStringConvertible` values into one.
23-
24-
- parameter strings: The convertibles to join.
25-
*/
26-
public func Join(strings: [AttributedStringConvertible]) -> AttributedStringConvertible
27-
{
28-
return NestedAttributedString(attributes: [:], children: strings)
29-
}
30-
3111
/**
3212
Returns an attributed string convertible with the specified attributes applied at the root level.
3313

Attributed/SequenceType+Join.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Attributed
2+
// Written in 2015 by Nate Stedman <[email protected]>
3+
//
4+
// To the extent possible under law, the author(s) have dedicated all copyright and
5+
// related and neighboring rights to this software to the public domain worldwide.
6+
// This software is distributed without any warranty.
7+
//
8+
// You should have received a copy of the CC0 Public Domain Dedication along with
9+
// this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
10+
11+
public extension SequenceType where Generator.Element == AttributedStringConvertible
12+
{
13+
/**
14+
Joins multiple `AttributedStringConvertible` values into one.
15+
*/
16+
public func join() -> AttributedStringConvertible
17+
{
18+
return NestedAttributedString(attributes: [:], children: Array(self))
19+
}
20+
}

AttributedTests/AttributedTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,17 @@ class AttributedTests: XCTestCase
7676

7777
XCTAssertEqual(attributed, mutable)
7878
}
79+
80+
func testJoin()
81+
{
82+
let attributed = [
83+
"Test",
84+
NSAttributedString(string: "Test", attributes: [NSForegroundColorAttributeName: ColorType.redColor()])
85+
].join().attributedString
86+
87+
let mutable = NSMutableAttributedString(string: "TestTest")
88+
mutable.addAttribute(NSForegroundColorAttributeName, value: ColorType.redColor(), range: NSMakeRange(4, 4))
89+
90+
XCTAssertEqual(attributed, mutable)
91+
}
7992
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A Swift DSL for `NSAttributedString`.
99
Attributed strings are composed through function calls on extensions of attribute value types, where the parameter is a list of attributed string values to apply the attributes to. The return values of these function calls can be included within lists passed to other function calls, creating a nested structure.
1010

1111
```swift
12-
label.attributedText = Join(
12+
label.attributedText = [
1313
"This is a string with ",
1414
UIColor.blueColor().foregroundAttribute(
1515
"blue ",
@@ -24,10 +24,10 @@ label.attributedText = Join(
2424
" text"
2525
),
2626
"."
27-
).attributedString
27+
].join().attributedString
2828
```
2929

30-
The `Join` function can be used to combine attributed strings without applying any additional attributes.
30+
The `join` extension of `SequenceType` can be used to combine attributed strings without applying any additional attributes.
3131

3232
Any value that conforms to the `AttributedStringConvertible` protocol can be passed as an argument. Implementations for `NSAttributedString` and `String` are included, so it shouldn't be necessary to implement this type. All attribute functions return a value of this protocol type. It can be unwrapped into a `NSAttributedString` with the `attributedString` property.
3333

0 commit comments

Comments
 (0)