Last active
August 29, 2015 13:57
-
-
Save Adnan1990/9480765 to your computer and use it in GitHub Desktop.
Using Attributed String in IOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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