title: How to calculate NSAttributedString text size
body: <p>I would like to calculate NSAttributedString text size.
  But I couldn't.</p>
  
  <p>```</p>
  
  <pre><code>func getTextSize(viewWidth:CGFloat, padding:CGFloat) -&gt; CGSize {
      var size:CGSize = CGSizeZero
      if let s:CGSize = self.makeSize(viewWidth) {
          size = CGSize(width: s.width, height: s.height + padding)
      }
      return size
  }
  // MARK: private
  func makeSize(width:CGFloat) -&gt; CGSize? {
      var size:CGSize? = nil
      if self.respondsToSelector("boundingRectWithSize:options:context:") {
          let bounds:CGSize = CGSize(width: width, height: CGFloat.max)
          let rect:CGRect = self.boundingRectWithSize(bounds, options:.UsesLineFragmentOrigin, context: nil)
          size = CGSize(width: rect.size.width, height: rect.size.height)
      }
      return size
  }
  
  
  let no_multi_catchcopy = catchcopy!.stringByReplacingOccurrencesOfString("　", withString: " ")
  
          //行間
          let attributedText = NSMutableAttributedString(string: no_multi_catchcopy)
          let paragraphStyle = NSMutableParagraphStyle()
          paragraphStyle.lineSpacing = 5
  
          paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail
          attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedText.length))
  
          let size = attributedText.getTextSize(self.view.bounds.width, padding: 10)
  </code></pre>
  
  <p>```</p>
  
  <p>Line spacing was ignored by above way.
  This returns same value.</p>
  
  <pre><code>【短期】★現金ポイントカードPRスタッフ募期】★現金ポイントカードPRスタッフ募\351\233集★（チラシ配布＆ご案内）【東急沿線期】★現金ポイントカードPRスタッフ募\351\233集★（チラシ配布＆ご案内）【東急沿線\343の駅構内】
  (314.869921875, 28.8)
  イベントスタッフ募集！一緒に盛り上げませんか？？
  (285.24, 28.8)
  イベントスタッフ募集！一緒に盛り上げませんか？？
  (285.24, 28.8)
  必見!!9/20 日給7時間で￥8,500！OMMで什器移動作業♪
  (297.41015625, 28.8)
  必見!!9/20 日給7時間で￥8,500！OMMで什器移見!!9/20 日給7時間で￥8,500！OMMで什器移\345\213動作業♪
  (297.41015625, 28.8)
  10/1～10/3（木金土）企業イベント運営
  (213.6309375, 28.8)
  10/1～10/3（木金土）企業イベント運営
  (213.6309375, 28.8)
  ♪絵画催事の受付業務♪
  (120.046875, 28.8)
  </code></pre>
  
  <p>UIFont is let font = UIFont(name: "HiraKakuProN-W6", size: 14)
  By the way,the following code is another way.</p>
  
  <pre><code>    +(CGFloat)heightForAttributedString:(NSAttributedString *)attrString forWidth:(CGFloat)inWidth
  {
      CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
      CGFloat width = inWidth;
  
      CFIndex offset = 0, length;
      CGFloat y = 0;
      do {
          length = CTTypesetterSuggestLineBreak(typesetter, offset, width);
          CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(offset, length));
  
          CGFloat ascent, descent, leading;
          CTLineGetTypographicBounds(line, &amp;ascent, &amp;descent, &amp;leading);
  
          CFRelease(line);
  
          offset += length;
          y += ascent + descent + leading;
      } while (offset &lt; [attrString length]);
  
      CFRelease(typesetter);
  
      return ceil(y);
  
  }
  </code></pre>
  
  <p>Font is japanese system font.</p>
  
  <pre><code>let font = UIFont(name: "HiraKakuProN-W6", size: 14){
  </code></pre>
  
  <p>But It can return correct height until 2 lines.
  If it has 3 lines,method return same value as 2 line.</p>
  
  <p>What is this problem?
  Hirakaku is problem?</p>