Having textfield background transparent, but placeholder text opaque - ios

Is it possible to have a textfield's placeholder text opaque while its background is set to be transparent, eg. if I choose some background color, and set alpha to some value, like this:
the placeholder text becomes transparent as well. But I want it opaque. So, is this achievable, preferably using storyboard ? Or at least through the code.
If it is unclear what I am trying to achieve, just let me know , and I'll post an image with an example.

You can set the color's transparency instead of the UITextField's alpha. Open the color drop down and select "Other". A color picker will open up. At the bottom of the color picker you can change the opacity.

In Swift you can obtain the placeholder element with:
let placeHolder = textField.value(forKey: "placeholderLabel") as! UILabel
placeHolder.textColor = .blue
placeHolder.isOpaque = true
and make all customizations you prefeer..

You can set the UITextView.background = .clear
I'm providing a little more code so it'll be easier to setup it programmatically, this is a ready-to-use sample.
private lazy var textField: UITextView = {
let textField = UITextView()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.font = UIFont.systemFont(ofSize: 14)
textField.isEditable = false
textField.textAlignment = .center
textField.isScrollEnabled = false
textField.backgroundColor = .clear // this is the line that answer you question
textField.text = """
People with these symptoms may have COVID-19:
Fever of chills, cough, shortness of breath or
difficulty breathing, fatigue, muscle or body aches,
headache, new loss of taste and smell, sore throat,
congestion or runny nose, nausea or vomiting,
diarrhea
"""
return textField
}()
Just remember to set the constraints and add the subview into your superview .addSubview(TextField)

Related

How could I exclude the text from UITextField to be transparent in terms of alpha?

I have created a uitextfield setted the alpha to 0.2, the problem is I can't find how I could set the alpha opacity only to be applied to the background of the UITextField, not also to the text that is inside the UItextField.
Snippet of my code:
self.emailInputField.text = "Enter your Email/Username"
self.passwordEmailInputField.text = "Enter your Password"
self.passwordEmailInputField.backgroundColor = .white
self.emailInputField.backgroundColor = .white
self.passwordEmailInputField.alpha = 0.1
self.emailInputField.alpha = 0.1
self.passwordEmailInputField.textColor = .gray
self.passwordEmailInputField.textColor = .gray
self.view.addSubview(self.emailInputField)
self.view.addSubview(self.passwordEmailInputField)
To extend the comment above: you should not set the alpha of the field itself to 0.1 if your intent is to only change the background color. Instead, initialize the backgroundColor property with alpha:
self.passwordEmailInputField.backgroundColor = .white.withAlphaComponent(0.1)
self.emailInputField.backgroundColor = .white.withAlphaComponent(0.1)

How to add long text shadow in swift?

I have tried but it was not a long style shadow.
let myString = "Show Your Creativity"
myLabel.layer.masksToBounds = false
myLabel.layer.shouldRasterize = true
myLabel.layer.rasterizationScale = 10
// Create a shadow
let myShadow = NSShadow()
myShadow.shadowBlurRadius = 1
myShadow.shadowOffset = CGSize(width: 40, height: 40)
myShadow.shadowColor = UIColor.gray
// Create an attribute from the shadow
let myAttribute = [ NSAttributedStringKey.shadow: myShadow ]
// Add the attribute to the string
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
// set the attributed text on a label
myLabel.attributedText = myAttrString // can also use with UITextView
Current result ...
I want an effect like given image.
The effect you want to reproduce is not made by adding a shadow to text - as a shadow will follow and extend the curves in the characters.
The image you have linked to seems to be using a linear gradient masked by the tops of the text.
Reproducing this programmatically would be easier done in Sketch/PaintCode using text outlines to form masks and the linear gradient, and then export the result to iOS-compliant code. This will give you resolution independence and a small file size.
Or ship the bitmap image from an illustration app.
Or do you need dynamic text?

Proper Way to Mimick UITextField Placeholder Color

I have another object that I want to have the same color as the UITextField's default placeholder text color.
I know I can simply make a new UIColor with the same color, but this seems hacky and problematic if Apple changes the default UITextField color. I instead am looking for the proper way to access the UITextField color so that I can set another field to the same color?
Thanks.
From UITextField API in UIKit, the place holder color of UITextField is 70% of gray color and it is defined by the iOS SDK.
open var placeholder: String? // default is nil. string is drawn 70% gray
From Apple Doc.s
The placeholder string is drawn using a system-defined color.
Probably, we may not have an access to these system defined colors with any attributes given in iOS SDK.
Try this:-
#IBOutlet weak var textField: UITextField!
Now in viewDidLoad method:-
let placeHolederText = "PlaceHolder"
let attributedPlaceHolder = NSAttributedString(string: placeHolederText, attributes: [NSAttributedString.Key.foregroundColor : UIColor.red ,NSAttributedString.Key.font : UIFont.systemFont(ofSize: 12) ])
self.textField.attributedPlaceholder = attributedPlaceHolder
self.textField.borderStyle = .bezel
self.textField.tintColor = .gray
Result:-
I hope this will help you,
Thanks.

UITextField wrong content width when isSecureTextEntry property set to true

I have two UITextFields configured like that:
let yellowPasswordTextField = UITextField()
view.addSubview(yellowPasswordTextField)
yellowPasswordTextField.placeholder = "Type"
yellowPasswordTextField.isSecureTextEntry = true
yellowPasswordTextField.backgroundColor = UIColor.yellow
yellowPasswordTextField.topToSuperview(offset: 30) // TinyConstraints api
yellowPasswordTextField.leftToSuperview()
yellowPasswordTextField.text = "mmmmmm"
let greenTextField = UITextField()
view.addSubview(greenTextField)
greenTextField.placeholder = "Type"
greenTextField.isSecureTextEntry = false
greenTextField.backgroundColor = UIColor.green
greenTextField.topToSuperview(offset: 60)
greenTextField.leftToSuperview()
greenTextField.text = "mmmmmm"
Please notice that both are configured exactly the same (including text), the only difference is that the first one has isSecureTextEntry property enabled while the second one has this property disabled.
And here is the result layout:
Why first UITextField has width that is exactly the same like for text field with isSecureTextEntry disabled (the one below)? Is there any way to force first text field to adjust its width to actual content - bullets replacing original text?
It's problematic because I would like to add rightView for showing/hiding password just after textfield content.

Multi-line editable piece of text: editable UILabel?

I am trying to create a large plot of editable text but there seems to be 1 option: using a small UITextField.
I know UILabels can be big and wide but I do not know how to make an editable UILabel.
I experimented with UILabel properties and the .layer method but nothing seems to be really working. Anybody have a recommendation as to what I should do?
To summarize, I am looking for a multi-line editable piece of text.
UITextView for the win!!
UITextViews allow for multiple line manipulation of texts and if you use the UITextViewDelegate, it can provide for methods that allow specific things when the textView is clicked on, etc...!
With a UITextView you can provide a specific amount of lines (if you only want 3, you can specify it) and also provide hyperlinks, if need be.
Here is an example I have (changed a little) to show an example for ya...
let textBox:UITextView = UITextView(frame: CGRect(x: firstBox.frame.width*0, y: firstBox.frame.height*0.375, width: firstBox.frame.width*1, height: firstBox.frame.height*0.5))
textBox.backgroundColor = UIColor.clearColor()
let websiteName = "http://stackoverflow.com/posts/38035564"
textBox.text = "SO is an awesome coding site! Please visit\n\(websiteName)"
//No need to set number of lines, it will auto set to as many as needed!
textBox.editable = false
textBox.selectable = true
//Register the hyperlink
textBox.dataDetectorTypes = UIDataDetectorTypes.All
textBox.textColor = UIColor.grayColor()
//Change only the hyperlink part
let textRange = NSMakeRange(textBox.text.characters.count-websiteName.characters.count, websiteName.characters.count)
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.Center
let attributedText = NSMutableAttributedString(string: textBox.text, attributes: [NSFontAttributeName:UIFont(
name: (textBox.font?.fontName)!,
size:13/15*fontSize)!,
NSParagraphStyleAttributeName: style])
attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
textBox.attributedText = attributedText
firstBox.addSubview(textBox)

Resources