Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created September 24, 2012 21:01
Show Gist options
  • Save odrobnik/3778352 to your computer and use it in GitHub Desktop.
Save odrobnik/3778352 to your computer and use it in GitHub Desktop.
//
// DTTextSelectionRect.h
// DTRichTextEditor
//
// Created by Oliver Drobnik on 9/11/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//
#import <UIKit/UIKit.h>
@class DTTextSelectionRect;
@protocol DTTextSelectionRect <NSObject>
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, assign) UITextWritingDirection writingDirection;
@property (nonatomic, assign) BOOL containsStart; // Returns YES if the rect contains the start of the selection.
@property (nonatomic, assign) BOOL containsEnd; // Returns YES if the rect contains the end of the selection.
@property (nonatomic, assign) BOOL isVertical; // Returns YES if the rect is for vertically oriented text.
@end
// on iOS 6 there is a new UITextSelectionRect class we want to be a subclass of
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1
@interface DTTextSelectionRectDerived : UITextSelectionRect <DTTextSelectionRect>
@end
#endif
@interface DTTextSelectionRect : NSObject <DTTextSelectionRect>
+ (id <DTTextSelectionRect>)textSelectionRectWithRect:(CGRect)rect;
@end
//
// DTTextSelectionRect.m
// DTRichTextEditor
//
// Created by Oliver Drobnik on 9/11/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//
#import "DTTextSelectionRect.h"
// on iOS 6 there is a new UITextSelectionRect class we want to be a subclass of
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1
@implementation DTTextSelectionRectDerived
{
CGRect _rect;
UITextWritingDirection _writingDirection;
}
- (id)initWithRect:(CGRect)rect
{
self = [super init];
if (self)
{
_rect = rect;
_writingDirection = UITextWritingDirectionLeftToRight;
}
return self;
}
#pragma mark Properties
@synthesize rect = _rect;
@synthesize writingDirection = _writingDirection;
@synthesize containsStart = _containsStart;
@synthesize containsEnd = _containsEnd;
@synthesize isVertical = _isVertical;
@end
#endif
@implementation DTTextSelectionRect
{
CGRect _rect;
UITextWritingDirection _writingDirection;
}
+ (id <DTTextSelectionRect>)textSelectionRectWithRect:(CGRect)rect
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1
if (NSStringFromClass([UITextSelectionRect class]))
{
return [[DTTextSelectionRectDerived alloc] initWithRect:rect];
}
#endif
return [[DTTextSelectionRect alloc] initWithRect:rect];
}
- (id)initWithRect:(CGRect)rect
{
self = [super init];
if (self)
{
_rect = rect;
_writingDirection = UITextWritingDirectionLeftToRight;
}
return self;
}
#pragma mark Properties
@synthesize rect = _rect;
@synthesize writingDirection = _writingDirection;
@synthesize containsStart = _containsStart;
@synthesize containsEnd = _containsEnd;
@synthesize isVertical = _isVertical;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment