Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // ChangeStyleOfText
  4. //
  5. // Created by Nguyen Viet Tien on 9/22/14.
  6. // Copyright (c) 2014 com.distance. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12.  
  13. @end
  14.  
  15. @implementation ViewController
  16.  
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view, typically from a nib.
  21.  
  22. //Set text attributed with function changeStyleOfSentence
  23. [_lblText setAttributedText:[self changeStyleOfSentence:@"A chocolate chip <-cookie-> is a cookie with <-pieces of-> chocolate."]];
  24. }
  25.  
  26. - (void)didReceiveMemoryWarning
  27. {
  28. [super didReceiveMemoryWarning];
  29. // Dispose of any resources that can be recreated.
  30. }
  31.  
  32. -(NSMutableAttributedString* )changeStyleOfSentence:(NSString* )sentence{
  33.  
  34. NSString * stringInput = sentence;
  35.  
  36. NSError *error = NULL;
  37.  
  38. NSRegularExpression *regexOne = [NSRegularExpression regularExpressionWithPattern:@"(<-).*?(->)"
  39. options:0 error:&error];
  40.  
  41. NSRegularExpression *regexTwo = [NSRegularExpression regularExpressionWithPattern:@"(?<=<-).*?(?=->)"
  42. options:0 error:&error];
  43. NSMutableAttributedString *stringBold;
  44. NSString *result;
  45.  
  46. if (regexTwo) {
  47. NSRange rangeOfTwoMatch = [regexTwo rangeOfFirstMatchInString:stringInput options:0 range:NSMakeRange(0, [stringInput length])];
  48. if (!NSEqualRanges(rangeOfTwoMatch, NSMakeRange(NSNotFound, 0))) {
  49. result = [stringInput substringWithRange:rangeOfTwoMatch];
  50.  
  51. //[resultTwo addObject:[stringInput substringWithRange:rangeOfTwoMatch]];
  52. }
  53. //Output : result = cookie
  54. NSLog(@"Style--- Result1: %@",result);
  55. }
  56.  
  57. NSRange rangeOfFirstMatch;
  58. NSString* result2;
  59.  
  60. if (regexOne) {
  61. rangeOfFirstMatch = [regexOne rangeOfFirstMatchInString:stringInput options:0 range:NSMakeRange(0, [stringInput length])];
  62. if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
  63. result2 = [stringInput substringWithRange:rangeOfFirstMatch];
  64.  
  65. //[resultOne addObject:[stringInput substringWithRange:rangeOfFirstMatch]];
  66. }
  67. //Output : result2 = <-cookie->
  68. NSLog(@"Style--- Result2: %@",result2);
  69. }
  70.  
  71. //Bold the word <-cookie->
  72.  
  73. stringBold = [[NSMutableAttributedString alloc] initWithString:stringInput];
  74.  
  75. UIFontDescriptor* fontDescriptor = [UIFontDescriptor
  76. preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
  77. UIFontDescriptor* boldFontDescriptor = [fontDescriptor
  78. fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
  79. [stringBold beginEditing];
  80. [stringBold addAttribute:NSFontAttributeName
  81. value:[UIFont fontWithDescriptor:boldFontDescriptor size: 0.0]
  82. range:rangeOfFirstMatch];
  83. [stringBold endEditing];
  84.  
  85.  
  86. //Create a bold word "cookie"
  87. NSMutableAttributedString *stringSelectedBold = [[NSMutableAttributedString alloc] initWithString:result];
  88.  
  89. [stringSelectedBold beginEditing];
  90. [stringSelectedBold addAttribute:NSFontAttributeName
  91. value:[UIFont fontWithDescriptor:boldFontDescriptor size: 0.0]
  92. range:NSMakeRange(0, [result length])];
  93. [stringSelectedBold endEditing];
  94.  
  95.  
  96. //Replace bold word "<-cookie->" by bold word "cookie"
  97. [stringBold replaceCharactersInRange:rangeOfFirstMatch withAttributedString:stringSelectedBold];
  98.  
  99.  
  100. return stringBold;
  101.  
  102. }
  103.  
  104.  
  105. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement