Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 1.80 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. NSString sizeWithFont, issue
  2. CGSize sz = [@"Test text to hilight without new line for testing" sizeWithFont:CGTextLabel.font];
  3.  
  4. NSLog(NSStringFromCGSize(sz));
  5.        
  6. str = @"Test text to hilight without new line for testing";
  7.  
  8.  
  9.     self.words = [NSMutableArray arrayWithArray:[str componentsSeparatedByString:@" "]];
  10.  
  11.     CGFloat pos = 0;
  12.     [self.rects addObject:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
  13.     for(int i = 0; i < [self.words count]; i++)
  14.     {
  15.         NSString *w = [self.words objectAtIndex:i];
  16.         NSLog(w);
  17.         CGSize sz = [w sizeWithFont:CGTextLabel.font];
  18.         pos += sz.width;
  19.         pos += [@" " sizeWithFont:CGTextLabel.font].width;
  20.  
  21.         if(i != [self.words count]-1)
  22.         {
  23.  
  24.             [self.rects addObject:[NSValue valueWithCGPoint:CGPointMake(pos, 0)]];
  25.         }
  26.         else {
  27.             NSLog(@"%f",pos); //here actual calculated width is printed.
  28.         }
  29.  
  30.     }
  31.        
  32. CGSize size1 = [@"Hello There" sizeWithFont:[UIFont systemFontOfSize:12]];
  33. CGSize size2 = [@" " sizeWithFont:[UIFont systemFontOfSize:12]];
  34. CGSize size3 = [@"Hello" sizeWithFont:[UIFont systemFontOfSize:12]];
  35. CGSize size4 = [@"There" sizeWithFont:[UIFont systemFontOfSize:12]];
  36.  
  37. CGSize size5 = [@"Hello There Hello There" sizeWithFont:[UIFont systemFontOfSize:12]];
  38.  
  39. NSLog(NSStringFromCGSize(size1));
  40. NSLog(NSStringFromCGSize(size2));
  41. NSLog(NSStringFromCGSize(size3));
  42. NSLog(NSStringFromCGSize(size4));
  43. NSLog(NSStringFromCGSize(size5));
  44.        
  45. 2012-07-17 10:57:40.513 TesterProject[62378:f803] {63, 15}
  46. 2012-07-17 10:57:40.514 TesterProject[62378:f803] {4, 15}
  47. 2012-07-17 10:57:40.514 TesterProject[62378:f803] {28, 15}
  48. 2012-07-17 10:57:40.514 TesterProject[62378:f803] {32, 15}
  49. 2012-07-17 10:57:40.514 TesterProject[62378:f803] {128, 15}
  50.        
  51. CGSize size = [@"HelloThere" sizeWithFont:[UIFont systemFontOfSize:12]];