Skip to content

Instantly share code, notes, and snippets.

@wezm
Created October 19, 2010 01:53
Show Gist options
  • Select an option

  • Save wezm/633459 to your computer and use it in GitHub Desktop.

Select an option

Save wezm/633459 to your computer and use it in GitHub Desktop.
Category that stubs out enough of UITextView to allow categories upon it to be speced
#import <Foundation/Foundation.h>
@interface UITextView (Spec)
@end
/* This category attempts to stub out UITextView to allow categories upon it
to be tested. Specifically the following error is being avoided:
Tried to obtain the web lock from a thread other than the main thread or the
web thread. This may be a result of calling to UIKit from a secondary thread.
Crashing now...
*/
#import "UITextView+Spec.h"
static NSMutableDictionary *attributes__;
// Overrides to allow UITextView categories to be tested
@implementation UITextView (Spec)
+ (void)initialize {
attributes__ = [[NSMutableDictionary alloc] init];
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
CFDictionaryAddValue((CFMutableDictionaryRef)attributes__, self, [NSMutableDictionary dictionary]);
}
return self;
}
- (NSMutableDictionary *)attributes {
return [attributes__ objectForKey:self];
}
- (void)dealloc {
CFDictionaryRemoveValue((CFMutableDictionaryRef)attributes__, self);
[super dealloc];
}
#pragma mark Property overrides
- (void)setFrame:(CGRect)frame {
}
- (void)setText:(NSString *)text {
[self.attributes setObject:text forKey:@"text"];
}
- (NSString *)text {
return [self.attributes objectForKey:@"text"];
}
- (void)setSelectedRange:(NSRange)range {
NSValue *value = [NSValue valueWithRange:range];
[self.attributes setObject:value forKey:@"selectedRange"];
}
- (NSRange)selectedRange {
NSValue *value = [self.attributes objectForKey:@"selectedRange"];
return [value rangeValue];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment