Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // ChangeStyleOfText
- //
- // Created by Nguyen Viet Tien on 9/22/14.
- // Copyright (c) 2014 com.distance. All rights reserved.
- //
- #import "ViewController.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- //Set text attributed with function changeStyleOfSentence
- [_lblText setAttributedText:[self changeStyleOfSentence:@"A chocolate chip <-cookie-> is a cookie with <-pieces of-> chocolate."]];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- -(NSMutableAttributedString* )changeStyleOfSentence:(NSString* )sentence{
- NSString * stringInput = sentence;
- NSError *error = NULL;
- NSRegularExpression *regexOne = [NSRegularExpression regularExpressionWithPattern:@"(<-).*?(->)"
- options:0 error:&error];
- NSRegularExpression *regexTwo = [NSRegularExpression regularExpressionWithPattern:@"(?<=<-).*?(?=->)"
- options:0 error:&error];
- NSMutableAttributedString *stringBold;
- NSString *result;
- if (regexTwo) {
- NSRange rangeOfTwoMatch = [regexTwo rangeOfFirstMatchInString:stringInput options:0 range:NSMakeRange(0, [stringInput length])];
- if (!NSEqualRanges(rangeOfTwoMatch, NSMakeRange(NSNotFound, 0))) {
- result = [stringInput substringWithRange:rangeOfTwoMatch];
- //[resultTwo addObject:[stringInput substringWithRange:rangeOfTwoMatch]];
- }
- //Output : result = cookie
- NSLog(@"Style--- Result1: %@",result);
- }
- NSRange rangeOfFirstMatch;
- NSString* result2;
- if (regexOne) {
- rangeOfFirstMatch = [regexOne rangeOfFirstMatchInString:stringInput options:0 range:NSMakeRange(0, [stringInput length])];
- if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
- result2 = [stringInput substringWithRange:rangeOfFirstMatch];
- //[resultOne addObject:[stringInput substringWithRange:rangeOfFirstMatch]];
- }
- //Output : result2 = <-cookie->
- NSLog(@"Style--- Result2: %@",result2);
- }
- //Bold the word <-cookie->
- stringBold = [[NSMutableAttributedString alloc] initWithString:stringInput];
- UIFontDescriptor* fontDescriptor = [UIFontDescriptor
- preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
- UIFontDescriptor* boldFontDescriptor = [fontDescriptor
- fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
- [stringBold beginEditing];
- [stringBold addAttribute:NSFontAttributeName
- value:[UIFont fontWithDescriptor:boldFontDescriptor size: 0.0]
- range:rangeOfFirstMatch];
- [stringBold endEditing];
- //Create a bold word "cookie"
- NSMutableAttributedString *stringSelectedBold = [[NSMutableAttributedString alloc] initWithString:result];
- [stringSelectedBold beginEditing];
- [stringSelectedBold addAttribute:NSFontAttributeName
- value:[UIFont fontWithDescriptor:boldFontDescriptor size: 0.0]
- range:NSMakeRange(0, [result length])];
- [stringSelectedBold endEditing];
- //Replace bold word "<-cookie->" by bold word "cookie"
- [stringBold replaceCharactersInRange:rangeOfFirstMatch withAttributedString:stringSelectedBold];
- return stringBold;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement