Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to easly store colors, like this:
extension UIColor {
class func color(hexString: UIColor) -> UIColor {
switch color {
case .light :
return UIColor.white
case .dark :
return UIColor.black
}
}
}
So I can do something like cell.myView.backgroundColor = UIColor.dark in a if statement. But the .light and .dark gives me this error:
Pattern cannot match values of type '(UIColor) -> UIColor'
Ant tips?
You could add extensions like this...
extension UIColor {
static let light = UIColor.white
static let dark = UIColor.black
}
And then access them like...
UIColor.light
And
UIColor.dark
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Swift.
I'm trying to make template files for all elements that I often use such as buttons and labels.
Can you tell me the best way manage the templates like that has the appearance like including the colors, corner radius?
I cannot figure out except using Class files.
Thanks!
You have 2 ways to do it.
1 - create your own class
final class PrimaryButton: UIButton {
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = UIColor.red
setTitleColor(UIColor.black, for: .normal)
}
}
2 - create extension to configure
extension UIButton {
func style() {
backgroundColor = R.color.primary()
setTitleColor(R.color.black(), for: .normal)
}
}
and call this function in viewDidLoad
first style is preferred
Personally, I like to create a new file called "Styling" with the same-named class.
Then, inside there, I create static functions that accept parameters that you want to edit (for example, button).
class Styling {
static func styleButton(_ button: UIButton) {
button.layer.cornerRadius = 25.0
}
}
Then I just call it by typing in vc:
Styling.styleButton(myButton)
To add to the above answers what works for me is putting series of buttons in an array of UIButtons. It makes it much simples to rearrange them while developing the app or adjusting to various screens.
Here is a simplified example of 12 buttons forming a colorful flag:
var flagButton: [UIButton] = []
for i in 0...11 {
flagButton.append(UIButton())
flagButton[i].frame = CGRect(x: x, y: y, width: width, height: height)
self.view.addSubview(flagButton[i])
}
You can create a class like this:
class CustomButton: UIButton {
override func awakeFromNib() {
super.awakeFromNib()
self.layer.cornerRadius = 20
}
}
And use it:
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In Obj C I created a property
#property(nonatomic) UILabel *subscriptionText;
Then I created a setter method for that property UILabel. like below
-(UILabel *)subscriptionText{
if (!_subscriptionText) {
_subscriptionText = [UILabel new];
_subscriptionText.translatesAutoresizingMaskIntoConstraints = NO;
_subscriptionText.textAlignment = NSTextAlignmentJustified;
}
return _subscriptionText;
}
then In viewdidload I add this view by
[self.view addSubview:self.subscriptionText];
How can I do this same scenario in Swift 4.2.
A lazy initialization is what you need.
lazy var subscriptionText: UILabel = {
let label = UILabel()
label.textAlignment = .justified
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func viewDidLoad() {
super.viewDidLoad()
view.addSubview(subscriptionLabel)
// Label constraints
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an anatomic picture and on it, I want to print a image like a dot or something when the user taps on the first image (the body) to point out where does it hurts.
I've already read something on UITapGestureRecognizer, but I don't really understood how it works.
Try this:
override func viewDidLoad() {
super.viewDidLoad()
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap))
self.imageView.addGestureRecognizer(gestureRecognizer)
}
#objc func handleTap(tap: UITapGestureRecognizer) {
let circle = UIView()
circle.center = tap.locationInView(imageView)
circle.frame.size = CGSize(width: 30, height: 30)
circle.layer.backgroundColor = UIColor.redColor().CGColor
circle.layer.cornerRadius = 15
imageView.addSubview(circle)
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to change the color by pressing typing in TextView?
For example, I type in black, and then press the button of color orange and more print text in orange color, then I press on the black button and print black text?
The studied methods can have the color of all typed text or placeholders etc.
Try this way:
#IBAction func blackButtonClicked(sender: UIButton) {
let attributedText = textView.attributedText
textView.textColor = UIColor.blackColor()
textView.attributedText = attributedText
}
#IBAction func redButtonClicked(sender: UIButton) {
let attributedText = textView.attributedText
textView.textColor = UIColor.redColor()
textView.attributedText = attributedText
}
You can also achieve this by setting the typingAttributes property of the textView, which seems to be a nicer solution:
#IBAction func redButtonClicked() {
textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.redColor()
}
#IBAction func blackButtonClicked() {
textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.blackColor()
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i want to fill text View in current controller from data existing in popover controller
with out reload data in current controller
can i do it ??
You can use custom delegate method to fill the value.
class PopupviewController {
weak var delegate: filleTextViewDelegate?
func calldelegate () {
delegate?.fillTextview(filledData)
}
}
protocol filleTextViewDelegate : class {
func fillTextview(filledData: String)
}
class CurrentViewController :filleTextViewDelegate {
func fillTextview(filledData: String) {
textView.text = filledData
}
}