forked from Cocoanetics/DTCoreText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTCoreTextLayoutFrame.h
More file actions
379 lines (262 loc) · 11.4 KB
/
Copy pathDTCoreTextLayoutFrame.h
File metadata and controls
379 lines (262 loc) · 11.4 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//
// DTCoreTextLayoutFrame.h
// DTCoreText
//
// 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
#import "DTCoreTextConstants.h"
@class DTCoreTextLayoutLine;
@class DTTextBlock;
/**
A handler block that is called whenever a text block attributed is encountered during text drawing
*/
typedef void (^DTCoreTextLayoutFrameTextBlockHandler)(DTTextBlock *textBlock, CGRect frame, CGContextRef context, BOOL *shouldDrawDefaultBackground);
/**
The drawing options for DTCoreTextLayoutFrame
*/
typedef NS_ENUM(NSUInteger, DTCoreTextLayoutFrameDrawingOptions)
{
/**
The default method for drawing draws links and attachments. Links are drawn non-highlighted
*/
DTCoreTextLayoutFrameDrawingDefault = 1<<0,
/**
Links are not drawn, e.g. if they are displayed via custom buttons
*/
DTCoreTextLayoutFrameDrawingOmitLinks = 1<<1,
/**
Text attachments are omitted from drawing, e.g. if they are displayed via custom views
*/
DTCoreTextLayoutFrameDrawingOmitAttachments = 1<<2,
/**
If links are drawn they are displayed with the highlighted variant
*/
DTCoreTextLayoutFrameDrawingDrawLinksHighlighted = 1<<3
} ;
@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_HEIGHT_UNKNOWN` 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_WIDTH_UNKNOWN` to not limit the width. Specify `CGFLOAT_HEIGHT_UNKNOWN` 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_WIDTH_UNKNOWN` to not limit the width. Specify `CGFLOAT_HEIGHT_UNKNOWN` 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;
/**
Calculates the frame that is covered by the text content.
The result is calculated by enumerating over all lines and creating a union over all their frames. This is different than the frame property since this gets calculated.
@returns The area that is covered by the text content.
@note The width depends on how many glyphs Core Text was able to fit into a line. A line that gets broken might not have glyphs all the way to the margin. The y origin is always adjusted to be the same as frame since the first line might have some leading. The height is the minimum height that fits all layout lines.
*/
- (CGRect)intrinsicContentFrame;
/**
@name Drawing
*/
/**
Draws the receiver into the given graphics context.
@warning This method is deprecated, use -[DTCoreTextLayoutFrame drawInContext:options:] instead
@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 drawLinks 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 images.
*/
- (void)drawInContext:(CGContextRef)context drawImages:(BOOL)drawImages drawLinks:(BOOL)drawLinks __attribute__((deprecated("use -[DTCoreTextLayoutFrame drawInContext:options:] instead")));
/**
Draws the receiver into the given graphics context.
@param context A graphics context to draw into
@param options The drawing options. See DTCoreTextLayoutFrameDrawingOptions for available options.
*/
- (void)drawInContext:(CGContextRef)context options:(DTCoreTextLayoutFrameDrawingOptions)options;
/**
Set a custom handler to be executed before text belonging to a text block is drawn. Of type <DTCoreTextLayoutFrameTextBlockHandler>.
*/
@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.
Support Layout options are:
- DTCoreTextLayoutFrameLinePositioningAlgorithmWebKit,
- DTCoreTextLayoutFrameLinePositioningAlgorithmLegacy
@param line The line
@param previousLine The line after which to position the line.
@param options The layout options to employ for positioning lines
@returns The correct baseline origin for the line.
*/
- (CGPoint)baselineOriginToPositionLine:(DTCoreTextLayoutLine *)line afterLine:(DTCoreTextLayoutLine *)previousLine options:(DTCoreTextLayoutFrameLinePositioningOptions)options;
/**
Finds the appropriate baseline origin for a line to position it at the correct distance from a previous line using the DTCoreTextLayoutFrameLinePositioningOptionAlgorithmLegacy algorithm.
@warning This method is deprecated, use -[baselineOriginToPositionLine:afterLine:algorithm:] instead
@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 __attribute__((deprecated("use use -[baselineOriginToPositionLine:afterLine:algorithm:] instead")));;
/**
The ratio to decide when to create a justified line
*/
@property (nonatomic, readwrite) CGFloat justifyRatio;
/**
@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;
/**
@returns the current value of the debug frame drawing
*/
+ (BOOL)shouldDrawDebugFrames;
/**
@name Truncation
*/
/**
Maximum number of lines to display before truncation. Default is 0 which indicates no limit.
*/
@property(nonatomic, assign) NSInteger numberOfLines;
/**
Line break mode used to indicate how truncation should occur
*/
@property(nonatomic, assign) NSLineBreakMode lineBreakMode;
/**
Optional attributed string tu use as truncation indicator. If nil, will use "…" w/ attributes taken from text being truncated
*/
@property(nonatomic, strong)NSAttributedString *truncationString;
@end