티스토리 뷰
728x90
반응형
convert UIColor to String
- DB 에 색을 저장하려고 하니까 String 으로 변환해줄 필요가 생겼다.
import Foundation
import UIKit
extension UIColor {
convenience init(hex: String, alpha: CGFloat = 1.0) {
var hexFormatted: String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
if hexFormatted.hasPrefix("#") {
hexFormatted = String(hexFormatted.dropFirst())
}
assert(hexFormatted.count == 6, "Invalid hex code used.")
var rgbValue: UInt64 = 0
Scanner(string: hexFormatted).scanHexInt64(&rgbValue)
self.init(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: alpha)
}
func toHexString() -> String {
var r:CGFloat = 0
var g:CGFloat = 0
var b:CGFloat = 0
var a:CGFloat = 0
getRed(&r, green: &g, blue: &b, alpha: &a)
let rgb:Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0
return String(format:"#%06x", rgb)
}
}
사용방법
//사용방법
var listBulletBtnColor = "#007aff"
listBulletBtn.backgroundColor = UIColor(hex: listBulletBtnColor)
if let color = listBulletBtn.backgroundColor {
database.reminderColor = color.toHexString()
728x90
반응형
'iOS' 카테고리의 다른 글
iOS) delegate 에 여러개의 컬렉션뷰를 연결하기 (0) | 2021.07.21 |
---|---|
iOS) UIButton programmatically (0) | 2021.07.21 |
iOS) Cell 꾹 눌러서 미리보기 만들기 (0) | 2021.07.21 |
iOS) UINavigationBar 의 large title 색 설정 (0) | 2021.07.21 |
iOS) push 와 pop 시 ViewLifeCycle (0) | 2021.07.21 |
TAG
- MVVM
- APNS
- projectsetting
- containerBackground
- MOYA
- Algorithm
- Protocol
- github
- WidgetKit
- WWDC22
- 서버통신
- Swift
- configurable widget
- CloneCoding
- rxswift
- Widget
- RxCocoa
- YPImagePicker
- async/await
- SwiftUI
- Objective-C
- urlsession
- Firebase
- WWDC
- OpenSourceLibrary
- IOS
- 2022 KAKAO TECH INTERNSHIP
- UserDefaults
- watchOS
- Notification
최근에 올라온 글
최근에 달린 댓글
글 보관함
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
링크
- Total
- Today
- Yesterday