Skip to content

Commit

Permalink
Add dynAttributedText to UILabel and UITextView.
Browse files Browse the repository at this point in the history
  • Loading branch information
srdanrasic committed Mar 12, 2015
1 parent c0089c7 commit f88c9d5
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Bond.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Bond"
s.version = "3.1.1"
s.version = "3.2.0"
s.summary = "A Swift binding framework"

s.description = <<-DESC
Expand All @@ -18,7 +18,7 @@ Pod::Spec.new do |s|
s.social_media_url = "http://twitter.com/srdanrasic"
s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.10"
s.source = { :git => "https://github.com/SwiftBond/Bond.git", :tag => "v3.1.1" }
s.source = { :git => "https://github.com/SwiftBond/Bond.git", :tag => "v3.2.0" }
s.source_files = "Bond"
s.osx.exclude_files = "Bond/Bond+UI*"
s.framework = 'SystemConfiguration'
Expand Down
14 changes: 14 additions & 0 deletions Bond/Bond+UILabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import UIKit

private var textDynamicHandleUILabel: UInt8 = 0;
private var attributedTextDynamicHandleUILabel: UInt8 = 0;

extension UILabel: Bondable {
public var dynText: Dynamic<String> {
Expand All @@ -43,6 +44,19 @@ extension UILabel: Bondable {
}
}

public var dynAttributedText: Dynamic<NSAttributedString> {
if let d: AnyObject = objc_getAssociatedObject(self, &attributedTextDynamicHandleUILabel) {
return (d as? Dynamic<NSAttributedString>)!
} else {
let d = InternalDynamic<NSAttributedString>(self.attributedText ?? NSAttributedString(string: ""), faulty: false)
let bond = Bond<NSAttributedString>() { [weak self] v in if let s = self { s.attributedText = v } }
d.bindTo(bond, fire: false, strongly: false)
d.retain(bond)
objc_setAssociatedObject(self, &attributedTextDynamicHandleUILabel, d, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
return d
}
}

public var designatedBond: Bond<String> {
return self.dynText.valueBond
}
Expand Down
22 changes: 22 additions & 0 deletions Bond/Bond+UITextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import UIKit

private var textDynamicHandleUITextView: UInt8 = 0;
private var attributedTextDynamicHandleUITextView: UInt8 = 0;

extension UITextView: Bondable {

Expand All @@ -52,6 +53,27 @@ extension UITextView: Bondable {
}
}

public var dynAttributedText: Dynamic<NSAttributedString> {
if let d: AnyObject = objc_getAssociatedObject(self, &attributedTextDynamicHandleUITextView) {
return (d as? Dynamic<NSAttributedString>)!
} else {
let d: InternalDynamic<NSAttributedString> = dynamicObservableFor(UITextViewTextDidChangeNotification, object: self) {
notification -> NSAttributedString in
if let textView = notification.object as? UITextView {
return textView.attributedText ?? NSAttributedString(string: "")
} else {
return NSAttributedString(string: "")
}
}

let bond = Bond<NSAttributedString>() { [weak self] v in if let s = self { s.attributedText = v } }
d.bindTo(bond, fire: false, strongly: false)
d.retain(bond)
objc_setAssociatedObject(self, &attributedTextDynamicHandleUITextView, d, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
return d
}
}

public var designatedDynamic: Dynamic<String> {
return self.dynText
}
Expand Down
2 changes: 1 addition & 1 deletion Bond/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.1.1</string>
<string>3.2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion BondTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>3.1.1</string>
<string>3.2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
14 changes: 14 additions & 0 deletions BondTests/UIKitBondTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ class UIKitTests: XCTestCase {
dynamicDriver.value = "c"
XCTAssert(label.text == "c", "Value after dynamic change")
}

func testUILabelAttributedTextBond() {
var dynamicDriver = Dynamic<NSAttributedString>(NSAttributedString(string: "b"))
let label = UILabel()

label.text = "a"
XCTAssert(label.text == "a", "Initial value")

dynamicDriver ->> label.dynAttributedText
XCTAssert(label.attributedText.string == "b", "Value after binding")

dynamicDriver.value = NSAttributedString(string: "c")
XCTAssert(label.attributedText.string == "c", "Value after dynamic change")
}

func testUIProgressViewBond() {
var dynamicDriver = Dynamic<Float>(0)
Expand Down
19 changes: 19 additions & 0 deletions BondTests/UIKitDynamicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ class UIKitDynamicTests: XCTestCase {
XCTAssert(dynamicDriver.value == "d", "Dynamic value reflects text view value change")
}

func testUITextViewAttributedDynamic() {
var dynamicDriver = Dynamic<NSAttributedString>(NSAttributedString(string: "b"))
let textView = UITextView()

textView.attributedText = NSAttributedString(string: "a")
XCTAssert(textView.attributedText.string == "a", "Initial value")

dynamicDriver <->> textView.dynAttributedText
XCTAssert(textView.attributedText.string == "b", "Text view value after binding")

dynamicDriver.value = NSAttributedString(string: "c")
XCTAssert(textView.attributedText.string == "c", "Text view value reflects dynamic value change")

textView.attributedText = NSAttributedString(string: "d")
NSNotificationCenter.defaultCenter().postNotificationName(UITextViewTextDidChangeNotification, object: textView)
XCTAssert(textView.dynAttributedText.value.string == "d", "Dynamic value reflects text view value change")
XCTAssert(dynamicDriver.value.string == "d", "Dynamic value reflects text view value change")
}

func testUIDatePickerDynamic() {
let date1 = NSDate(timeIntervalSince1970: 10)
let date2 = NSDate(timeIntervalSince1970: 10000)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ Following table lists all available Dynamics of UIKit objects:
|----------------|---------------------------------------------------------|-----------------|
| UIView | dynAlpha <br> dynHidden <br> dynBackgroundColor | -- |
| UISlider | dynValue | dynValue |
| UILabel | dynText | dynText |
| UILabel | dynText <br> dynAttributedText | dynText |
| UIProgressView | dynProgress | dynProgress |
| UIImageView | dynImage | dynImage |
| UIButton | dynEnabled <br> dynTitle <br> dynImageForNormalState | dynEnabled |
| UIBarItem | dynEnabled <br> dynTitle <br> dynImage | dynEnabled |
| UISwitch | dynOn | dynOn |
| UITextField | dynText | dynText |
| UITextView | dynText | dynText |
| UITextView | dynText <br> dynAttributedText | dynText |
| UIDatePicker | dynDate | dynDate |
| UIActivityIndicatorView | dynIsAnimating | dynIsAnimating |

Expand Down

0 comments on commit f88c9d5

Please sign in to comment.