Permalink
Browse files

Refactored code to use NSFont instead of TUIFont

  • Loading branch information...
1 parent e8d75b0 commit 7231b63c77e1f5951590f63a269d9c21454538c9 @jspahrsummers jspahrsummers committed Jul 25, 2012
@@ -20,7 +20,6 @@ extern NSString * const TUIAttributedStringBackgroundColorAttributeName;
extern NSString * const TUIAttributedStringBackgroundFillStyleName;
extern NSString * const TUIAttributedStringPreDrawBlockName;
-@class TUIFont;
@class TUIColor;
typedef void (^TUIAttributedStringPreDrawBlock)(NSAttributedString *attributedString, NSRange substringRange, CGRect rects[], CFIndex rectCount);
@@ -61,7 +60,7 @@ typedef enum {
@interface NSMutableAttributedString (TUIAdditions)
// write-only properties, reading will return nil
-@property (nonatomic, retain) TUIFont *font;
+@property (nonatomic, retain) NSFont *font;
@property (nonatomic, retain) TUIColor *color;
@property (nonatomic, retain) TUIColor *backgroundColor;
@property (nonatomic, assign) TUIBackgroundFillStyle backgroundFillStyle;
@@ -74,7 +73,7 @@ typedef enum {
- (void)setAlignment:(TUITextAlignment)alignment lineBreakMode:(TUILineBreakMode)lineBreakMode;
- (NSRange)_stringRange;
-- (void)setFont:(TUIFont *)font inRange:(NSRange)range;
+- (void)setFont:(NSFont *)font inRange:(NSRange)range;
- (void)setColor:(TUIColor *)color inRange:(NSRange)range;
- (void)setBackgroundColor:(TUIColor *)color inRange:(NSRange)range;
- (void)setBackgroundFillStyle:(TUIBackgroundFillStyle)fillStyle inRange:(NSRange)range;
@@ -15,7 +15,6 @@
*/
#import "TUIAttributedString.h"
-#import "TUIFont.h"
#import "TUIColor.h"
NSString * const TUIAttributedStringBackgroundColorAttributeName = @"TUIAttributedStringBackgroundColorAttributeName";
@@ -38,9 +37,10 @@ - (NSRange)_stringRange
return NSMakeRange(0, [self length]);
}
-- (void)setFont:(TUIFont *)font inRange:(NSRange)range
+- (void)setFont:(NSFont *)font inRange:(NSRange)range
{
- [self addAttribute:(NSString *)kCTFontAttributeName value:(id)[font ctFont] range:range];
+ // NSFont and CTFont are toll-free bridged.
+ [self addAttribute:(NSString *)kCTFontAttributeName value:font range:range];
}
- (void)setColor:(TUIColor *)color inRange:(NSRange)range
@@ -58,7 +58,7 @@ - (void)setKerning:(CGFloat)k inRange:(NSRange)range
[self addAttribute:(NSString *)kCTKernAttributeName value:[NSNumber numberWithFloat:k] range:range];
}
-- (void)setFont:(TUIFont *)font
+- (void)setFont:(NSFont *)font
{
[self setFont:font inRange:[self _stringRange]];
}
@@ -199,7 +199,7 @@ - (void)setAlignment:(TUITextAlignment)alignment
[self setAlignment:alignment lineBreakMode:TUILineBreakModeWordWrap];
}
-- (TUIFont *)font
+- (NSFont *)font
{
return nil;
}
View
@@ -30,7 +30,7 @@
} _textLabelFlags;
NSString *_text;
- TUIFont *_font;
+ NSFont *_font;
TUIColor *_textColor;
TUITextAlignment _alignment;
TUILineBreakMode _lineBreakMode;
@@ -41,7 +41,7 @@
@property(nonatomic,getter=isSelectable) BOOL selectable;
@property(nonatomic, readonly) TUITextRenderer *renderer;
-@property(nonatomic,strong) TUIFont *font;
+@property(nonatomic,strong) NSFont *font;
@property(nonatomic,strong) TUIColor *textColor;
@property(nonatomic,assign) TUITextAlignment alignment;
@property(nonatomic, assign) TUILineBreakMode lineBreakMode;
View
@@ -16,7 +16,6 @@
#import "TUILabel.h"
#import "TUIColor.h"
-#import "TUIFont.h"
#import "TUINSView.h"
#import "TUIStringDrawing.h"
#import "TUITextRenderer.h"
@@ -130,7 +129,7 @@ - (void)setText:(NSString *)text
self.attributedString = nil;
}
-- (void)setFont:(TUIFont *)font
+- (void)setFont:(NSFont *)font
{
if(font == _font) return;
@@ -17,7 +17,7 @@
#import <Foundation/Foundation.h>
#import "TUIAttributedString.h"
-@class TUIFont;
+@class NSFont;
@class TUIColor;
@interface NSAttributedString (TUIStringDrawing)
@@ -33,15 +33,15 @@
@interface NSString (TUIStringDrawing)
-- (CGSize)ab_sizeWithFont:(TUIFont *)font;
-- (CGSize)ab_sizeWithFont:(TUIFont *)font constrainedToSize:(CGSize)size;
+- (CGSize)ab_sizeWithFont:(NSFont *)font;
+- (CGSize)ab_sizeWithFont:(NSFont *)font constrainedToSize:(CGSize)size;
#if TARGET_OS_MAC
// for ABRowView
-//- (CGSize)drawInRect:(CGRect)rect withFont:(TUIFont *)font lineBreakMode:(TUILineBreakMode)lineBreakMode alignment:(TUITextAlignment)alignment;
+//- (CGSize)drawInRect:(CGRect)rect withFont:(NSFont *)font lineBreakMode:(TUILineBreakMode)lineBreakMode alignment:(TUITextAlignment)alignment;
#endif
-- (CGSize)ab_drawInRect:(CGRect)rect color:(TUIColor *)color font:(TUIFont *)font;
-- (CGSize)ab_drawInRect:(CGRect)rect withFont:(TUIFont *)font lineBreakMode:(TUILineBreakMode)lineBreakMode alignment:(TUITextAlignment)alignment;
+- (CGSize)ab_drawInRect:(CGRect)rect color:(TUIColor *)color font:(NSFont *)font;
+- (CGSize)ab_drawInRect:(CGRect)rect withFont:(NSFont *)font lineBreakMode:(TUILineBreakMode)lineBreakMode alignment:(TUITextAlignment)alignment;
@end
@@ -17,7 +17,6 @@
#import "TUIAttributedString.h"
#import "TUICGAdditions.h"
#import "TUIColor.h"
-#import "TUIFont.h"
#import "TUIStringDrawing.h"
#import "TUITextRenderer.h"
@@ -69,36 +68,36 @@ @implementation NSString (TUIStringDrawing)
#if TARGET_OS_MAC
-- (CGSize)ab_sizeWithFont:(TUIFont *)font
+- (CGSize)ab_sizeWithFont:(NSFont *)font
{
TUIAttributedString *s = [TUIAttributedString stringWithString:self];
s.font = font;
return [s ab_size];
}
-- (CGSize)ab_sizeWithFont:(TUIFont *)font constrainedToSize:(CGSize)size
+- (CGSize)ab_sizeWithFont:(NSFont *)font constrainedToSize:(CGSize)size
{
TUIAttributedString *s = [TUIAttributedString stringWithString:self];
s.font = font;
return [s ab_sizeConstrainedToSize:size];
}
-//- (CGSize)drawInRect:(CGRect)rect withFont:(TUIFont *)font lineBreakMode:(TUILineBreakMode)lineBreakMode alignment:(TUITextAlignment)alignment
+//- (CGSize)drawInRect:(CGRect)rect withFont:(NSFont *)font lineBreakMode:(TUILineBreakMode)lineBreakMode alignment:(TUITextAlignment)alignment
//{
// return [self ab_drawInRect:rect withFont:font lineBreakMode:lineBreakMode alignment:alignment];
//}
#endif
-- (CGSize)ab_drawInRect:(CGRect)rect color:(TUIColor *)color font:(TUIFont *)font
+- (CGSize)ab_drawInRect:(CGRect)rect color:(TUIColor *)color font:(NSFont *)font
{
TUIAttributedString *s = [TUIAttributedString stringWithString:self];
s.color = color;
s.font = font;
return [s ab_drawInRect:rect];
}
-- (CGSize)ab_drawInRect:(CGRect)rect withFont:(TUIFont *)font lineBreakMode:(TUILineBreakMode)lineBreakMode alignment:(TUITextAlignment)alignment
+- (CGSize)ab_drawInRect:(CGRect)rect withFont:(NSFont *)font lineBreakMode:(TUILineBreakMode)lineBreakMode alignment:(TUITextAlignment)alignment
{
TUIAttributedString *s = [TUIAttributedString stringWithString:self];
[s addAttribute:(NSString *)kCTForegroundColorFromContextAttributeName
@@ -17,8 +17,8 @@
#import "TUIResponder.h"
#import "CoreText+Additions.h"
+@class NSFont;
@class TUIColor;
-@class TUIFont;
@class TUIView;
@protocol ABActiveTextRange;
@@ -19,7 +19,6 @@
#import "TUIAttributedString.h"
#import "TUICGAdditions.h"
#import "TUIColor.h"
-#import "TUIFont.h"
#import "TUIStringDrawing.h"
#import "TUITextRenderer+Event.h"
#import "TUIView.h"
View
@@ -19,7 +19,7 @@
#import "TUIAttributedString.h"
@class TUITextEditor;
-@class TUIFont;
+@class NSFont;
@class TUIColor;
@protocol TUITextViewDelegate;
@@ -32,7 +32,7 @@
NSString *placeholder;
TUITextRenderer *placeholderRenderer;
- TUIFont *font;
+ NSFont *font;
TUIColor *textColor;
TUITextAlignment textAlignment;
BOOL editable;
@@ -67,7 +67,7 @@
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *placeholder;
-@property (nonatomic, strong) TUIFont *font;
+@property (nonatomic, strong) NSFont *font;
@property (nonatomic, strong) TUIColor *textColor;
@property (nonatomic, assign) TUITextAlignment textAlignment;
@property (nonatomic, assign) TUIEdgeInsets contentInset;
View
@@ -17,7 +17,6 @@
#import "TUITextView.h"
#import "TUICGAdditions.h"
#import "TUIColor.h"
-#import "TUIFont.h"
#import "TUINSView.h"
#import "TUINSWindow.h"
#import "TUITextViewEditor.h"
@@ -90,12 +89,12 @@ - (void)dealloc {
- (void)_updateDefaultAttributes
{
renderer.defaultAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
- (id)[self.font ctFont], kCTFontAttributeName,
+ self.font, kCTFontAttributeName,
[self.textColor CGColor], kCTForegroundColorAttributeName,
ABNSParagraphStyleForTextAlignment(textAlignment), NSParagraphStyleAttributeName,
nil];
renderer.markedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSFont fontWithName:self.font.fontName size:self.font.pointSize], kCTFontAttributeName, // NSFont and CTFont are toll-free bridged. *BUT* for reasons beyond my understanding, advanced input methods like Japanese and simplified Pinyin break unless this is an NSFont. So there we go.
+ self.font, kCTFontAttributeName,
[self.textColor CGColor], kCTForegroundColorAttributeName,
ABNSParagraphStyleForTextAlignment(textAlignment), NSParagraphStyleAttributeName,
nil];
@@ -122,7 +121,7 @@ - (id)initWithFrame:(CGRect)frame
self.autocorrectedResults = [NSMutableDictionary dictionary];
- self.font = [TUIFont fontWithName:@"HelveticaNeue" size:12];
+ self.font = [NSFont fontWithName:@"HelveticaNeue" size:12];
self.textColor = [TUIColor blackColor];
[self _updateDefaultAttributes];
@@ -166,7 +165,7 @@ - (TUIResponder *)initialFirstResponder
return renderer.initialFirstResponder;
}
-- (void)setFont:(TUIFont *)f
+- (void)setFont:(NSFont *)f
{
font = f;
[self _updateDefaultAttributes];
@@ -323,7 +322,7 @@ - (CGRect)_cursorRect
// Ugh. So this seems to be a decent approximation for the height of the cursor. It doesn't always match the native cursor but what ev.
CGRect r = CGRectIntegral([renderer firstRectForCharacterRange:ABCFRangeFromNSRange(selection)]);
r.size.width = 2.0f;
- CGRect fontBoundingBox = CTFontGetBoundingBox(self.font.ctFont);
+ CGRect fontBoundingBox = CTFontGetBoundingBox((__bridge CTFontRef)self.font);
r.size.height = round(fontBoundingBox.origin.y + fontBoundingBox.size.height);
r.origin.y += floor(self.font.leading);
//NSLog(@"ascent: %f, descent: %f, leading: %f, cap height: %f, x-height: %f, bounding: %@", self.font.ascender, self.font.descender, self.font.leading, self.font.capHeight, self.font.xHeight, NSStringFromRect(CTFontGetBoundingBox(self.font.ctFont)));
@@ -17,7 +17,6 @@
#import "TUITooltipWindow.h"
#import "TUIAttributedString.h"
#import "TUICGAdditions.h"
-#import "TUIFont.h"
#import "TUIStringDrawing.h"
#define TOOLTIP_HEIGHT 18
@@ -140,7 +139,7 @@ + (void)updateTooltip:(NSString *)s delay:(NSTimeInterval)delay
}
CurrentTooltipString = [TUIAttributedString stringWithString:s];
- CurrentTooltipString.font = [TUIFont fontWithName:@"HelveticaNeue" size:11];
+ CurrentTooltipString.font = [NSFont fontWithName:@"HelveticaNeue" size:11];
CurrentTooltipString.kerning = 0.2;
[CurrentTooltipString setAlignment:TUITextAlignmentCenter lineBreakMode:TUILineBreakModeClip];
} else {

0 comments on commit 7231b63

Please sign in to comment.