Skip to content

Commit

Permalink
Switch from swizzling setText: to an independent setTextAndApplyChain…
Browse files Browse the repository at this point in the history
…able: method.
  • Loading branch information
Imperiopolis committed Mar 12, 2016
1 parent f47564a commit 4824fcc
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 137 deletions.
14 changes: 6 additions & 8 deletions Example/Tests/BONUtilitiesTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (void)testLabelTextAfterChainable
{
UILabel *label = UILabel.new;
label.bonChainable = BONChain.new.font([UIFont systemFontOfSize:16]);
label.text = @"Hello, world!";
label.textAndApplyChainable = @"Hello, world!";

XCTAssertEqualObjects(label.attributedText.string, @"Hello, world!");

Expand All @@ -41,7 +41,7 @@ - (void)testLabelTextAfterChainable
- (void)testLabelTextBeforeChainable
{
UILabel *label = UILabel.new;
label.text = @"Hello, world!";
label.textAndApplyChainable = @"Hello, world!";
label.bonChainable = BONChain.new.font([UIFont systemFontOfSize:16]);

XCTAssertEqualObjects(label.attributedText.string, @"Hello, world!");
Expand All @@ -64,7 +64,6 @@ - (void)testLabelAttributedTextAfterChainable
label.bonChainable = BONChain.new.font([UIFont systemFontOfSize:16]);
label.attributedText = [[NSAttributedString alloc] initWithString:@"Hello, world!"];

XCTAssertNil(label.bonChainable);
XCTAssertEqualObjects(label.attributedText.string, @"Hello, world!");

NSDictionary *controlAttributes = [self defaultAttributesForClass:[UILabel class] withString:@"Hello, world!"];
Expand Down Expand Up @@ -99,7 +98,7 @@ - (void)testTextViewTextAfterChainable
{
UITextView *textView = UITextView.new;
textView.bonChainable = BONChain.new.font([UIFont systemFontOfSize:16]);
textView.text = @"Hello, world!";
textView.textAndApplyChainable = @"Hello, world!";

XCTAssertEqualObjects(textView.attributedText.string, @"Hello, world!");

Expand All @@ -118,7 +117,7 @@ - (void)testTextViewTextAfterChainable
- (void)testTextViewTextBeforeChainable
{
UITextView *textView = UITextView.new;
textView.text = @"Hello, world!";
textView.textAndApplyChainable = @"Hello, world!";
textView.bonChainable = BONChain.new.font([UIFont systemFontOfSize:16]);

XCTAssertEqualObjects(textView.attributedText.string, @"Hello, world!");
Expand Down Expand Up @@ -174,7 +173,7 @@ - (void)testTextFieldTextAfterChainable
{
UITextField *textField = UITextField.new;
textField.bonChainable = BONChain.new.font([UIFont systemFontOfSize:16]);
textField.text = @"Hello, world!";
textField.textAndApplyChainable = @"Hello, world!";

XCTAssertEqualObjects(textField.attributedText.string, @"Hello, world!");

Expand All @@ -189,7 +188,7 @@ - (void)testTextFieldTextAfterChainable
- (void)testTextFieldTextBeforeChainable
{
UITextField *textField = UITextField.new;
textField.text = @"Hello, world!";
textField.textAndApplyChainable = @"Hello, world!";
textField.bonChainable = BONChain.new.font([UIFont systemFontOfSize:16]);

XCTAssertEqualObjects(textField.attributedText.string, @"Hello, world!");
Expand All @@ -208,7 +207,6 @@ - (void)testTextFieldAttributedTextAfterChainable
textField.bonChainable = BONChain.new.font([UIFont systemFontOfSize:16]);
textField.attributedText = [[NSAttributedString alloc] initWithString:@"Hello, world!"];

XCTAssertNil(textField.bonChainable);
XCTAssertEqualObjects(textField.attributedText.string, @"Hello, world!");

NSDictionary *controlAttributes = [self defaultAttributesForClass:[UITextField class] withString:@"Hello, world!"];
Expand Down
7 changes: 7 additions & 0 deletions Pod/Classes/UILabel+BonMotUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@
*/
@property (copy, nonatomic) id<BONChainable> bonChainable;

/**
* Assigning text via this method will apply the chain attributes and set the @p attributedText with the resulting @p NSAttributedString.
*
* @param text The text to be displayed.
*/
- (void)setTextAndApplyChainable:(NSString *)text;

@end
47 changes: 4 additions & 43 deletions Pod/Classes/UILabel+BonMotUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,23 @@ - (void)setBonChainable:(id<BONChainable>)chainable
{
objc_setAssociatedObject(self, @selector(bonChainable), chainable, OBJC_ASSOCIATION_COPY_NONATOMIC);

if (chainable) {
chainable.text.string = self.text;
[self bon_setAttributedText:chainable.text.attributedString];

if (chainable.text.font) {
self.font = chainable.text.font;
}

if (chainable.text.textColor) {
self.textColor = chainable.text.textColor;
}
}
self.textAndApplyChainable = self.text;
}

- (BONChain *)bonChainable
{
return objc_getAssociatedObject(self, @selector(bonChainable));
}

+ (void)load
{
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
Method originalSetTextMethod = class_getInstanceMethod(self, @selector(setText:));
Method newSetTextMethod = class_getInstanceMethod(self, @selector(bon_setText:));
method_exchangeImplementations(originalSetTextMethod, newSetTextMethod);

Method originalSetAttributedTextMethod = class_getInstanceMethod(self, @selector(setAttributedText:));
Method newSetAttributedTextMethod = class_getInstanceMethod(self, @selector(bon_setAttributedText:));
method_exchangeImplementations(originalSetAttributedTextMethod, newSetAttributedTextMethod);
});
}

- (void)bon_setText:(NSString *)text
- (void)setTextAndApplyChainable:(NSString *)text
{
if (self.bonChainable) {
self.bonChainable.text.string = text;
[self bon_setAttributedText:self.bonChainable.text.attributedString];
self.attributedText = self.bonChainable.text.attributedString;
}
else {
[self bon_setText:text];
self.text = text;
}
}

- (void)bon_setAttributedText:(NSAttributedString *)attributedText
{
if ([self.bonChainable.text.font isEqual:self.font]) {
self.font = nil;
}

if ([self.bonChainable.text.textColor isEqual:self.textColor]) {
self.textColor = nil;
}

self.bonChainable = nil;
[self bon_setAttributedText:attributedText];
}

@end
7 changes: 7 additions & 0 deletions Pod/Classes/UITextField+BonMotUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@
*/
@property (copy, nonatomic) id<BONChainable> bonChainable;

/**
* Assigning text via this method will apply the chain attributes and set the @p attributedText with the resulting @p NSAttributedString.
*
* @param text The text to be displayed.
*/
- (void)setTextAndApplyChainable:(NSString *)text;

@end
47 changes: 4 additions & 43 deletions Pod/Classes/UITextField+BonMotUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,23 @@ - (void)setBonChainable:(id<BONChainable>)chainable
{
objc_setAssociatedObject(self, @selector(bonChainable), chainable, OBJC_ASSOCIATION_COPY_NONATOMIC);

if (chainable) {
chainable.text.string = self.text;
[self bon_setAttributedText:chainable.text.attributedString];

if (chainable.text.font) {
self.font = chainable.text.font;
}

if (chainable.text.textColor) {
self.textColor = chainable.text.textColor;
}
}
self.textAndApplyChainable = self.text;
}

- (BONChain *)bonChainable
{
return objc_getAssociatedObject(self, @selector(bonChainable));
}

+ (void)load
{
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
Method originalSetTextMethod = class_getInstanceMethod(self, @selector(setText:));
Method newSetTextMethod = class_getInstanceMethod(self, @selector(bon_setText:));
method_exchangeImplementations(originalSetTextMethod, newSetTextMethod);

Method originalSetAttributedTextMethod = class_getInstanceMethod(self, @selector(setAttributedText:));
Method newSetAttributedTextMethod = class_getInstanceMethod(self, @selector(bon_setAttributedText:));
method_exchangeImplementations(originalSetAttributedTextMethod, newSetAttributedTextMethod);
});
}

- (void)bon_setText:(NSString *)text
- (void)setTextAndApplyChainable:(NSString *)text
{
if (self.bonChainable) {
self.bonChainable.text.string = text;
[self bon_setAttributedText:self.bonChainable.text.attributedString];
self.attributedText = self.bonChainable.text.attributedString;
}
else {
[self bon_setText:text];
self.text = text;
}
}

- (void)bon_setAttributedText:(NSAttributedString *)attributedText
{
if ([self.bonChainable.text.font isEqual:self.font]) {
self.font = nil;
}

if ([self.bonChainable.text.textColor isEqual:self.textColor]) {
self.textColor = nil;
}

self.bonChainable = nil;
[self bon_setAttributedText:attributedText];
}

@end
7 changes: 7 additions & 0 deletions Pod/Classes/UITextView+BonMotUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@
*/
@property (copy, nonatomic) id<BONChainable> bonChainable;

/**
* Assigning text via this method will apply the chain attributes and set the @p attributedText with the resulting @p NSAttributedString.
*
* @param text The text to be displayed.
*/
- (void)setTextAndApplyChainable:(NSString *)text;

@end
47 changes: 4 additions & 43 deletions Pod/Classes/UITextView+BonMotUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,23 @@ - (void)setBonChainable:(id<BONChainable>)chainable
{
objc_setAssociatedObject(self, @selector(bonChainable), chainable, OBJC_ASSOCIATION_COPY_NONATOMIC);

if (chainable) {
chainable.text.string = self.text;
[self bon_setAttributedText:chainable.text.attributedString];

if (chainable.text.font) {
self.font = chainable.text.font;
}

if (chainable.text.textColor) {
self.textColor = chainable.text.textColor;
}
}
self.textAndApplyChainable = self.text;
}

- (BONChain *)bonChainable
{
return objc_getAssociatedObject(self, @selector(bonChainable));
}

+ (void)load
{
static dispatch_once_t once_token;
dispatch_once(&once_token, ^{
Method originalSetTextMethod = class_getInstanceMethod(self, @selector(setText:));
Method newSetTextMethod = class_getInstanceMethod(self, @selector(bon_setText:));
method_exchangeImplementations(originalSetTextMethod, newSetTextMethod);

Method originalSetAttributedTextMethod = class_getInstanceMethod(self, @selector(setAttributedText:));
Method newSetAttributedTextMethod = class_getInstanceMethod(self, @selector(bon_setAttributedText:));
method_exchangeImplementations(originalSetAttributedTextMethod, newSetAttributedTextMethod);
});
}

- (void)bon_setText:(NSString *)text
- (void)setTextAndApplyChainable:(NSString *)text
{
if (self.bonChainable) {
self.bonChainable.text.string = text;
[self bon_setAttributedText:self.bonChainable.text.attributedString];
self.attributedText = self.bonChainable.text.attributedString;
}
else {
[self bon_setText:text];
self.text = text;
}
}

- (void)bon_setAttributedText:(NSAttributedString *)attributedText
{
if ([self.bonChainable.text.font isEqual:self.font]) {
self.font = nil;
}

if ([self.bonChainable.text.textColor isEqual:self.textColor]) {
self.textColor = nil;
}

self.bonChainable = nil;
[self bon_setAttributedText:attributedText];
}

@end

0 comments on commit 4824fcc

Please sign in to comment.