티스토리 뷰

1. Text

폰트로 표현되는 UIBezierPath의 모음

UIBezierPath가 그려지는 높은 수준 방법

 

View에  UILabel을 하위뷰로 얹고 싶지 않고 drawRect로 텍스트를 그리는 방법

NSAttributedString(Objective-C 클래스) 

각각의 문자를 어떻게 그릴지에 대한 속성으로 구성

모든 문자에 대한 설정값(컬러, 폰트 등)이 담긴 Dictionary를 가지고 있음

let text = NSAttributedString("hello")
text.drawAtPoint(aCGPoint)
let textSize: CGSize = text.size

스위프트에서 사용을 어렵게 만드는 두가지 차이점

 

1. 값이 변할 수 있는 특성을 var과 let으로 할 수 없음

NSAttributedString을 var로 선언해도 값은 바뀔 수 없음

값이 바뀔수 있는 String은 NSMutableAttributedString으로 생성

let mutableText = NSMutableAttributedString("some string")

NSAttributedString은 Swift의 String도 아니고 Objective-C의 NSString도 아니다

string과 mutableString이라는 프로퍼티로 String이나 NSString을 돌려준다

 

2. Attribute 설정

NSRange는 Range와 다르고 안에 index가 있고 범위를 표현하는 구조체로 구성

index는 Objective-C의 NSString에 있음

Swift의 String은 완전히 유니코드, NSString는 유니코드 전부를 반드시 다루지 않을수도 있음

Swift의 String -> Objective-C의 NSString(자동 브리징) -> 얼마나 긴지, 어디있는지 확인

let setAttributes(attributes: Dictionary, range: NSRange)
func addAttributes(attributes: Dictionary, range: NSRange)

Attributes

Dictionary에 설정하고 attributedString의 어떤 문자에든 설정할 수 있음

NSForegroundColorAttributeName : UIColor //텍스트 컬러
NSStrokeWidthAttributeName : CGFloat //얼마나 두껍게 그릴지
NSFontAttributeName : UIFont //폰트

Fonts

폰트를 선택하는 방법은 총 3가지

1. preferredFont 

사용자 컨텐츠 보여줄 때 사용

static func preferredFontForTextStyle(UIFontTextStyle) -> UIFont

UI가 그 시점에 뭐를 하는지에 따라 적정 스타일 선택 필요

UIFontTextStyle.HeadLine
UIFontTextStyle.Body
UIFontTextStyle.Footnote

2. sysmtemFont

static func systemFontOfSize(pointSize: CGFloat) -> UIFont
static func boldSystemFontOfSize(pointSize: CGFloat) -> UIFont

3. custermizingFont

2. Image

이미지를 그리기 위해선 UIImageView가 필요

let image: UIImage? = UIImage(named: "foo") //Images.xcassets 에 파일 추가
let image: UIImage? = UIImage(contentsOfFile: aString) //파일에서 가져온 이미지
let image: UIImage? = UIImage(data: anNSData) //인터넷에서 가져온 이미지 raw jpg, png, tiff

이미지 그리기

let image: UIImage = ...
image.drawAtPoint(aCGPoint) //왼쪽 상단 기준
image.drawInRect(aCGRect) //이미지를 Rect에 맞게 비율조정
image.drawAsPatternInRect(aCGRect) //타일 까는 효과

bounds가 바꼈을 때 다시 그려내는 상황 가정

세로에서 가로로 바뀌면 얼굴이 늘어남

bounds를 바꿨을 때 기본 설정이 다시 그리지 않기 위해 모든 비트 크기 조정

UIView의 contentMode라는 프로퍼티로 수정이 가능

var contentMode: UIViewContentMode
.Left/.Right/.Top/.Bottom //해당 위치로 이동 가능
.ScaleToFill/.ScaleAspectFill/.ScaleAspectFit //ScaleToFill이 기본
.Redraw //다시 그리기. drawRect 다시 호출

 

최근에 올라온 글
Total
Today
Yesterday