Skip to content

Commit 90d29ab

Browse files
authored
Merge pull request #1 from fabioalmeida/dev
Initial version of README. Added proper Usage examples to project
2 parents 021d219 + 9160574 commit 90d29ab

File tree

8 files changed

+190
-63
lines changed

8 files changed

+190
-63
lines changed

Example/LabelConfigurator.xcodeproj/project.pbxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@
461461
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
462462
GCC_WARN_UNUSED_FUNCTION = YES;
463463
GCC_WARN_UNUSED_VARIABLE = YES;
464-
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
464+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
465465
MTL_ENABLE_DEBUG_INFO = YES;
466466
ONLY_ACTIVE_ARCH = YES;
467467
SDKROOT = iphoneos;
@@ -508,7 +508,7 @@
508508
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
509509
GCC_WARN_UNUSED_FUNCTION = YES;
510510
GCC_WARN_UNUSED_VARIABLE = YES;
511-
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
511+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
512512
MTL_ENABLE_DEBUG_INFO = NO;
513513
SDKROOT = iphoneos;
514514
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

Example/LabelConfigurator/Base.lproj/Main.storyboard

+8-10
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,20 @@
2121
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
2222
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2323
<subviews>
24-
<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="286-35-xGl">
25-
<rect key="frame" x="16" y="20" width="343" height="20.5"/>
26-
<fontDescription key="fontDescription" type="system" pointSize="17"/>
27-
<nil key="textColor"/>
28-
<nil key="highlightedColor"/>
29-
</label>
24+
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="top" translatesAutoresizingMaskIntoConstraints="NO" id="Mpq-c2-tHE">
25+
<rect key="frame" x="16" y="20" width="343" height="322"/>
26+
</stackView>
3027
</subviews>
3128
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3229
<constraints>
33-
<constraint firstItem="286-35-xGl" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" id="CaQ-nd-tdz"/>
34-
<constraint firstAttribute="trailingMargin" secondItem="286-35-xGl" secondAttribute="trailing" id="POZ-00-h01"/>
35-
<constraint firstAttribute="leadingMargin" secondItem="286-35-xGl" secondAttribute="leading" id="gZZ-rA-8V7"/>
30+
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="Mpq-c2-tHE" secondAttribute="bottom" constant="325" id="7ar-YL-hMb"/>
31+
<constraint firstItem="Mpq-c2-tHE" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" id="GA2-PE-8wG"/>
32+
<constraint firstAttribute="leadingMargin" secondItem="Mpq-c2-tHE" secondAttribute="leading" id="W8U-Y4-pr7"/>
33+
<constraint firstAttribute="trailingMargin" secondItem="Mpq-c2-tHE" secondAttribute="trailing" id="avo-h2-UtN"/>
3634
</constraints>
3735
</view>
3836
<connections>
39-
<outlet property="myLabel" destination="286-35-xGl" id="IZp-nE-jyN"/>
37+
<outlet property="stackView" destination="Mpq-c2-tHE" id="xJF-8L-ZCo"/>
4038
</connections>
4139
</viewController>
4240
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>

Example/LabelConfigurator/ViewController.swift

+54-12
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,65 @@ import LabelConfigurator
1111

1212
class ViewController: UIViewController {
1313

14-
@IBOutlet weak var myLabel: UILabel!
14+
@IBOutlet weak var stackView: UIStackView!
1515

1616
override func viewDidLoad() {
1717

1818
super.viewDidLoad()
1919

20-
self.myLabel.setLabelText("Sed ut perspiciatis unde omnis iste natus error")
21-
.set(font: UIFont.boldSystemFont(ofSize: 30))
22-
.set(textColor: .blue)
23-
.set(lineSpacing: 6)
24-
.configure()
25-
}
20+
let label1 = UILabel()
2621

27-
override func didReceiveMemoryWarning() {
28-
super.didReceiveMemoryWarning()
29-
// Dispose of any resources that can be recreated.
30-
}
22+
label1.setLabelText("Some text")
23+
.set(font: UIFont.boldSystemFont(ofSize: 14))
24+
.set(textColor: .blue)
25+
.configure()
3126

32-
}
27+
self.stackView.addArrangedSubview(label1)
28+
29+
30+
31+
let label2 = UILabel()
32+
label2.numberOfLines = 0
33+
34+
label2.setLabelText("Some text \n Other text")
35+
.set(font: UIFont.boldSystemFont(ofSize: 14))
36+
.set(textColor: .blue)
37+
.set(fontTracking: 4)
38+
.set(lineSpacing: 10)
39+
.configure()
40+
41+
self.stackView.addArrangedSubview(label2)
42+
43+
44+
45+
let label3 = UILabel()
3346

47+
let minimumAmount = "$5"
48+
49+
label3.setLabelText("Please donate \(minimumAmount) or more!")
50+
.set(font: UIFont.systemFont(ofSize: 12))
51+
.set(textColor: .black)
52+
.set(font: UIFont.boldSystemFont(ofSize: 13), onSubstring: minimumAmount)
53+
.set(textColor: .red, onSubstring: minimumAmount)
54+
.configure()
55+
56+
self.stackView.addArrangedSubview(label3)
57+
58+
59+
60+
let label4 = UILabel()
61+
62+
let oldPrice = "$350"
63+
let newPrice = "$299"
64+
65+
label4.setLabelText("New price \(oldPrice) \(newPrice)")
66+
.set(font: UIFont.systemFont(ofSize: 12))
67+
.set(textColor: .black)
68+
.set(textColor: .red, onSubstring: newPrice)
69+
.set(textColor: .lightGray, onSubstring: oldPrice)
70+
.set(attribute: NSStrikethroughStyleAttributeName, value: NSNumber(value: 1), onSubstring: oldPrice)
71+
.configure()
72+
73+
self.stackView.addArrangedSubview(label4)
74+
}
75+
}

Example/Podfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ target 'LabelConfigurator_Example' do
77
target 'LabelConfigurator_Tests' do
88
inherit! :search_paths
99

10-
pod 'FBSnapshotTestCase' , '~> 2.1.4'
10+
#pod 'FBSnapshotTestCase' , '~> 2.1.4'
1111
end
1212
end

Example/Podfile.lock

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
PODS:
2-
- FBSnapshotTestCase (2.1.4):
3-
- FBSnapshotTestCase/SwiftSupport (= 2.1.4)
4-
- FBSnapshotTestCase/Core (2.1.4)
5-
- FBSnapshotTestCase/SwiftSupport (2.1.4):
6-
- FBSnapshotTestCase/Core
7-
- LabelConfigurator (0.1.1)
2+
- LabelConfigurator (0.1.2)
83

94
DEPENDENCIES:
10-
- FBSnapshotTestCase (~> 2.1.4)
115
- LabelConfigurator (from `../`)
126

137
EXTERNAL SOURCES:
148
LabelConfigurator:
159
:path: ../
1610

1711
SPEC CHECKSUMS:
18-
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
19-
LabelConfigurator: dd59f2a41b3b89d056f8da866c0ad435caf192df
12+
LabelConfigurator: 0f5235e9a35927dc55233ce511031c4f6f1dbc58
2013

21-
PODFILE CHECKSUM: 648bbe941a85cf3d9eb69924f639f3c92de0a90b
14+
PODFILE CHECKSUM: 08e5c5bb7cf9dbc2a1d76bd7b3cf459830a63839
2215

2316
COCOAPODS: 1.3.1

Example/Pods/Manifest.lock

+3-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LabelConfigurator.podspec

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11

22
Pod::Spec.new do |s|
33
s.name = 'LabelConfigurator'
4-
s.version = '0.1.1'
4+
s.version = '0.2.0'
55
s.summary = 'The most simple way to configure your UILabels in one go'
6-
7-
# This description is used to generate tags and improve search results.
8-
# * Think: What does it do? Why did you write it? What is the focus?
9-
# * Try to keep it short, snappy and to the point.
10-
# * Write the description between the DESC delimiters below.
11-
# * Finally, don't worry about the indent, CocoaPods strips it!
12-
136
s.description = <<-DESC
147
The most simple way to configure your UILabels in one go.
158
This Library was created to help on the configuration of UILabels on iOS apps.
169
With this, you can set all your configurations like UIFonts, spacings, UITextColors and so on in one command.
17-
It also helps on the hard task of adding different style to substrings.
10+
It also helps on the hard task of adding different style to substrings.
1811
DESC
19-
2012
s.homepage = 'https://github.com/fabioalmeida/LabelConfigurator'
2113
s.license = { :type => 'MIT', :file => 'LICENSE' }
22-
s.author = { 'fabioalmeida' => 'fabio.almeida@farfetch.com' }
14+
s.author = { 'fabioalmeida' => 'fabio.andre.almeida@gmail.com' }
2315
s.source = { :git => 'https://github.com/fabioalmeida/LabelConfigurator.git', :tag => s.version.to_s }
2416
s.social_media_url = 'https://twitter.com/fabioacalmeida'
2517

26-
s.ios.deployment_target = '8.0'
18+
s.ios.deployment_target = '9.0'
2719
s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3.0' }
2820

2921
s.source_files = 'LabelConfigurator/Classes/**/*'

README.md

+115-6
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,138 @@
22

33
[![CI Status](http://img.shields.io/travis/fabioalmeida/LabelConfigurator.svg?style=flat)](https://travis-ci.org/fabioalmeida/LabelConfigurator)
44
[![Version](https://img.shields.io/cocoapods/v/LabelConfigurator.svg?style=flat)](http://cocoapods.org/pods/LabelConfigurator)
5+
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
56
[![License](https://img.shields.io/cocoapods/l/LabelConfigurator.svg?style=flat)](http://cocoapods.org/pods/LabelConfigurator)
67
[![Platform](https://img.shields.io/cocoapods/p/LabelConfigurator.svg?style=flat)](http://cocoapods.org/pods/LabelConfigurator)
78

8-
## Example
99

10-
To run the example project, clone the repo, and run `pod install` from the Example directory first.
10+
LabelConfigurator is a compact library for iOS designed to help configuring [UILabels](https://developer.apple.com/documentation/uikit/uilabel) without the usual burden of setting all the text attributes in several steps, specially when we have substrings with different attributes.
11+
12+
It was created using a [Builder Pattern](https://en.wikipedia.org/wiki/Builder_pattern) to allow adding several attributes to a single label, but just setting all them at once when needed. The interface itself is very easy and straightforward, making this library adoption super fast. For more detailed examples please check the [Usage](#usage) section.
13+
1114

1215
## Requirements
1316

17+
The minimum requirements needed to use `LabelConfigurator` are:
18+
- iOS 9.0
19+
- Xcode 8
20+
- Obj-C and Swift compatible
21+
22+
1423
## Installation
1524

16-
LabelConfigurator is available through [CocoaPods](http://cocoapods.org). To install
17-
it, simply add the following line to your Podfile:
25+
### CocoaPods
26+
27+
You can use [CocoaPods](http://cocoapods.org) to add `LabelConfigurator` to your project. To install it, simply specify it in your **Podfile**:
1828

1929
```ruby
20-
pod 'LabelConfigurator'
30+
source 'https://github.com/CocoaPods/Specs.git'
31+
platform :ios, '9.0'
32+
use_frameworks!
33+
34+
target 'TargetName' do
35+
pod 'LabelConfigurator', '~> 0.2.0'
36+
end
37+
```
38+
39+
After specifying the new dependency on the Podfile, just run `pod install` to make sure you install the latest version of the library.
40+
41+
42+
### Carthage
43+
44+
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
45+
46+
To integrate `LabelConfigurator` into your Xcode project using Carthage, specify it in your `Cartfile`:
47+
48+
```ogdl
49+
github "fabioameida/LabelConfigurator" ~> 0.2.0
50+
```
51+
52+
Run `carthage update` to build the framework and drag the built `LabelConfigurator.framework` into your Xcode project.
53+
54+
55+
## Usage
56+
57+
To import the library to your project just add the following line to your Swift file:
58+
59+
```swift
60+
import LabelConfigurator
61+
```
62+
63+
If you want to make a really simple [UIFont](https://developer.apple.com/documentation/uikit/uifont) and text color to your label, just add the following code:
64+
65+
```swift
66+
self.myLabel.setLabelText("Some text")
67+
.set(font: UIFont.boldSystemFont(ofSize: 14))
68+
.set(textColor: .blue)
69+
.configure()
2170
```
2271

72+
Please notice two important aspects about the previous code:
73+
1. The first method being called is `setLabelText()`, which returns the `UILabelBuilder` that allows you to add all the needed attributes to the UILabel without any boilerplate code;
74+
2. After adding all the attributes you just need call `configure()` at the end, which will apply all the defined attributes to the UILabel.
75+
76+
The previous code may look quite similar to the `UIKit` code you write to just change the font and text color. But when it comes to other common cases like setting a custom **Font Tracking** or **Line Spacing**, the case is different because that will imply creating an [NSAttributedString](https://developer.apple.com/documentation/foundation/nsattributedstring).
77+
78+
To achieve this with `LabelConfigurator` you just need to do the following:
79+
80+
```swift
81+
self.myLabel.setLabelText("Some text \n Other text")
82+
.set(font: UIFont.boldSystemFont(ofSize: 14))
83+
.set(textColor: .blue)
84+
.set(fontTracking: 4)
85+
.set(lineSpacing: 10)
86+
.configure()
87+
```
88+
89+
Other common example is adding different text attributes to a substring. Now, you can do it like this:
90+
91+
```swift
92+
let minimumAmount = "$5"
93+
94+
self.myLabel.setLabelText("Please donate \(minimumAmount) or more!")
95+
.set(font: UIFont.systemFont(ofSize: 12))
96+
.set(textColor: .black)
97+
.set(font: UIFont.boldSystemFont(ofSize: 13), onSubstring: minimumAmount)
98+
.set(textColor: .red, onSubstring: minimumAmount)
99+
.configure()
100+
```
101+
102+
103+
## Advanced Usage
104+
105+
The most common customisations done on `UILabels` are covered with helper methods to allow you adding attributes as easily as possible as you can see on the previous examples.
106+
107+
However, not all the possibilities are covered (and we want to keep it that way) on `UILabel` attributes and also `NSAttributedString`.
108+
109+
The good this is that you can add your custom attributes you wanted to add to your `NSAttributedString` using two convenience methods to do so:
110+
- `func set(attribute: String, value: AnyObject, onSubstring substring: String)`
111+
- `func set(attribute: String, value: AnyObject, onRange range: NSRange)`
112+
113+
For example, if you want to add a strikethrough attribute to a substring on your `UILabel`, you can do as the following
114+
115+
```swift
116+
let oldPrice = "$350"
117+
let newPrice = "$299"
118+
119+
self.myLabel.setLabelText("New price \(oldPrice) \(newPrice)")
120+
.set(font: UIFont.systemFont(ofSize: 12))
121+
.set(textColor: .black)
122+
.set(textColor: .red, onSubstring: newPrice)
123+
.set(textColor: .lightGray, onSubstring: oldPrice)
124+
.set(attribute: NSStrikethroughStyleAttributeName, value: NSNumber(value: 1), onSubstring: oldPrice)
125+
.configure()
126+
```
127+
128+
All these examples can be found on the repository "Examples" project.
129+
130+
131+
23132
## Author
24133

25134
If you want to get in touch, reach me on twitter: [@fabioacalmeida](https://twitter.com/fabioacalmeida)
26135

27136

28137
## License
29138

30-
LabelBuilder is available under the MIT license. See the LICENSE file for more info.
139+
LabelConfigurator is available under the MIT license. See the LICENSE file for more info.

0 commit comments

Comments
 (0)