2018년 8월 29일 수요일

Using NSAttributedString in Swift

1. NSAttributedString 설정

guard let font = UIFont(name: "Helvetica-Bold", size: 14else { return }

let foregroundColor = UIColor(red: 51 / 255.0, green: 51 / 255.0, blue: 51 / 255.0, alpha: 1.0)

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 0.2 * font.lineHeight
paragraphStyle.alignment = NSTextAlignment.center


titleLabel.attributedText = NSAttributedString(string: "Hello World", attributes: [ .font: font, .foregroundColor: foregroundColor, .paragraphStyle: paragraphStyle])




2. 두개의 폰트 붙여쓰기(Cat)

let helveticaFont = UIFont(name: "Helvetica-Bold", size: 15) ?? UIFont.systemFont(ofSize: 15)
        let zapfinoFont = UIFont(name: "Zapfino", size: 20) ?? UIFont.systemFont(ofSize: 20)
        
        let attString1 = NSMutableAttributedString(string: "Destination", attributes: [NSAttributedStringKey.font: helveticaFont, NSAttributedStringKey.foregroundColor: UIColor.red])
        let attString2 = NSMutableAttributedString(string: "Source", attributes: [NSAttributedStringKey.font: zapfinoFont, NSAttributedStringKey.foregroundColor: UIColor.blue])
        attString1.append(attString2)
        

        label.attributedText = attString1



캡션 추가

Date 에서 년,월,일,요일 정보 가져오기

1. Date 에서 년,월,일,요일 정보 가져오기 예제

let today = Date()
let year = Calendar.current.component(.year, from: today)
let month = Calendar.current.component(.month, from: today)
let day = Calendar.current.component(.day, from: today)
let weekday = Calendar.current.component(.weekday, from: today)
let weekOfMonth = Calendar.current.component(.weekOfMonth, from: today)
let weekOfYear = Calendar.current.component(.weekOfYear, from: today)


print(year, month, day, weekday, weekOfMonth, weekOfYear)


출력 : 
2018 8 30 5 5 35

String , Hexa Decimal ===> Color 로 변환


static func rgb(_ red: CGFloat, _ green: CGFloat, _ blue: CGFloat, _ alpha: CGFloat = 1.0) -> UIColor {
        return UIColor(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: alpha)

    }

static func hexadecimal(_ value: UInt32, _ alpha: CGFloat = 1.0) -> UIColor {
        return UIColor.rgb(CGFloat((value & 0xFF0000) >> 16),
                           CGFloat((value & 0xFF00) >> 8),
                           CGFloat(value & 0xFF),
                           alpha)
    }

static func hexadecimal(_ value: String, _ alpha: CGFloat = 1.0) -> UIColor {
        let alphanumerics = value.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        return UIColor.hexadecimal(UInt32(alphanumerics, radix: 16) ?? 0, alpha)
    }

String에서 numeric 또는 alphanumerics 추출

1. String에서 numeric 또는 alphanumerics  추출 예제

let value = "39_ABCD*^%()!@ \n~_384749.374"
let alphanumerics = value.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
let numeric = value.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()

출력 : 
alphanumerics : 39_ABCD*^%()!@ 
~_384749.374
numeric : 39384749374

숫자를 통화로 표기


1. 숫자를 통화로 변환 기본

let value = 39384749.374
let decimalFormatter = NumberFormatter()
decimalFormatter.numberStyle = NumberFormatter.Style.decimal
let currency = decimalFormatter.string(from: value as NSNumber) ?? String(value)

출력 => "39,384,749.374"



2. 세퍼레이터 변경

let value = 39384749.374
let decimalFormatter = NumberFormatter()
decimalFormatter.numberStyle = NumberFormatter.Style.decimal
decimalFormatter.groupingSeparator = "#"
let currency = decimalFormatter.string(from: value as NSNumber) ?? String(value)

출력 => "39#384#749.374"


2. 통화기호 표시

let value = 39384749.374
let decimalFormatter = NumberFormatter()
decimalFormatter.numberStyle = NumberFormatter.Style.currency
decimalFormatter.currencySymbol = "₩"
let currency = decimalFormatter.string(from: value as NSNumber) ?? String(value)

출력 => "₩39,384,749.37"

2018년 8월 27일 월요일

범위연산 (range operators) 활용

1. 기본




2. 배열

let numbers = [10, 20, 30, 40, 50]
let part1 = numbers[0 ..< 5]
let part2 = numbers[2...]
let part3 = numbers[..<4]

let part4 = numbers[0...3]

print("part1 = \(part1)")
print("part2 = \(part2)")
print("part3 = \(part3)")

print("part4 = \(part4)")


part1 = [10, 20, 30, 40, 50]
part2 = [30, 40, 50]
part3 = [10, 20, 30, 40]

part4 = [10, 20, 30, 40]



3. Int Array 만들기

1)
let day = [Int](1...31)
print("15 is ", day.contains(15))

print("40 is ", day.contains(40))
print(day)

15 is  true
40 is  false

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]

2)
let lessThanFive = 0.0..<5.0
print(lessThanFive.contains(3.14))  // Prints "true"
print(lessThanFive.contains(5.0))   // Prints "false"


4. 스트링

let text = "Hello World!"
if let firstSpace = text.index(of: " ") {
    print(text[text.startIndex..<text.endIndex])
    
    print("-----------")
    print(text[text.startIndex...])
    print(text[..<firstSpace])
    
    print("-----------")
    print(text[text.startIndex...firstSpace])
    
    let nextIndex = text.index(firstSpace, offsetBy: 1)
    print(text[text.startIndex...nextIndex])

}

Hello World!
-----------
Hello World!
Hello
-----------
Hello 

Hello W