Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fea58f5bf3
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 199 lines (165 sloc) 6.135 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
#import "StringMeasuringDemo.h"
#import "NS(Attributed)String+Geometrics.h"

@interface NSLayoutManager (extra)
- (id)myExtraData;
@end

@implementation NSLayoutManager (extra)
- (id)myExtraData;
{
    return self->_extraData;
}
@end

@implementation StringMeasuringDemo

+ (void)initialize {
    [self setKeys:[NSArray arrayWithObjects:@"attributedString",nil]
triggerChangeNotificationsForDependentKey:@"windowWidth"];

    [self setKeys:[NSArray arrayWithObjects:@"attributedString",nil]
triggerChangeNotificationsForDependentKey:@"windowHeight"];
}

- (NSAttributedString *)attributedString {
    return [[_attributedString retain] autorelease];
}

- (void)setAttributedString:(NSAttributedString *)value {
if (_attributedString != value) {
        [_attributedString release];
        _attributedString = [value copy];
    }
}

- (NSString*)string {
return [[self attributedString] string] ;
}

- (void)setString:(NSString*)string {
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
[textView font], NSFontAttributeName,
nil] ;
    NSAttributedString* as = [[NSAttributedString alloc] initWithString:string
attributes:attrs] ;
[self setAttributedString:as] ;
    [as release] ;
}

- (float)refreshTextHeights {
[textView setSelectedRange:NSMakeRange(0,0)] ;

    // Here is where we actually invoke NS(Attributed)String+Geometrics
float answer ;
NSAttributedString* attributedString = [self attributedString] ;
NSString* string = [attributedString string] ;
if (string == nil) {
string = @"" ;
}

// Measure contents of the Text View
answer = [attributedString heightForWidth:[textView frame].size.width] ;
[textView showGreenArrowsWithHeight:answer] ;
[heightOfTextView setStringValue:[NSString stringWithFormat:@"Calculated Height: %0.1f pts", answer]] ;

// Set font of text field by copying the initial font in the text view
NSFont* font = [attributedString attribute:NSFontAttributeName
atIndex:0
effectiveRange:NULL] ;

NSString* fontDescription ;
if (font != nil) {
[_textField setFont:font] ;
fontDescription = [[_textField font] description] ;
}
else {
fontDescription = @"nil ('View: Helvetica 12. 'Field: Lucida Grande 12.)" ;
}
[textFieldFontReadout setStringValue:fontDescription] ;

[_textField setStringValue:string] ;

// Measure contents of the Text Field
gNSStringGeometricsTypesetterBehavior = NSTypesetterBehavior_10_2_WithCompatibility ;
answer = [[self string] heightForWidth:[_textField frame].size.width
font:[_textField font]] ;
gNSStringGeometricsTypesetterBehavior = NSTypesetterLatestBehavior ;
[_textField showGreenArrowsWithHeight:answer] ;
[heightOfTextField setStringValue:[NSString stringWithFormat:@"Calculated Height: %0.1f pts", answer]] ;

    return answer ;
}

- (void)observeSize {
[self setAttributedString:[self attributedString]] ;
[self refreshTextHeights] ;
}

- (IBAction)setStringReadMe:(id)sender {
NSString* s = @"To cause recalculation of sizes, resize the window.\n\nYou can change the font by typing cmd+T, selecting text in the NSTextView, etc.\n\nAfter you resize the window, the string in the 'Field will be set to the same as in the 'View, and the (uniform) font of the 'Field will be set to that found in the first attribute run of the 'View. However, initially they are both nil fonts, which is Hevetica 12 for the 'View and Lucida Grande 12 for the 'Field.\n\nSome descenders: ygpqj)" ;
[self setString:s] ;
}

- (IBAction)setStringOutline:(id)sender {
NSMutableString* s = [NSMutableString string] ;
int nLines = 9 ;
int i ;
for (i=0; i<nLines; i++) {
if (i>0) {
[s appendString:@"\n"] ;
}
int j ;
for (j=0; j<i; j++) {
[s appendString:@" "] ;
}
if (i>0) {
for (j=0; j<i; j++) {
[s appendString:@"Sub"] ;
}
}

[s appendFormat:@"Heading %i", i] ;
}
[self setString:s] ;
}

- (float)windowWidth {
NSWindow* window = [textView window] ;
NSRect frame = [window frame] ;
return frame.size.width ;
}

- (void)setWindowWidth:(float)width {
NSWindow* window = [textView window] ;
NSRect frame = [window frame] ;
frame.size.width = width ;
[window setFrame:frame display:YES] ;
}

- (float)windowHeight {
NSWindow* window = [textView window] ;
NSRect frame = [window frame] ;
return frame.size.height ;
}

- (void)setWindowHeight:(float)height {
NSWindow* window = [textView window] ;
NSRect frame = [window frame] ;
frame.size.height = height ;
[window setFrame:frame display:YES] ;
}


- (void)awakeFromNib {
// Want two lines for textFieldFontReadout but stupid Interface Builder
// doesn't allow it to be set to more than one line.
NSRect frame ;

frame = [textFieldFontReadout frame] ;
float lineHeight = frame.size.height ;
frame.origin.y -= lineHeight ;
frame.size.height += lineHeight ;
[textFieldFontReadout setFrame:frame] ;
[[textFieldFontReadout cell] setWraps:YES];
if ([[textFieldFontReadout cell] respondsToSelector:@selector(setLineBreakMode:)]) { // 10.3 does not
[[textFieldFontReadout cell] setLineBreakMode:NSLineBreakByWordWrapping];
}
[self setStringReadMe:self] ;

// Steal attributes from placeholderView
int autoresizingMask = [placeholderView autoresizingMask] ;
frame = [placeholderView frame] ;
NSView* superview = [placeholderView superview] ;

// Create new _textField
_textField = [[NSTextField alloc] initWithFrame:frame] ;
[[_textField cell] setWraps:YES];
if ([[_textField cell] respondsToSelector:@selector(setLineBreakMode:)]) { // 10.3 does not
[[_textField cell] setLineBreakMode:NSLineBreakByWordWrapping];
}
[_textField setAutoresizingMask:autoresizingMask] ;
[_textField bind:@"stringValue"
toObject:self
withKeyPath:@"string"
options:nil] ;

// Replace placeholder with _textField
[placeholderView removeFromSuperviewWithoutNeedingDisplay] ;
[superview addSubview:_textField] ;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(observeSize)
name:NSWindowDidResizeNotification
object:nil] ;
[self refreshTextHeights] ;
}

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