Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 286 lines (190 sloc) 8.011 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
//
// DTCoreTextLayoutFrame.h
// CoreTextExtensions
//
// Created by Oliver Drobnik on 1/24/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//


#if TARGET_OS_IPHONE
#import <CoreText/CoreText.h>
#elif TARGET_OS_MAC
#import <ApplicationServices/ApplicationServices.h>
#endif

@class DTCoreTextLayoutLine;
@class DTTextBlock;


// the value to use if the height is unknown
#define CGFLOAT_OPEN_HEIGHT 16777215.0f

typedef void (^DTCoreTextLayoutFrameTextBlockHandler)(DTTextBlock *textBlock, CGRect frame, CGContextRef context, BOOL *shouldDrawDefaultBackground);


@class DTCoreTextLayouter;

/**
This class represents a single frame of text and basically wraps CTFrame. It provides an array of text lines that fit in the given rectangle.
Both styles of layouting are supported: open ended (suitable for scroll views) and limited to a given rectangle. To use the open-ended style specify `CGFLOAT_OPEN_HEIGHT` for the <frame> height when creating a layout frame.
The array of lines is built lazily the first time it is accessed or - for open-ended frames - when the frame property is being queried.
*/
@interface DTCoreTextLayoutFrame : NSObject
{
CGRect _frame;

NSArray *_lines;
NSArray *_paragraphRanges;

NSArray *_textAttachments;
NSAttributedString *_attributedStringFragment;
}


/**
@name Creating Layout Frames
*/


/**
Creates a Layout Frame with the given frame using the attributed string loaded into the layouter.
@param frame The rectangle specifying origin and size of available for text. Specify `CGFLOAT_OPEN_HEIGHT` to not limit the height.
@param layouter A reference to the layouter for this text box.
*/
- (id)initWithFrame:(CGRect)frame layouter:(DTCoreTextLayouter *)layouter;


/**
Creates a Layout Frame with the given frame using the attributed string loaded into the layouter.
@param frame The rectangle specifying origin and size of available for text. Specify `CGFLOAT_OPEN_HEIGHT` to not limit the height.
@param layouter A reference to the layouter for the receiver. Note: The layouter owns the attributed string.
@param range The range within the attributed string to layout into the receiver.
*/
- (id)initWithFrame:(CGRect)frame layouter:(DTCoreTextLayouter *)layouter range:(NSRange)range;


/**
@name Getting Information
*/


/**
The string range that is visible i.e. fits into the given rectangle. For open-ended frames this is typically the entire string. For frame-contrained layout frames it is the substring that fits.
*/
- (NSRange)visibleStringRange;


/**
This is a copy of the attributed string owned by the layouter of the receiver.
*/
- (NSAttributedString *)attributedStringFragment;


/**
An array that maps glyphs with string indices.
*/
- (NSArray *)stringIndices;


/**
The frame rectangle for the layout frame.
*/
 @property (nonatomic, assign, readonly) CGRect frame;


/**
@name Drawing
*/


/**
Draws the entire layout frame into the given graphics context.
@param context A graphics context to draw into
@param drawImages Whether images should be drawn together with the text. If you specify `NO` then space is left blank where images would go and you have to add your own views to display these images.
@param drawImages Whether hyperlinks should be drawn together with the text. If you specify `NO` then space is left blank where links would go and you have to add your own views to display these links.
*/
- (void)drawInContext:(CGContextRef)context drawImages:(BOOL)drawImages drawLinks:(BOOL)drawLinks;


/**
Set a custom handler to be executed before text belonging to a text block is drawn.
@param handler A DTCoreTextLayoutFrameTextBlockHandler block.
*/
@property (nonatomic, copy) DTCoreTextLayoutFrameTextBlockHandler textBlockHandler;


/**
@name Working with Glyphs
*/


/**
Retrieves the index of the text line that contains the given glyph index.
@param index The index of the glyph
@returns The index of the line containing this glyph
*/
- (NSInteger)lineIndexForGlyphIndex:(NSInteger)index;


/**
Retrieves the frame of the glyph at the given glyph index.
@param index The index of the glyph
@returns The frame of this glyph
*/
- (CGRect)frameOfGlyphAtIndex:(NSInteger)index;


/**
@name Working with Text Lines
*/


/**
The text lines that belong to the receiver.
*/
@property (nonatomic, strong, readonly) NSArray *lines;


/**
The text lines that are visible inside the given rectangle. Also incomplete lines are included.
@param rect The rectangle
@returns An array, sorted from top to bottom, of lines at least partially visible
*/
- (NSArray *)linesVisibleInRect:(CGRect)rect;


/**
The text lines that are visible inside the given rectangle. Only fully visible lines are included.
@param rect The rectangle
@returns An array, sorted from top to bottom, of lines fully visible
*/
- (NSArray *)linesContainedInRect:(CGRect)rect;


/**
The layout line that contains the given string index.
@param index The string index
@returns The layout line that this index belongs to
*/
- (DTCoreTextLayoutLine *)lineContainingIndex:(NSUInteger)index;


/**
Determins if the given line is the first in a paragraph.
This is needed for example to determine whether paragraphSpaceBefore needs to be applied before it.
@param line The Line
@returns `YES` if the given line is the first in a paragraph
*/
- (BOOL)isLineFirstInParagraph:(DTCoreTextLayoutLine *)line;


/**
Determins if the given line is the last in a paragraph.
This is needed for example to determine whether paragraph spacing needs to be applied after it.
@param line The Line
@returns `YES` if the given line is the last in a paragraph
*/
- (BOOL)isLineLastInParagraph:(DTCoreTextLayoutLine *)line;


/**
Finds the appropriate baseline origin for a line to position it at the correct distance from a previous line.
@param line The line
@param previousLine The line after which to position the line.
@returns The correct baseline origin for the line.
*/
- (CGPoint)baselineOriginToPositionLine:(DTCoreTextLayoutLine *)line afterLine:(DTCoreTextLayoutLine *)previousLine;


/**
@name Text Attachments
*/


/**
The array of all <DTTextAttachment> instances that belong to the receiver.
@returns All text attachments of the receiver.
*/
- (NSArray *)textAttachments;


/**
The array of all DTTextAttachment instances that belong to the receiver which also match the specified predicate.
@param predicate A predicate that uses properties of <DTTextAttachment> to reduce the returned array
@returns A filtered array of text attachments.
*/
- (NSArray *)textAttachmentsWithPredicate:(NSPredicate *)predicate;


/**
@name Getting Paragraph Info
*/


/**
Finding which paragraph a given string index belongs to.
@param stringIndex The index in the string to look for
@returns The index of the paragraph, numbered from 0
*/
- (NSUInteger)paragraphIndexContainingStringIndex:(NSUInteger)stringIndex;


/**
Determines the paragraph range (of paragraph indexes) that encompass the entire given string Range.
@param stringRange The string range for which the paragraph range is sought for
@returns The range of paragraphs that fully enclose the string range
*/
- (NSRange)paragraphRangeContainingStringRange:(NSRange)stringRange;


/**
The text lines that belong to the specified paragraph.
@param index The index of the paragraph
@returns An array, sorted from top to bottom, of lines in this paragraph
*/
- (NSArray *)linesInParagraphAtIndex:(NSUInteger)index;


/**
An array of `NSRange` values encapsulated in `NSValue` instances. Each range is the string range contained in the corresponding paragraph.
*/
@property (nonatomic, strong, readonly) NSArray *paragraphRanges;


/**
@name Debugging
*/


/**
Switches on the debug drawing mode where individual glph runs, baselines, et ceter get individually marked.
@param debugFrames if the debug drawing should occur
*/
+ (void)setShouldDrawDebugFrames:(BOOL)debugFrames;

@end
Something went wrong with that request. Please try again.