Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide font customization options #48

Closed
orschaef opened this issue May 6, 2014 · 3 comments
Closed

Provide font customization options #48

orschaef opened this issue May 6, 2014 · 3 comments

Comments

@orschaef
Copy link

orschaef commented May 6, 2014

It would be nice to customize the fonts of the title and message "labels" (I saw you render the text in drawRect).
I discovered the static constants in the implementation file (kTWMessageViewTitleFont and kTWMessageViewDescriptionFont - didn't dive into details).

Wouldn't that be a nice and a low hanging fruit enhancement?

Greetings.

@terryworona
Copy link
Owner

It would be easy to add a new protocol function to the TWMessageBarStyleSheet:

 - (UIFont *)fontForMessageType:(TWMessageBarMessageType)type;

Don't have time to tackle this myself. Why don't you take a swing at it?

@albertmartga
Copy link

fyi here's a patch for this I just did:

commit 7ede0a4d85b0f72d05479d39192ce8474ff0b40b
Author: Albert <albert@bloomfits.com>
Date:   Wed Jun 4 11:36:16 2014 +0200

    inapp push notification design

diff --git a/TWMessageBarManager/Classes/TWMessageBarManager.h b/TWMessageBarManager/Classes/TWMessageBarManager.h
index 77a7e4f..4f22265 100644
--- a/TWMessageBarManager/Classes/TWMessageBarManager.h
+++ b/TWMessageBarManager/Classes/TWMessageBarManager.h
@@ -45,6 +45,12 @@ typedef enum {
  */
 - (UIImage *)iconImageForMessageType:(TWMessageBarMessageType)type;

+- (UIFont *)fontTitleForMessageType:(TWMessageBarMessageType)type;
+- (UIFont *)fontDescriptionForMessageType:(TWMessageBarMessageType)type;
+- (UIColor *)fontTitleColorForMessageType:(TWMessageBarMessageType)type;
+- (UIColor *)fontDescriptionColorForMessageType:(TWMessageBarMessageType)type;
+
+
 @end

 @interface TWMessageBarManager : NSObject
diff --git a/TWMessageBarManager/Classes/TWMessageBarManager.m b/TWMessageBarManager/Classes/TWMessageBarManager.m
index 454164e..4f6b24b 100644
--- a/TWMessageBarManager/Classes/TWMessageBarManager.m
+++ b/TWMessageBarManager/Classes/TWMessageBarManager.m
@@ -5,6 +5,7 @@
 //  Copyright (c) 2013 Terry Worona. All rights reserved.
 //

+
 #import "TWMessageBarManager.h"

 // Quartz
@@ -552,29 +553,29 @@ static UIColor *kTWDefaultMessageBarStyleSheetInfoStrokeColor = nil;
             NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
             paragraphStyle.alignment = NSTextAlignmentLeft;

-            [kTWMessageViewTitleColor set];
+            [[styleSheet fontTitleColorForMessageType:self.messageType] set];
             [self.titleString drawWithRect:CGRectMake(xOffset, yOffset, titleLabelSize.width, titleLabelSize.height)
                                    options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
-                                attributes:@{NSFontAttributeName:kTWMessageViewTitleFont, NSForegroundColorAttributeName:kTWMessageViewTitleColor, NSParagraphStyleAttributeName:paragraphStyle}
+                                attributes:@{NSFontAttributeName:[styleSheet fontTitleForMessageType:self.messageType], NSForegroundColorAttributeName:[styleSheet fontTitleColorForMessageType:self.messageType], NSParagraphStyleAttributeName:paragraphStyle}
                                    context:nil];

             yOffset += titleLabelSize.height;

-            [kTWMessageViewDescriptionColor set];
+            [[styleSheet fontDescriptionColorForMessageType:self.messageType] set];
             [self.descriptionString drawWithRect:CGRectMake(xOffset, yOffset, descriptionLabelSize.width, descriptionLabelSize.height)
                                          options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
-                                      attributes:@{NSFontAttributeName:kTWMessageViewDescriptionFont, NSForegroundColorAttributeName:kTWMessageViewDescriptionColor, NSParagraphStyleAttributeName:paragraphStyle}
+                                      attributes:@{NSFontAttributeName:[styleSheet fontDescriptionForMessageType:self.messageType], NSForegroundColorAttributeName:[styleSheet fontDescriptionColorForMessageType:self.messageType], NSParagraphStyleAttributeName:paragraphStyle}
                                          context:nil];
         }
         else
         {
-            [kTWMessageViewTitleColor set];
-            [self.titleString drawInRect:CGRectMake(xOffset, yOffset, titleLabelSize.width, titleLabelSize.height) withFont:kTWMessageViewTitleFont lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentLeft];
+            [[styleSheet fontTitleColorForMessageType:self.messageType] set];
+            [self.titleString drawInRect:CGRectMake(xOffset, yOffset, titleLabelSize.width, titleLabelSize.height) withFont:[styleSheet fontTitleForMessageType:self.messageType] lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentLeft];

             yOffset += titleLabelSize.height;

-            [kTWMessageViewDescriptionColor set];
-            [self.descriptionString drawInRect:CGRectMake(xOffset, yOffset, descriptionLabelSize.width, descriptionLabelSize.height) withFont:kTWMessageViewDescriptionFont lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentLeft];
+            [[styleSheet fontDescriptionColorForMessageType:self.messageType] set];
+            [self.descriptionString drawInRect:CGRectMake(xOffset, yOffset, descriptionLabelSize.width, descriptionLabelSize.height) withFont:[styleSheet fontDescriptionForMessageType:self.messageType] lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentLeft];
         }
     }
 }
@@ -605,12 +606,22 @@ static UIColor *kTWDefaultMessageBarStyleSheetInfoStrokeColor = nil;

 - (CGSize)titleSize
 {
+
+    UIFont *titleFont;
+    if ([self.delegate respondsToSelector:@selector(styleSheetForMessageView:)])
+    {
+        id<TWMessageBarStyleSheet> styleSheet = [self.delegate styleSheetForMessageView:self];
+        titleFont=[styleSheet fontTitleForMessageType:self.messageType];
+    } else {
+        titleFont=kTWMessageViewTitleFont;
+    }
+        
     CGSize boundedSize = CGSizeMake([self availableWidth], CGFLOAT_MAX);
     CGSize titleLabelSize;

     if ([[UIDevice currentDevice] isRunningiOS7OrLater])
     {
-        NSDictionary *titleStringAttributes = [NSDictionary dictionaryWithObject:kTWMessageViewTitleFont forKey: NSFontAttributeName];
+        NSDictionary *titleStringAttributes = [NSDictionary dictionaryWithObject:titleFont forKey: NSFontAttributeName];
         titleLabelSize = [self.titleString boundingRectWithSize:boundedSize
                                                         options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin
                                                      attributes:titleStringAttributes
@@ -618,7 +629,7 @@ static UIColor *kTWDefaultMessageBarStyleSheetInfoStrokeColor = nil;
     }
     else
     {
-        titleLabelSize = [_titleString sizeWithFont:kTWMessageViewTitleFont constrainedToSize:boundedSize lineBreakMode:NSLineBreakByTruncatingTail];
+        titleLabelSize = [_titleString sizeWithFont:titleFont constrainedToSize:boundedSize lineBreakMode:NSLineBreakByTruncatingTail];
     }

     return CGSizeMake(ceilf(titleLabelSize.width), ceilf(titleLabelSize.height));
@@ -626,12 +637,22 @@ static UIColor *kTWDefaultMessageBarStyleSheetInfoStrokeColor = nil;

 - (CGSize)descriptionSize
 {
+    UIFont *descriptionFont;
+    if ([self.delegate respondsToSelector:@selector(styleSheetForMessageView:)])
+    {
+        id<TWMessageBarStyleSheet> styleSheet = [self.delegate styleSheetForMessageView:self];
+        descriptionFont=[styleSheet fontDescriptionForMessageType:self.messageType];
+    } else {
+        descriptionFont=kTWMessageViewDescriptionFont;
+    }
+    
+    
     CGSize boundedSize = CGSizeMake([self availableWidth], CGFLOAT_MAX);
     CGSize descriptionLabelSize;

     if ([[UIDevice currentDevice] isRunningiOS7OrLater])
     {
-        NSDictionary *descriptionStringAttributes = [NSDictionary dictionaryWithObject:kTWMessageViewDescriptionFont forKey: NSFontAttributeName];
+        NSDictionary *descriptionStringAttributes = [NSDictionary dictionaryWithObject:descriptionFont forKey: NSFontAttributeName];
         descriptionLabelSize = [self.descriptionString boundingRectWithSize:boundedSize
                                                                     options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin
                                                                  attributes:descriptionStringAttributes
@@ -639,7 +660,7 @@ static UIColor *kTWDefaultMessageBarStyleSheetInfoStrokeColor = nil;
     }
     else
     {
-        descriptionLabelSize = [_descriptionString sizeWithFont:kTWMessageViewDescriptionFont constrainedToSize:boundedSize lineBreakMode:NSLineBreakByTruncatingTail];
+        descriptionLabelSize = [_descriptionString sizeWithFont:descriptionFont constrainedToSize:boundedSize lineBreakMode:NSLineBreakByTruncatingTail];
     }

     return CGSizeMake(ceilf(descriptionLabelSize.width), ceilf(descriptionLabelSize.height));
@@ -760,6 +781,21 @@ static UIColor *kTWDefaultMessageBarStyleSheetInfoStrokeColor = nil;
     return iconImage;
 }

+
+- (UIFont *)fontTitleForMessageType:(TWMessageBarMessageType)type{
+    return kTWMessageViewTitleFont;
+}
+- (UIFont *)fontDescriptionForMessageType:(TWMessageBarMessageType)type{
+    return kTWMessageViewDescriptionFont;
+    
+}
+- (UIColor *)fontTitleColorForMessageType:(TWMessageBarMessageType)type{
+    return kTWMessageViewTitleColor;
+}
+- (UIColor *)fontDescriptionColorForMessageType:(TWMessageBarMessageType)type{
+    return kTWMessageViewDescriptionColor;
+}
+
 @end

 @implementation TWMessageWindow

@terryworona
Copy link
Owner

This was pulled in via #52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants