Philographer

@IBInspectable, @IBDesignable, Auto Font Size

늦은 밤이지만 너무나 감격스런 마음에 포스팅을 올리게 된다.

지난 몇일(1주일)정도 왜 아이폰은 안드로이드처럼 SP를 지원을 안 하는가에 대해서 생각하면서

하나의 폰트 사이즈로 3.5, 4, 4.7, 5.5를 어떻게 보여주어야 하나.

이걸 코드로 쓴다면 얼마나 하드코딩이 될 것이며 얼마나 힘들까 생각했다.

그렇게 구글링으로 헤메기를 반복하다가 드디어 해답을 깨달았다.

정말 왠만하게 해결되기 어려운 질문이 아니면 스택오버플로에 질문을 안 쓰지만, 이건 영영 해결 못 하겠다 해서 질문을 올렸다.

스택오버플로 질문글

스택오버플로에 적힌 답변글에서 힌트를 얻게 되어서 결국 답을 찾게 되었다.

결론은 클래스를 기기별로 만들기로 하였다.

//
//  LabelDeviceClass.swift
//  wazap
//
//  Created by 유호균 on 2016. 7. 1..
//  Copyright © 2016년 nexters. All rights reserved.
//

import UIKit

class UILabelDeviceClass : UILabel {

    @IBInspectable var iPhoneFontSize:CGFloat = 0 {
        didSet {
            overrideFontSize(iPhoneFontSize)
        }
    }
    func overrideFontSize(fontSize:CGFloat){
        let currentFontName = self.font.fontName
        var calculatedFont: UIFont?
        let bounds = UIScreen.mainScreen().bounds
        let height = bounds.size.height
        switch height {
        case 480.0: //Iphone 3,4,SE => 3.5 inch
            calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.7)
            self.font = calculatedFont
            break
        case 568.0: //iphone 5, 5s => 4 inch
            calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.8)
            self.font = calculatedFont
            break
        case 667.0: //iphone 6, 6s => 4.7 inch
            calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.9)
            self.font = calculatedFont
            break
        case 736.0: //iphone 6s+ 6+ => 5.5 inch
            calculatedFont = UIFont(name: currentFontName, size: fontSize)
            self.font = calculatedFont
            break
        default:
            print("not an iPhone")
            break
        }
    }
}

'IOS > Layout' 카테고리의 다른 글

IOS, iPhone 사이즈  (0) 2016.06.30
Scrollview Autolayout Bug  (0) 2016.06.28
no index path for table cell being reused  (2) 2016.06.27
Custom Modal 만들기 (alpha tip)  (0) 2016.06.26
ScrollView Auto Layout  (0) 2016.06.26
댓글 로드 중…

트랙백을 확인할 수 있습니다

URL을 배껴둬서 트랙백을 보낼 수 있습니다