Skip to content

Commit e2207d3

Browse files
author
wang_liang
committed
add: support responsive design for iPhone specially
1 parent d74c1d8 commit e2207d3

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# The missing CSS-like module for SwiftUI
22
> Check out the [example project using SwiftUI-CSS](https://github.com/hite/SwiftUI-CSS_example);
33
> Also, Swift Package availble which url is `https://github.com/hite/SwiftUI-CSS`
4-
4+
> Supported macOS(.v10_14), .iOS(.v13)
55
The SwiftUI is a great UI development framework for the iOS app. After I wrote some to-be-released app with SwiftUI framework, I realized that I need a solution to write more clear, simple, view-style-decoupled code with lots of custom style design.
66

77
So here is **SwiftUI-CSS**. With **SwiftUI-CSS**, you can:
@@ -162,7 +162,15 @@ let fontStyle = CSSStyle([.font(.caption)])
162162
let finalStyle = fontStyle + colorStyle
163163
print("finalStyle = \(finalStyle)")
164164
```
165-
5. (to be continued)
166-
165+
5. use responsive class to make view larger on larger screen
166+
```swift
167+
// In iOS, if the sketch file designed for screen 375x667, the responsive fator should be compared to UIScreen.main.bounds.size.width.
168+
let responsive = Responsive(UIScreen.main.bounds.size.width / 375)
169+
let wikiDesc_clsName = CSSStyle([
170+
.font(Font.system(size: responsive.r(12))),
171+
.foregroundColor(NormalDescColor)
172+
.paddingEdges([.bottom], responsive.r(10))
173+
])
174+
```
167175

168176

Sources/SwiftUI_CSS/Responsive.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by hite on 2020/11/3.
6+
//
7+
8+
import Foundation
9+
import Combine
10+
import SwiftUI
11+
12+
class Responsive: ObservableObject {
13+
/// For example. the size of view in sketch file designed for iPhone 6 should larger on iPhone by 1.104 ( 414 / 375) times.
14+
/// so the factor = 1.104
15+
@Published var factor: CGFloat
16+
17+
init(_ factor: CGFloat) {
18+
self.factor = factor
19+
}
20+
convenience init(designSize: CGRect) {
21+
var factor: CGFloat = 1
22+
if designSize.equalTo(.zero) {
23+
factor = 1
24+
} else {
25+
#if os(iOS)
26+
// Code specific to iOS
27+
let size = UIScreen.main.bounds.size
28+
factor = size.width / designSize.width
29+
#elseif os(macOS)
30+
// Code specific to macOS
31+
factor = 1// maybe 1 is not right number.
32+
#else
33+
factor = 1
34+
#endif
35+
}
36+
37+
self.init(factor)
38+
}
39+
40+
public func r(_ standardSize: CGFloat) -> CGFloat {
41+
return self.factor * standardSize
42+
}
43+
}

Sources/SwiftUI_CSS/SwiftUI_CSS.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ extension View {
205205
}
206206

207207
func changeClassName(_ clsName: CSSStyle) -> some View {
208-
// 省略
208+
//
209209
return self
210210
}
211211

0 commit comments

Comments
 (0)