Skip to content

Instantly share code, notes, and snippets.

@Adnan1990
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save Adnan1990/9480765 to your computer and use it in GitHub Desktop.

Select an option

Save Adnan1990/9480765 to your computer and use it in GitHub Desktop.
Using Attributed String in IOS
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UILabel *label;
@end
@implementation ViewController
- (NSAttributedString *) attributedText{
NSString *string = @"Viz Teck";
NSMutableAttributedString *result = [[NSMutableAttributedString alloc]
initWithString:string];
NSDictionary *attributesForFirstWord = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],
NSForegroundColorAttributeName : [UIColor redColor],
NSBackgroundColorAttributeName : [UIColor blackColor]
};
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor darkGrayColor];
shadow.shadowOffset = CGSizeMake(4.0f, 4.0f);
NSDictionary *attributesForSecondWord = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],
NSForegroundColorAttributeName : [UIColor whiteColor],
NSBackgroundColorAttributeName : [UIColor redColor],
NSShadowAttributeName : shadow
};
/* Find the string "Viz" in the whole string and sets its attribute */
[result setAttributes:attributesForFirstWord
range:[string rangeOfString:@"Viz"]];
/* Do the same thing for the string "Teck" */
[result setAttributes:attributesForSecondWord
range:[string rangeOfString:@"Teck"]];
return [[NSAttributedString alloc] initWithAttributedString:result];
}
- (void)viewDidLoad{
[super viewDidLoad];
self.label = [[UILabel alloc] init];
self.label.backgroundColor = [UIColor clearColor];
self.label.attributedText = [self attributedText];
[self.label sizeToFit];
self.label.center = self.view.center;
[self.view addSubview:self.label];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment