Skip to content

Commit

Permalink
Merge pull request #43 from digitalcatnip/ReadMe
Browse files Browse the repository at this point in the history
Add support for custom fonts and attributed strings on Title and Subtitle
  • Loading branch information
nimati authored Feb 13, 2017
2 parents d9186e9 + f5b2206 commit a24f993
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 17 deletions.
119 changes: 113 additions & 6 deletions Example/FCAlertView/ExampleViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ - (void)viewDidLoad {
@"setting" : @"Off",
@"status" : @0,
@"customIndicator" : @0,
@"selection" : @[@"Off", @"On"]},
@{@"title" : @"Title Custom Font",
@"description" : @"Specify a different font for the title - such as Avenir!",
@"setting" : @"Off",
@"status" : @0,
@"customIndicator" : @0,
@"selection" : @[@"Off", @"On"]},
@{@"title" : @"Subtitle Custom Font",
@"description" : @"Specify a different font for the subtitle - such as Avenir!",
@"setting" : @"Off",
@"status" : @0,
@"customIndicator" : @0,
@"selection" : @[@"Off", @"On"]},
@{@"title" : @"Title Attributed Text",
@"description" : @"Specify formatted text in the title.",
@"setting" : @"Off",
@"status" : @0,
@"customIndicator" : @0,
@"selection" : @[@"Off", @"On"]},
@{@"title" : @"Title Custom Font",
@"description" : @"Specify formatted text in the subtitle.",
@"setting" : @"Off",
@"status" : @0,
@"customIndicator" : @0,
@"selection" : @[@"Off", @"On"]}];

_alertViewOptionsOriginal = @[@{@"title" : @"Custom Image",
Expand Down Expand Up @@ -735,6 +759,10 @@ - (IBAction)showAlert:(id)sender {

FCAlertView *alert = [[FCAlertView alloc] init]; // 2) Add This Where you Want to Create an FCAlertView

//For use with Attributed Text demo
BOOL useAttributedTitle = NO;
BOOL useAttributedSubTitle = NO;

// ALL CUSTOMIZATIONS - DON'T COPY THIS TO YOUR CODE // 3) Customize as you need to
// Go Through README.md or this example to understand how to use these customizations work and how to add them
// It's best to include these customiztions before PRESENTING THE FCALERTVIEW
Expand Down Expand Up @@ -900,6 +928,26 @@ - (IBAction)showAlert:(id)sender {
if (![[[_alertViewLatestOptions objectAtIndex:7] objectForKey:@"setting"] isEqual:@"Off"])
alert.hideSeparatorLineView = YES;

// Set Title Font to Avenir

if (![[[_alertViewLatestOptions objectAtIndex:8] objectForKey:@"setting"] isEqual:@"Off"])
alert.titleFont = [UIFont fontWithName:@"Avenir" size:18.0];

// Set Subtitle Font to Avenir

if (![[[_alertViewLatestOptions objectAtIndex:9] objectForKey:@"setting"] isEqual:@"Off"])
alert.subtitleFont = [UIFont fontWithName:@"Avenir" size:15.0];

// Set Title to Attributed Text

if (![[[_alertViewLatestOptions objectAtIndex:10] objectForKey:@"setting"] isEqual:@"Off"])
useAttributedTitle = YES;

// Set Subtitle to Attributed Text

if (![[[_alertViewLatestOptions objectAtIndex:11] objectForKey:@"setting"] isEqual:@"Off"])
useAttributedSubTitle = YES;

// ALERT ANIMATIONS SECTION
// Animate In Options

Expand Down Expand Up @@ -968,13 +1016,72 @@ - (IBAction)showAlert:(id)sender {

alert.delegate = self; // 5) Add This is You Would like to Use Buttons without Action Blocks

[alert showAlertInView:self
withTitle:_alertTitle
withSubtitle:@"This is my alert's subtitle. Keep it short and concise. 😜"
withCustomImage:_alertImage
withDoneButtonTitle:nil
andButtons:self.arrayOfButtonTitles];
if (useAttributedSubTitle && useAttributedTitle) {
[alert showAlertWithAttributedTitle:[self getAttributedTitle]
withAttributedSubtitle:[self getAttributedSubtitle]
withCustomImage:_alertImage
withDoneButtonTitle:nil
andButtons:self.arrayOfButtonTitles];
} else if (useAttributedTitle) {
[alert showAlertWithAttributedTitle:[self getAttributedTitle]
withSubtitle:@"This is my alert's subtitle. Keep it short and concise. 😜"
withCustomImage:_alertImage
withDoneButtonTitle:nil
andButtons:self.arrayOfButtonTitles];
} else if (useAttributedSubTitle) {
[alert showAlertWithTitle:_alertTitle
withAttributedSubtitle:[self getAttributedSubtitle]
withCustomImage:_alertImage
withDoneButtonTitle:nil
andButtons:self.arrayOfButtonTitles];
} else {
[alert showAlertInView:self
withTitle:_alertTitle
withSubtitle:@"This is my alert's subtitle. Keep it short and concise. 😜"
withCustomImage:_alertImage
withDoneButtonTitle:nil
andButtons:self.arrayOfButtonTitles];
}
}

-(NSAttributedString *)getAttributedTitle {
NSString *text = @"Alert Title";

NSDictionary *attrib = @{
NSForegroundColorAttributeName: [UIColor blackColor],
NSFontAttributeName: [UIFont systemFontOfSize:18.0 weight:UIFontWeightRegular]
};
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text attributes:attrib];

NSRange nameRange = [text rangeOfString:@"Title"];
UIFont *italics = [UIFont systemFontOfSize:18.0 weight:UIFontWeightHeavy];
[str setAttributes:@{NSFontAttributeName:italics} range:nameRange];

return str;
}

-(NSAttributedString *)getAttributedSubtitle {
NSString *text = @"This is my alert's subtitle. Keep it short and concise. 😜";

NSDictionary *attrib = @{
NSForegroundColorAttributeName: [UIColor blackColor],
NSFontAttributeName: [UIFont systemFontOfSize:15.0 weight:UIFontWeightRegular]
};
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text attributes:attrib];

NSRange nameRange = [text rangeOfString:@"my"];
UIFont *newFont = [UIFont systemFontOfSize:15.0 weight:UIFontWeightHeavy];
[str setAttributes:@{NSFontAttributeName:newFont} range:nameRange];

nameRange = [text rangeOfString:@"short"];
newFont = [UIFont systemFontOfSize:15.0 weight:UIFontWeightThin];
[str setAttributes:@{NSFontAttributeName:newFont} range:nameRange];

nameRange = [text rangeOfString:@"concise"];
newFont = [UIFont systemFontOfSize:15.0 weight:UIFontWeightHeavy];
[str setAttributes:@{NSFontAttributeName:newFont} range:nameRange];

return str;
}

#pragma mark - Handling Orientation Change for Example App
Expand Down
11 changes: 11 additions & 0 deletions FCAlertView/Classes/FCAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subTitle;
@property (nonatomic, retain) NSAttributedString *attributedTitle;
@property (nonatomic, retain) NSAttributedString *attributedSubTitle;
@property (nonatomic, retain) UIFont *titleFont;
@property (nonatomic, retain) UIFont *subtitleFont;

// AlertView Background

Expand Down Expand Up @@ -143,6 +147,13 @@ typedef void (^FCRatingBlock)(NSInteger rating);

- (void) showAlertWithTitle:(NSString *)title withSubtitle:(NSString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons;

- (void) showAlertWithAttributedTitle:(NSAttributedString *)title withSubtitle:(NSString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons;

- (void) showAlertWithTitle:(NSString *)title withAttributedSubtitle:(NSAttributedString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons;

- (void) showAlertWithAttributedTitle:(NSAttributedString *)title withAttributedSubtitle:(NSAttributedString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons;


- (void)setAlertViewAttributes:(NSString *)title withSubtitle:(NSString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons;

// Dismissing AlertView
Expand Down
104 changes: 94 additions & 10 deletions FCAlertView/Classes/FCAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ - (id)init
_fullCircleCustomImage = NO;
_hideSeparatorLineView = NO;
_customImageScale = 1;

_titleFont = [UIFont systemFontOfSize:18.0f weight:UIFontWeightMedium];
_subtitleFont = nil;
defaultSpacing = [self configureAVWidth];
defaultHeight = [self configureAVHeight];

Expand Down Expand Up @@ -148,8 +149,8 @@ - (void) checkCustomizationValid {
}
}

if (_subTitle == nil || [_subTitle isEqualToString:@""])
if (_title == nil || [_title isEqualToString:@""])
if (![self hasSubTitle])
if (![self hasTitle])
NSLog(@"FCAlertView Warning: Your Alert should have a title and/or subtitle.");

if (doneTitle == nil || [doneTitle isEqualToString:@""]) {
Expand Down Expand Up @@ -182,6 +183,17 @@ - (void) safetyCloseCheck {

}

#pragma mark - Title Validation
-(BOOL)hasTitle {
return (_title != nil && _title.length > 0) ||
(_attributedTitle != nil && _attributedTitle.length > 0);
}

-(BOOL)hasSubTitle {
return (_subTitle != nil && _subTitle.length > 0) ||
(_attributedSubTitle != nil && _attributedSubTitle.length > 0);
}

#pragma mark - Touch Events

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
Expand Down Expand Up @@ -276,7 +288,7 @@ - (void)drawRect:(CGRect)rect {
result.width - defaultSpacing,
defaultHeight - 30);

if (_title == nil || _title.length == 0) // Frames for when AlertView doesn't contain a title
if (![self hasTitle]) // Frames for when AlertView doesn't contain a title
alertViewFrame = CGRectMake(self.frame.size.width/2 - ((result.width - defaultSpacing)/2),
self.frame.size.height/2 - ((alertViewFrame.size.height - 50)/2),
result.width - defaultSpacing,
Expand Down Expand Up @@ -333,7 +345,7 @@ - (void)drawRect:(CGRect)rect {

NSInteger descriptionLevel = 45.0f;

if (_title == nil || _title.length == 0) {
if (![self hasTitle]) {

descriptionLevel = 15.0f;
alertViewFrame = CGRectMake(alertViewFrame.origin.x,
Expand All @@ -346,14 +358,18 @@ - (void)drawRect:(CGRect)rect {
descriptionLevel + (alertViewWithVector * 30),
alertViewFrame.size.width - 50.0f,
60.0f)];

if (_title != nil)
if (_subtitleFont != nil)
descriptionLabel.font = self.subtitleFont;
else if (_title != nil)
descriptionLabel.font = [UIFont systemFontOfSize:15.0f weight:UIFontWeightLight];
else
descriptionLabel.font = [UIFont systemFontOfSize:16.0f weight:UIFontWeightRegular];

descriptionLabel.textColor = self.subTitleColor;
descriptionLabel.text = self.subTitle;
if (_subTitle == nil)
descriptionLabel.attributedText = self.attributedSubTitle;
else
descriptionLabel.text = self.subTitle;
descriptionLabel.textAlignment = NSTextAlignmentCenter;
descriptionLabel.adjustsFontSizeToFitWidth = NO;

Expand Down Expand Up @@ -446,10 +462,13 @@ - (void)drawRect:(CGRect)rect {
20.0f + (alertViewWithVector * 30),
alertViewFrame.size.width - 30.0f,
20.0f)];
titleLabel.font = [UIFont systemFontOfSize:18.0f weight:UIFontWeightMedium];
titleLabel.font = self.titleFont;
titleLabel.numberOfLines = 1;
titleLabel.textColor = self.titleColor;
titleLabel.text = self.title;
if (_title == nil)
titleLabel.attributedText = self.attributedTitle;
else
titleLabel.text = self.title;
titleLabel.textAlignment = NSTextAlignmentCenter;

// SEPARATOR LINE - Seperating Header View with Button View
Expand Down Expand Up @@ -1166,6 +1185,71 @@ - (void) showAlertWithTitle:(NSString *)title withSubtitle:(NSString *)subTitle

}

- (void) showAlertWithAttributedTitle:(NSAttributedString *)title withSubtitle:(NSString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons {

self.attributedTitle = title;
[self setAlertViewAttributes:nil withSubtitle:subTitle withCustomImage:image withDoneButtonTitle:done andButtons:buttons];
UIWindow *window = [UIApplication sharedApplication].windows.lastObject;

// Blur Effect
if (_blurBackground && NSClassFromString(@"UIVisualEffectView") != nil) {
UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
backgroundVisualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
backgroundVisualEffectView.frame = window.bounds;
_alertBackground.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5];
[window addSubview:backgroundVisualEffectView];
}

// Adding Alert

[window addSubview:self];
[window bringSubviewToFront:self];
}

- (void) showAlertWithTitle:(NSString *)title withAttributedSubtitle:(NSAttributedString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons {

self.attributedSubTitle = subTitle;
[self setAlertViewAttributes:title withSubtitle:nil withCustomImage:image withDoneButtonTitle:done andButtons:buttons];
UIWindow *window = [UIApplication sharedApplication].windows.lastObject;

// Blur Effect
if (_blurBackground && NSClassFromString(@"UIVisualEffectView") != nil) {
UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
backgroundVisualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
backgroundVisualEffectView.frame = window.bounds;
_alertBackground.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5];
[window addSubview:backgroundVisualEffectView];
}

// Adding Alert

[window addSubview:self];
[window bringSubviewToFront:self];
}

- (void) showAlertWithAttributedTitle:(NSAttributedString *)title withAttributedSubtitle:(NSAttributedString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons {

self.attributedTitle = title;
self.attributedSubTitle = subTitle;
[self setAlertViewAttributes:nil withSubtitle:nil withCustomImage:image withDoneButtonTitle:done andButtons:buttons];
UIWindow *window = [UIApplication sharedApplication].windows.lastObject;

// Blur Effect
if (_blurBackground && NSClassFromString(@"UIVisualEffectView") != nil) {
UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
backgroundVisualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
backgroundVisualEffectView.frame = window.bounds;
_alertBackground.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5];
[window addSubview:backgroundVisualEffectView];
}

// Adding Alert

[window addSubview:self];
[window bringSubviewToFront:self];
}


- (void)setAlertViewAttributes:(NSString *)title withSubtitle:(NSString *)subTitle withCustomImage:(UIImage *)image withDoneButtonTitle:(NSString *)done andButtons:(NSArray *)buttons{

self.title = title;
Expand Down
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Use this line to apply a beautiful dark theme to your FCAlert:
alert.darkTheme = YES;
```

#### Title and Subtitle Colors
#### Title and Subtitle Styling

Change Title Color by Adding

Expand All @@ -185,6 +185,39 @@ Change SubTitle Color by Adding
alert.subTitleColor = alertView.flatBlue;
```

Change Title Font by Adding

```Objective-C
alert.titleFont = [UIFont fontWithName:@"Avenir" size:30.0];
```
Change SubTitle Font by Adding
```Objective-C
alert.subtitleFont = [UIFont fontWithName:@"Avenir" size:15.0];
```

You can also use Attributed text in the title or the subtitle!

```Objective-C
NSString *text = @"My Alert Title";

NSDictionary *attrib = @{
NSForegroundColorAttributeName: [UIColor blackColor],
NSFontAttributeName: [UIFont systemFontOfSize:18.0 weight:UIFontWeightRegular]
};
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text attributes:attrib];

NSRange nameRange = [text rangeOfString:@"Title"];
UIFont *italics = [UIFont systemFontOfSize:18.0 weight:UIFontWeightHeavy];
[str setAttributes:@{NSFontAttributeName:italics} range:nameRange];
// Use the string as a title!
[alert showAlertWithAttributedTitle:str withSubtitle:@"This is my subtitle!" withCustomImage:_alertImage withDoneButtonTitle:nil andButtons:self.arrayOfButtonTitles];
// Or use it as a subtitle!
[alert showAlertWithTitle:@"My Title" withAttributedSubtitle:str withCustomImage:_alertImage withDoneButtonTitle:nil andButtons:self.arrayOfButtonTitles];
// Or use it as both!
[alert showAlertWithAttributedTitle:str withAttributedSubtitle:str withCustomImage:_alertImage withDoneButtonTitle:nil andButtons:self.arrayOfButtonTitles];
```
#### Button Colors
Change Title Color of Buttons
Expand Down

0 comments on commit a24f993

Please sign in to comment.