实战需求
SwiftUI 精品完成项目之宠物展示与领养App
本文价值与收获
看完本文后,您将能够作出下面的界面
home-screen.png home-screen-dark.png details-screen.png details-screen-dark.png Jietu20210303-155225.gif看完本文您将掌握的技能
- 自定义字体
import SwiftUI
enum SailecFontType: String {
case bold = "Sailec-Bold"
case medium = "Sailec-Medium"
case regular = "Sailec"
}
struct SailecFont: ViewModifier {
var type: SailecFontType
var size: CGFloat
init(_ type: SailecFontType = .regular, size: CGFloat = 16) {
self.type = type
self.size = size
}
func body(content: Content) -> some View {
content.font(Font.custom(type.rawValue, size: size))
}
}
- 自定义颜色
extension Color {
static let main_color = Color("main_color")
static let primary_color = Color("primary")
static let secondary_color = Color("secondary")
static let text_primary_color = Color("text_primary_color")
static let placeholder_color = Color(UIColor(red: 189/255, green: 189/255, blue: 189/255, alpha: 1.0))
static let red_color = Color(UIColor(red: 235/255, green: 87/255, blue: 87/255, alpha: 1.0))
static let blue_color = Color(UIColor(red: 0/255, green: 106/255, blue: 246/255, alpha: 1.0))
static let red_color_trans = Color(UIColor(red: 235/255, green: 87/255, blue: 87/255, alpha: 0.1))
static let blue_color_trans = Color(UIColor(red: 0/255, green: 106/255, blue: 246/255, alpha: 0.1))
init(hex: String, alpha: Double = 1) {
var cString: String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) { cString.remove(at: cString.startIndex) }
let scanner = Scanner(string: cString)
scanner.currentIndex = scanner.string.startIndex
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(.sRGB, red: Double(r) / 0xff, green: Double(g) / 0xff, blue: Double(b) / 0xff, opacity: alpha)
}
}
网友评论