Last active
          August 29, 2015 14:10 
        
      - 
      
- 
        Save morizotter/449c7a0656f64b05a1fc to your computer and use it in GitHub Desktop. 
    CATextLayerの上下中央揃え ref: http://qiita.com/morizotter/items/1a89bc60ba870a51d251
  
        
  
    
      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
    
  
  
    
  | // とりあえず、fontと表示したいテキストを用意 | |
| let font = UIFont.systemFontOfSize(20.0) | |
| let text = "T" | |
| // 上記で用意したfontを利用した場合のテキストのサイズを取得 | |
| var attributes = [NSObject: AnyObject]() | |
| attributes[NSFontAttributeName] = font | |
| let size = text.sizeWithAttributes(attributes) | |
| // テキストのサイズから表示するframeを作成 | |
| let x = CGRectGetMinX(bodyRect) + (CGRectGetWidth(bodyRect) - size.width) / 2 | |
| let y = CGRectGetMinY(bodyRect) + (CGRectGetHeight(bodyRect) - size.height) / 2 | |
| let height = size.height | |
| let width = size.width | |
| let frame = CGRectMake(x, y, width, height) | |
| // CATextLayerを作っていきます。 | |
| let textLayer = CATextLayer() | |
| textLayer.string = text | |
| textLayer.font = font | |
| // 解説1 | |
| textLayer.fontSize = font.pointSize | |
| textLayer.foregroundColor = UIColor.whiteColor().CGColor | |
| textLayer.frame = frame | |
| // 左右中央揃えです | |
| textLayer.alignmentMode = kCAAlignmentCenter | |
| // スケールの調整 | |
| textLayer.contentsScale = UIScreen.mainScreen().scale | |
| shapeLayer.addSublayer(textLayer) | |
| return shapeLayer | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment