Skip to content

Commit

Permalink
Merge pull request #27 from salguodnz/master
Browse files Browse the repository at this point in the history
minor typos fixes, a potential optional addition,  and some more example usage
  • Loading branch information
kazuhiro4949 committed May 23, 2019
2 parents fb0d144 + e30e485 commit 4b4f069
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 34 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ open .xcworkspace
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))

// build NSAttributedString.
let greed = "Hi, ".stylize().color(0x2200ee).size(12).font(.HelveticaNeue).attr
let greeting = "Hi, ".stylize().color(0x2200ee).size(12).font(.HelveticaNeue).attr

// build NSAttributedString with ranges.
let msg = "something happened ".stylize()
Expand All @@ -122,11 +122,15 @@ let msg = "something happened ".stylize()
// build NSAttributedString objects and join them.
let name = "to ".stylize().color(0x23abfc).size(12).font(.HelveticaNeue).attr +
"you".stylize().color(0x123456).size(14).font(.HelveticaNeue_Italic).underline(.double).attr

// build NSAttributedString objects with strikethrough and kerning applied.
let response = "\nHow ".stylize().attr + "boring".stylize().strikeThrough(.single).attr +
" exciting!".stylize().kern(-2).attr
```

This sample generates a styled label.

<img width="350" src="https://cloud.githubusercontent.com/assets/18266814/14254571/49882d08-facb-11e5-9e3d-c37cbef6a003.png">
<img width="350" src="https://user-images.githubusercontent.com/2948712/58213150-3fe99980-7d45-11e9-8b4c-9da54ff156de.png">

Of course, you can wrap up the method chains.
```swift
Expand All @@ -149,6 +153,12 @@ label.attributedText = "you".stylize().strong()
```swift
let firstStep = "yay!".stylize() // => StringStylizer<Styling>
```

##### 1a. Alternatively, use an optional String
```swift
let optionalString:String? = nil
let firstStep = optionalString.stylize() // => StringStylizer<Styling>
```
#### 2. Call methods to select range. Then, StringStylizer change into "NarrowDown" state
```swift
let secondStep = "yay!".stylize().range(0..<UInt.max) // => StringStylizer<NarrowDown>
Expand Down Expand Up @@ -176,7 +186,7 @@ StringStylizer is based on **"Builder Pattern"** (Effective Java version). In ad
<img width="800" src="https://cloud.githubusercontent.com/assets/18320004/18075348/f038fec8-6eae-11e6-8e9c-98c0fa39bcde.png">

Because of them, we are able to
- write our code in a liner manner
- write our code in a linear manner
- call proper methods depending on the situation.

## License
Expand Down
8 changes: 8 additions & 0 deletions StringStylizer/String+StringStylizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ public extension String {
}
}


public extension Optional where Wrapped == String {

func stylize() -> StringStylizer<Styling> {
return (self ?? "").stylize()
}

}
10 changes: 5 additions & 5 deletions StringStylizer/StringStylizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,15 @@ open class StringStylizer<T: StringStylizerStatus>: ExpressibleByStringLiteral {
```
"StringStylizer".stylize().karn(5).attr
"StringStylizer".stylize().kern(5).attr
```
<img width="210" src="https://cloud.githubusercontent.com/assets/18320004/14388994/94d50894-fdeb-11e5-9e78-7c31480bb00c.png">
- parameter value:Double
- returns: StringStylizer<Styling>
*/
open func karn(_ value: Double) -> StringStylizer<Styling> {
open func kern(_ value: Double) -> StringStylizer<Styling> {
_attributes[.kern] = value as Any
let stylizer = StringStylizer<Styling>(attributedString: _attrString, range: _range, attributes: _attributes)
return stylizer
Expand Down Expand Up @@ -398,7 +398,7 @@ open class StringStylizer<T: StringStylizerStatus>: ExpressibleByStringLiteral {
```
"StringStylizer".stylize().strokeThrogh(.StyleDouble, rgb: 0xff0000, alpha: 1).attr
"StringStylizer".stylize().strikeThrough(.StyleDouble, rgb: 0xff0000, alpha: 1).attr
```
<img width="139" src="https://cloud.githubusercontent.com/assets/18320004/14389273/ddc222f2-fdec-11e5-9a80-0181383ef553.png">
Expand All @@ -408,7 +408,7 @@ open class StringStylizer<T: StringStylizerStatus>: ExpressibleByStringLiteral {
- parameter alpha:Double (default:1.0)
- returns: StringStylizer<Styling>
*/
open func strokeThrogh(_ style: NSUnderlineStyle..., rgb: UInt? = nil, alpha: Double = 1) -> StringStylizer<Styling> {
open func strikeThrough(_ style: NSUnderlineStyle..., rgb: UInt? = nil, alpha: Double = 1) -> StringStylizer<Styling> {
#if swift(>=4.2)
let _style: [NSUnderlineStyle] = style.isEmpty ? [.single] : style
#else
Expand Down Expand Up @@ -494,7 +494,7 @@ open class StringStylizer<T: StringStylizerStatus>: ExpressibleByStringLiteral {
- parameter value:Int
- returns: StringStylizer<Styling>
*/
open func ligeture(_ value: Int) -> StringStylizer<Styling> {
open func ligature(_ value: Int) -> StringStylizer<Styling> {
_attributes[.ligature] = value as Any
let stylizer = StringStylizer<Styling>(attributedString: _attrString, range: _range, attributes: _attributes)
return stylizer
Expand Down
14 changes: 7 additions & 7 deletions StringStylizer/UIImage+StringStylizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public extension UIImage {
///
/// - Parameter size: attachmented image size
/// - Returns: NSTextAttachment
func textAttachemnt(with size: CGSize) -> NSTextAttachment {
let textAttachemnt = NSTextAttachment()
textAttachemnt.image = self
textAttachemnt.bounds = CGRect(origin: .zero, size: size)
return textAttachemnt
func textAttachment(with size: CGSize) -> NSTextAttachment {
let textAttachment = NSTextAttachment()
textAttachment.image = self
textAttachment.bounds = CGRect(origin: .zero, size: size)
return textAttachment
}


Expand All @@ -28,8 +28,8 @@ public extension UIImage {
/// - width: attachmented image width
/// - height: attachmented image height
/// - Returns: NSTextAttachment
func textAttachemnt(_ width: CGFloat, _ height: CGFloat) -> NSTextAttachment {
func textAttachment(_ width: CGFloat, _ height: CGFloat) -> NSTextAttachment {
let size = CGSize(width: width, height: height)
return textAttachemnt(with: size)
return textAttachment(with: size)
}
}
39 changes: 26 additions & 13 deletions StringStylizerTests/StringStylizerTests.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class StringStylizerTests: XCTestCase {
XCTAssert(str.isEqual(to: expected), "has background attributed")
}

func testKarn() {
let str = "StringStylizer".stylize().karn(1).attr
func testkern() {
let str = "StringStylizer".stylize().kern(1).attr
let expected = NSAttributedString(string: "StringStylizer", attributes: [.kern: 1])
XCTAssert(str.isEqual(to: expected), "has karn attributed")
XCTAssert(str.isEqual(to: expected), "has kern attributed")
}

func testUnderline() {
Expand Down Expand Up @@ -101,25 +101,25 @@ class StringStylizerTests: XCTestCase {
XCTAssert(str.isEqual(to: expected), "has stroke attributed")
}

func testStrokeThroghParam() {
func testStrikeThroughParam() {
#if swift(>=4.2)
let str = "StringStylizer".stylize().strokeThrogh(.double).attr
let str = "StringStylizer".stylize().strikeThrough(.double).attr
let expected = NSAttributedString(string: "StringStylizer", attributes: [.strikethroughStyle: NSUnderlineStyle.double.rawValue])
#else
let str = "StringStylizer".stylize().strokeThrogh(.styleDouble).attr
let str = "StringStylizer".stylize().strikeThrough(.styleDouble).attr
let expected = NSAttributedString(string: "StringStylizer", attributes: [.strikethroughStyle: NSUnderlineStyle.styleDouble.rawValue])
#endif
XCTAssert(str.isEqual(to: expected), "has strokeThrogh attributed")
XCTAssert(str.isEqual(to: expected), "has strikeThrough attributed")
}

func testStrokeThroghBlankParam() {
let str = "StringStylizer".stylize().strokeThrogh().attr
func testStrikeThroughBlankParam() {
let str = "StringStylizer".stylize().strikeThrough().attr
#if swift(>=4.2)
let expected = NSAttributedString(string: "StringStylizer", attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue])
#else
let expected = NSAttributedString(string: "StringStylizer", attributes: [.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue])
#endif
XCTAssert(str.isEqual(to: expected), "has strokeThrogh attributed")
XCTAssert(str.isEqual(to: expected), "has strikeThrough attributed")
}

func testShadowParam() {
Expand Down Expand Up @@ -157,10 +157,10 @@ class StringStylizerTests: XCTestCase {
XCTAssert(str.isEqual(to: expected), "has shadow attributed")
}

func testLigetureParam() {
let str = "StringStylizer".stylize().ligeture(1).attr
func testLigatureParam() {
let str = "StringStylizer".stylize().ligature(1).attr
let expected = NSAttributedString(string: "StringStylizer", attributes: [.ligature: 1])
XCTAssert(str.isEqual(to: expected), "has ligeture attributed")
XCTAssert(str.isEqual(to: expected), "has ligature attributed")
}

func testBaselineParam() {
Expand Down Expand Up @@ -296,6 +296,19 @@ class StringStylizerTests: XCTestCase {
XCTAssert(str.isEqual(to: expcted), "has paragraph Indent")
}

func testOptionalString() {
let nothing: String? = nil
let something = "StringStylizer"

let combined = (
nothing.stylize().attr +
something.stylize().attr
)

let expected = NSAttributedString(string: "StringStylizer")
XCTAssert(combined.isEqual(expected), "optional StringStylized strings do not add any content to the resulting NSAttributedString")
}

// MARK:- private
fileprivate func rgb(_ rgb: UInt, alpha: Double) -> UIColor {
return UIColor(
Expand Down
8 changes: 4 additions & 4 deletions iOS Sample/iOS Sample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -18,8 +18,8 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qGZ-sC-ib4">
<rect key="frame" x="166" y="323" width="42" height="21"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qGZ-sC-ib4">
<rect key="frame" x="166.5" y="323.5" width="42" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
Expand Down
8 changes: 6 additions & 2 deletions iOS Sample/iOS Sample/ViewController.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ViewController: UIViewController {
super.viewDidLoad()

// build NSAttributedString.
let greed = " Hi, ".stylize().color(0x2200ee).size(12).font(.HelveticaNeue).attr
let greeting = " Hi, ".stylize().color(0x2200ee).size(12).font(.HelveticaNeue).attr

// build NSAttributedString with ranges.
let msg = "something happened ".stylize()
Expand All @@ -27,7 +27,11 @@ class ViewController: UIViewController {
let name = "to ".stylize().color(0x23abfc).size(12).font(.HelveticaNeue).attr +
"you👩‍👩‍👦‍👦".stylize().color(0x123456).size(14).font(.HelveticaNeue_Italic).underline(.double).attr

centerLabel.attributedText = #imageLiteral(resourceName: "Attachment").textAttachemnt(17, 17) + greed + msg + name
// build NSAttributedString objects with strikethrough and kerning applied.
let response = "\nHow ".stylize().attr + "boring".stylize().strikeThrough(.single).attr +
" exciting!".stylize().kern(-2).attr

centerLabel.attributedText = #imageLiteral(resourceName: "Attachment").textAttachment(17, 17) + greeting + msg + name + response
}

override func didReceiveMemoryWarning() {
Expand Down

0 comments on commit 4b4f069

Please sign in to comment.