From 406034566f8e256a2f6ff06d7bd63ca4e7767cf6 Mon Sep 17 00:00:00 2001 From: popwarfour Date: Thu, 4 Jan 2018 17:57:12 -0500 Subject: [PATCH] font fix --- Sources/UITextView+Placeholder.m | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Sources/UITextView+Placeholder.m b/Sources/UITextView+Placeholder.m index 276a0bf..225e9ef 100644 --- a/Sources/UITextView+Placeholder.m +++ b/Sources/UITextView+Placeholder.m @@ -87,22 +87,22 @@ - (UILabel *)placeholderLabel { NSAttributedString *originalText = self.attributedText; self.text = @" "; // lazily set font of `UITextView`. self.attributedText = originalText; - + label = [[UILabel alloc] init]; label.textColor = [self.class defaultPlaceholderColor]; label.numberOfLines = 0; label.userInteractionEnabled = NO; objc_setAssociatedObject(self, @selector(placeholderLabel), label, OBJC_ASSOCIATION_RETAIN_NONATOMIC); - + self.needsUpdateFont = YES; [self updatePlaceholderLabel]; self.needsUpdateFont = NO; - + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePlaceholderLabel) name:UITextViewTextDidChangeNotification object:self]; - + for (NSString *key in self.class.observingKeys) { [self addObserver:self forKeyPath:key options:NSKeyValueObservingOptionNew context:nil]; } @@ -173,19 +173,25 @@ - (void)updatePlaceholderLabel { [self.placeholderLabel removeFromSuperview]; return; } - + [self insertSubview:self.placeholderLabel atIndex:0]; - + if (self.needsUpdateFont) { - self.placeholderLabel.font = self.font; + [self.placeholderLabel.attributedText enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, self.placeholderLabel.attributedText.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { + if(value != nil) { + self.placeholderLabel.font = (UIFont *)value; + } else { + self.placeholderLabel.font = self.font; + } + }]; self.needsUpdateFont = NO; } self.placeholderLabel.textAlignment = self.textAlignment; - + // `NSTextContainer` is available since iOS 7 CGFloat lineFragmentPadding; UIEdgeInsets textContainerInset; - + #pragma deploymate push "ignored-api-availability" // iOS 7+ if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { @@ -193,13 +199,13 @@ - (void)updatePlaceholderLabel { textContainerInset = self.textContainerInset; } #pragma deploymate pop - + // iOS 6 else { lineFragmentPadding = 5; textContainerInset = UIEdgeInsetsMake(8, 0, 8, 0); } - + CGFloat x = lineFragmentPadding + textContainerInset.left; CGFloat y = textContainerInset.top; CGFloat width = CGRectGetWidth(self.bounds) - x - lineFragmentPadding - textContainerInset.right; @@ -208,3 +214,4 @@ - (void)updatePlaceholderLabel { } @end +