Skip to content

Commit

Permalink
Add support to attributed text
Browse files Browse the repository at this point in the history
  • Loading branch information
Diogo Autilio committed Oct 8, 2014
1 parent 3db649f commit 7da660e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 6 additions & 1 deletion SCLAlertView/SCLAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ - (id)init

// View text
_viewText.editable = NO;
_viewText.allowsEditingTextAttributes = YES;
_viewText.textAlignment = NSTextAlignmentCenter;
_viewText.font = [UIFont fontWithName:kDefaultFont size:14.0f];

Expand Down Expand Up @@ -354,7 +355,11 @@ -(SCLAlertViewResponder *)showTitle:(UIViewController *)vc title:(NSString *)tit
// Subtitle
if([subTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length > 0)
{
_viewText.text = subTitle;
// No custom text
if(_viewText.attributedText.string.length <= 0)
{
_viewText.text = subTitle;
}

// Adjust text view size, if necessary
NSString *str = subTitle;
Expand Down
16 changes: 15 additions & 1 deletion SCLAlertViewExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ @interface ViewController ()
NSString *kInfoTitle = @"Info";
NSString *kSubtitle = @"You've just displayed this awesome Pop Up View";
NSString *kButtonTitle = @"Done";
NSString *kAttributeTitle = @"Attributed string operation successfully completed.";

@implementation ViewController

Expand Down Expand Up @@ -106,8 +107,21 @@ - (IBAction)showAdvanced:(id)sender
[alert addButton:@"Show Name" actionBlock:^(void) {
NSLog(@"Text value: %@", textField.text);
}];

NSMutableAttributedString *subTitle = [[NSMutableAttributedString alloc]initWithString:kAttributeTitle];

NSRange redRange = [kAttributeTitle rangeOfString:@"Attributed" options:NSCaseInsensitiveSearch];
[subTitle addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];

NSRange greenRange = [kAttributeTitle rangeOfString:@"successfully" options:NSCaseInsensitiveSearch];
[subTitle addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:greenRange];

NSRange underline = [kAttributeTitle rangeOfString:@"completed" options:NSCaseInsensitiveSearch];
[subTitle addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:underline];

alert.viewText.attributedText = subTitle;

[alert showTitle:self title:@"Congratulations" subTitle:@"Operation successfully completed." style:Success closeButtonTitle:@"Done" duration:0.0f];
[alert showTitle:self title:@"Congratulations" subTitle:alert.viewText.attributedText.string style:Success closeButtonTitle:@"Done" duration:0.0f];
}

- (IBAction)showWithDuration:(id)sender
Expand Down

0 comments on commit 7da660e

Please sign in to comment.