Swift 원형 아이콘에 텍스트 중심 그리기

4446 단어 SwiftiOS

Swift 원형 아이콘에 텍스트 중심 그리기


UIView의 사용자 정의 클래스에서 override의 drawRect
// 丸描画
let roundRect = UIBezierPath(roundedRect: CGRectMake(1, 1, 28, 28), cornerRadius: 10)
UIColor.blueColor().setStroke()
roundRect.lineWidth = 2
roundRect.stroke()

// フォント属性
let fontAttr = [NSFontAttributeName: UIFont.systemFontOfSize(10)]
// テキスト
let str = self.initial_txt! as NSString!
// サイズ取得
let size = str.sizeWithAttributes(fontAttr)

let x_pos = (roundRect.bounds.size.width - size.width) / 2
let y_pos = (roundRect.bounds.size.height - size.height) / 2

// テキスト描画
str.drawAtPoint(CGPointMake(roundRect.bounds.origin.x + x_pos, roundRect.bounds.origin.y + y_pos),
    withAttributes: fontAttr)

결실



참고 자료


How to use NSString drawInRect to center text?
http://stackoverflow.com/questions/477509/how-to-use-nsstring-drawinrect-to-center-text

좋은 웹페이지 즐겨찾기