Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #31 from github/attributedstring-nil-fonts

Handle nil fonts given to TUIAttributedString and TUITextView
  • Loading branch information...
commit e5af501c7c666ea7fabe7ed9ef21091fc0f53308 2 parents 608c042 + eec844a
@joshaber joshaber authored
Showing with 27 additions and 12 deletions.
  1. +6 −2 lib/UIKit/TUIAttributedString.m
  2. +21 −10 lib/UIKit/TUITextView.m
View
8 lib/UIKit/TUIAttributedString.m
@@ -39,8 +39,12 @@ - (NSRange)_stringRange
- (void)setFont:(NSFont *)font inRange:(NSRange)range
{
- // NSFont and CTFont are toll-free bridged.
- [self addAttribute:(NSString *)kCTFontAttributeName value:font range:range];
+ if (font != nil) {
+ // NSFont and CTFont are toll-free bridged.
+ [self addAttribute:(NSString *)kCTFontAttributeName value:font range:range];
+ } else {
+ [self removeAttribute:(NSString *)kCTFontAttributeName range:range];
+ }
}
- (void)setColor:(TUIColor *)color inRange:(NSRange)range
View
31 lib/UIKit/TUITextView.m
@@ -82,22 +82,33 @@ @implementation TUITextView
@synthesize autocorrectedResults;
@synthesize placeholderRenderer;
+- (NSFont *)font {
+ // Fall back to the system font if none (or an invalid one) was set.
+ // Otherwise, text rendering becomes dog slow.
+ return font ?: [NSFont systemFontOfSize:[NSFont systemFontSize]];
+}
+
- (void)dealloc {
renderer.delegate = nil;
}
- (void)_updateDefaultAttributes
{
- renderer.defaultAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
- self.font, kCTFontAttributeName,
- [self.textColor CGColor], kCTForegroundColorAttributeName,
- ABNSParagraphStyleForTextAlignment(textAlignment), NSParagraphStyleAttributeName,
- nil];
- renderer.markedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
- self.font, kCTFontAttributeName,
- [self.textColor CGColor], kCTForegroundColorAttributeName,
- ABNSParagraphStyleForTextAlignment(textAlignment), NSParagraphStyleAttributeName,
- nil];
+ NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
+
+ if (self.textColor != nil) {
+ [attributes setObject:(__bridge id)self.textColor.CGColor forKey:(__bridge id)kCTForegroundColorAttributeName];
+ }
+
+ NSParagraphStyle *style = ABNSParagraphStyleForTextAlignment(textAlignment);
+ if (style != nil) {
+ [attributes setObject:style forKey:NSParagraphStyleAttributeName];
+ }
+
+ [attributes setObject:self.font forKey:(__bridge id)kCTFontAttributeName];
+
+ renderer.defaultAttributes = attributes;
+ renderer.markedAttributes = attributes;
}
- (Class)textEditorClass
Please sign in to comment.
Something went wrong with that request. Please try again.