diff options
author | rowanbeentje <rowan@beent.je> | 2012-03-18 23:55:21 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2012-03-18 23:55:21 +0000 |
commit | b222f608ac679b877b984b3645c7a0bb455f582a (patch) | |
tree | cc261b51b6d4b6bb97359ebec627f9f83e48b5da /Source/SPFontPreviewTextField.m | |
parent | c70dc822c2b9d2d39a65bdde0ca7b61a1355b846 (diff) | |
download | sequelpro-b222f608ac679b877b984b3645c7a0bb455f582a.tar.gz sequelpro-b222f608ac679b877b984b3645c7a0bb455f582a.tar.bz2 sequelpro-b222f608ac679b877b984b3645c7a0bb455f582a.zip |
- Rewrite SPFontPreviewTextField to replace previous Colloquy-derived GPL-licensed previous implementation, and amend calls to match
Diffstat (limited to 'Source/SPFontPreviewTextField.m')
-rw-r--r-- | Source/SPFontPreviewTextField.m | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/Source/SPFontPreviewTextField.m b/Source/SPFontPreviewTextField.m index 90e9f93f..60243233 100644 --- a/Source/SPFontPreviewTextField.m +++ b/Source/SPFontPreviewTextField.m @@ -4,9 +4,6 @@ // SPFontPreviewTextField.m // sequel-pro // -// This is a heavily modified version of JVFontPreviewField from -// the Colloquy Project <http://colloquy.info/> -// // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or @@ -27,37 +24,39 @@ @implementation SPFontPreviewTextField -- (void)setFont:(NSFont *)font +/** + * Add a method to set the font to use for the preview. The font metrics + * are applied to the textField, and the font name is displayed in the textField + * for an easy preview. + */ +- (void)setFont:(NSFont *)theFont { - if (!font) return; - - if (_actualFont) [_actualFont release]; - - _actualFont = [font retain]; - [super setFont:[[NSFontManager sharedFontManager] convertFont:font toSize:11.0f]]; + // If no font was supplied, clear the preview + if (!theFont) { + [self setObjectValue:@""]; + return; + } - NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[_actualFont displayName]]; - NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; + // Take the supplied font and apply all its traits except for a standardised + // font size to the text field + NSFont *displayFont = [[NSFontManager sharedFontManager] convertFont:theFont toSize:11.0f]; + [super setFont:displayFont]; - [paraStyle setMinimumLineHeight:NSHeight([self bounds])]; - [paraStyle setMaximumLineHeight:NSHeight([self bounds])]; - - [text addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [text length])]; + // Set up a paragraph style for display, setting bounds and display settings + NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle new] autorelease]; + [paragraphStyle setAlignment:NSNaturalTextAlignment]; + [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingMiddle]; + [paragraphStyle setMaximumLineHeight:NSHeight([self bounds]) + [displayFont descender]]; - [self setObjectValue:text]; - - [text release]; - [paraStyle release]; -} + // Set up the text to display - the font display name and the point size. + NSMutableAttributedString *displayString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@, %.1f pt", [theFont displayName], [theFont pointSize]]]; -#pragma mark - + // Apply the paragraph style + [displayString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [displayString length])]; -- (void)dealloc -{ - if (_actualFont) [_actualFont release], _actualFont = nil; - - [super dealloc]; + // Update the display + [self setObjectValue:displayString]; } @end |