swift create label programmatically
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.text = "I'm a test label"
self.view.addSubview(label)
swift create label programmatically
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.text = "I'm a test label"
self.view.addSubview(label)
label swiftui
//A label is a struct in swiftui
//Declaration:
struct Label<Title, Icon> : View where Title : View, Icon : View
//One of the most common and recognizable user interface components is the combination of an icon and a label.
//This idiom appears across many kinds of apps and shows up in collections, lists, menus of action items, and disclosable lists, just to name a few.
//You create a label, in its simplest form, by providing a title and the name of an image, such as an icon from the SF Symbols collection:
//From Label- SwiftOnTap
//Some common examples include:
Label("Lightning", systemImage: "bolt.fill")
//Makes a HStack with two elements, a text with the name "Lightning", and a sf symbol named bolt.fill
//To customise a label, you can either use the default label customisations by apple, such as:
.labelStyle(IconOnlyLabelStyle())
//Or make your own style
struct RedBorderedLabelStyle: LabelStyle {
func makeBody(configuration: Configuration) -> some View {
Label(configuration)
.padding()
.border(Color.red)
}
}
struct RedBorderedBoltView: View {
var body: some View {
Label("Lightning", systemImage: "bolt.fill")
.labelStyle(RedBorderedLabelStyle())
}
}
//In this case we make a struct that defines a new label style, which will allow us to use the .labelStyle modifier on it
//The .padding() and .border(Color.red) make it so that the label has a padding and also a border
//(I'll save most of the explaining. This is just to introduce labels in general)
//(For more info please go to the source link)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us