jaywcjlove / swiftui-example Goto Github PK
View Code? Open in Web Editor NEWSwiftUI 示例,技巧和技术集合,帮助我构建应用程序,解决问题以及了解SwiftUI的实际工作方式。
Home Page: https://jaywcjlove.github.io/swiftui-example
License: MIT License
SwiftUI 示例,技巧和技术集合,帮助我构建应用程序,解决问题以及了解SwiftUI的实际工作方式。
Home Page: https://jaywcjlove.github.io/swiftui-example
License: MIT License
您可以使用属性字符串设置占位符文本。 通过属性传递所需的颜色:
SwiftUI
使用
struct ContentView: View {
@State var text = ""
var body: some View {
ZStack(alignment: .leading) {
if text.isEmpty { Text("Placeholder").foregroundColor(.red) }
TextField("", text: $text)
}
}
}
var myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
myTextField.backgroundColor = .blue
myTextField.attributedPlaceholder = NSAttributedString(
string: "placeholder text",
attributes: [NSForegroundColorAttributeName: UIColor.yellow]
)
Swift 3+
使用以下内容:
myTextField.attributedPlaceholder = NSAttributedString(
string: "placeholder text",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]
)
Swift 4.2
使用以下内容:
myTextField.attributedPlaceholder = NSAttributedString(
string: "placeholder text",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white]
)
Button(action: {
// 退出应用
NSApplication.shared.terminate(self)
}, label: {
Image(systemName: "clock")
Text("Click Me")
})
在 iOS 14 才开始能用 ,有没有类似的库么😂
https://stackoverflow.com/a/59891334/1334703
TextField("placeholder", text: .constant(""))
struct SignInButtons_Previews: PreviewProvider {
static var previews: some View {
SignInButtons(backgroundColor: Color.blue,
signInUrl: .constant("http://www.apple.com"),
showModal: .constant(false))
}
}
var myArray = ["Steve", "Bill", "Linus", "Bret"]
myArray.insert("jaywcjlove", at: 1)
print(myArray)
// ["Steve", "jaywcjlove", "Bill", "Linus", "Bret"]
var myArray = ["Steve", "Bill", "Linus", "Bret"]
myArray.append("wcj")
print(myArray)
// ["Steve", "Bill", "Linus", "Bret", "wcj"]
var array1 = [1,2,3,4,5]
let array2 = [6,7,8,9]
let array3 = array1+array2
print(array3)
array1.append(contentsOf: array2)
print(array1)
let entities = PersistenceController.shared.container.managedObjectModel.entities
for entity in entities {
if ((entity.name) != nil) {
let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: entity.name!)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
do {
try viewContext.execute(deleteRequest)
try viewContext.save()
} catch {
print ("There was an error")
}
}
}
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates are pending. To force PRs open, click the checkbox below.
.github/workflows/ci.yml
actions/checkout v3
actions/setup-node v3
peaceiris/actions-gh-pages v3
ncipollo/release-action v1
package.json
idoc ^1.29.0
struct Triangle: Shape {
func path(in rect: CGRect)-> Path {
var path = Path()
path.move(to: CGPoint(x: rect.midX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.midX, y: rect.minY))
return path
}
}
使用
Triangle()
.fill(Color.green)
.frame(width: 100, height: 100, alignment: .center)
https://stackoverflow.com/a/33706783/1334703
enum Audience : String {
case public
case friends
case private
}
let audience = Audience.public.rawValue // "public"
除了获得 Mac)是从 Mac App Store 下载 Xcode。
如果您已经使用 Xcode 一段时间了,您应该删除不再支持的旧模拟器以清理一些空间。 使用以下终端命令删除旧模拟器:
$ xcrun simctl delete unavailable
https://stackoverflow.com/a/63937440/1334703
struct ContainerView<Content: View>: View {
let content: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
content
}
}
这不仅允许您将简单的Views放入其中,而且由于使用@ViewBuilder,还可以使用if-else和switch-case块:
使用示例 1:
struct SimpleView: View {
var body: some View {
ContainerView {
Text("SimpleView Text")
}
}
}
使用示例 2:
struct IfElseView: View {
var flag = true
var body: some View {
ContainerView {
if flag {
Text("True text")
} else {
Text("False text")
}
}
}
}
使用示例 3:
struct SwitchCaseView: View {
var condition = 1
var body: some View {
ContainerView {
switch condition {
case 1:
Text("One")
case 2:
Text("Two")
default:
Text("Default")
}
}
}
}
如果您想要一个贪婪的容器,它将占用所有可能的空间(与上面的容器只声明其子视图所需的空间相反),这里是:
struct GreedyContainerView<Content: View>: View {
let content: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
Color.clear
.overlay(content)
}
}
结构只能从协议继承(如果正确的话)。 不能从基本结构继承,所以您不能做
struct Resolution {
var width = 0
var height = 0
}
struct MyStruct: Resolution { ... } // ERROR!
因此,有两个选择。 第一种是改用类。 第二个是重构代码以使用协议。
因此,有一些常用方法,则可以执行以下操作:
protocol PixelContainer {
var width: Int { get }
var height: Int { get }
}
extension PixelContainer {
var count: Int { return width * height }
}
struct Resolution: PixelContainer {
var width = 10
var height = 20
}
let numPixels = Resolution().count // Legal
let values = [2.0, 4.0, 5.0, 7.0]
var squares: [Double] = []
for value in values {
squares.append(value * value)
}
print(squares) // [4.0, 16.0, 25.0, 49.0]
let values = [2.0,4.0,5.0,7.0]
let squares2 = values.map({ (value: Double) -> Double in
return value * value
})
print(squares2) // [4.0, 16.0, 25.0, 49.0]
let values = [2.0,4.0,5.0,7.0]
let squares2 = values.map { value in
value * value
}
print(squares2) // [4.0, 16.0, 25.0, 49.0]
let values = [2.0,4.0,5.0,7.0]
let squares = values.map { $0 * $0 }
print(squares) // [4.0, 16.0, 25.0, 49.0]
let scores = [0,28,124]
let words = scores.map {
NumberFormatter.localizedString(from: NSNumber(value: $0), number: .spellOut)
}
print(words) // ["zero", "twenty-eight", "one hundred twenty-four"]
let milesToPoint = ["point1":120.0, "point2": 50.0,"point3": 70.0]
let kmToPoint = milesToPoint.map { name,miles in miles * 1.6093 }
print(kmToPoint) // [112.651, 193.11599999999999, 80.465]
let lengthInMeters: Set = [4.0, 6.2, 8.9]
let lengthInFeet = lengthInMeters.map { meters in meters * 3.2808 }
print(lengthInFeet) // [29.199120000000004, 20.340960000000003, 13.1232]
let digits = [1, 4, 5, 10, 15]
let even = digits.filter { (number) -> Bool in return
number % 2 == 0
}
print(even) // [4, 10]
// -----> 简写 <------
let digits = [1, 4, 10, 15]
let even = digits.filter { $0 % 2 == 0 }
print(even) // [4, 10]
func hashtags(in string: String) -> [String] {
let words = string.components(
separatedBy: .whitespacesAndNewlines
)
// 通过过滤器,我们可以删除所有不符合条件的元素
// 给定的要求,在这种情况下,那些没有开始的要求
// 以前置的"#"字字符:
return words.filter { $0.starts(with: "#") }
}
let tags = hashtags(in: "#Swift by Hello #Basics")
print(tags) // ["#Swift", "#Basics"]
func hashtags(in string: String) -> [String] {
let words = string.components(
separatedBy: .whitespacesAndNewlines
)
let tags = words.filter { $0.starts(with: "#") }
// 使用'map',我们可以将闭包作为转换将值序列转换为新的值数组:
return tags.map { $0.lowercased() }
}
let strings = [
"I'm excited about #SwiftUI",
"#Combine looks cool too",
"This year's #WWDC was amazing"
]
let tags = strings.map { hashtags(in: $0) }
print(tags) // [["#swiftui"], ["#combine"], ["#wwdc"]]
let names = ["Hello", "World", "SwiftUI"]
let position = names.firstIndex(of: "SwiftUI")
print(position ?? -1) // 1
func convertToInt(_ string: String?) -> Int? {
return string.flatMap(Int.init)
}
let str = convertToInt("23")
print(str) // Optional(23)
print(str ?? "") // 23
guard let value = str else {return}
print(value) // 23
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.