Red Asterisk directly beside placeholder in input box in ios? - ios

I would like to put a red asterix( * ) besides placeholder in ios in the registration form where i am using the uitextfield. I know how to change the placeholder color but in my requirement i have the placeholder color as grey with red asterix( * ) following it. How to achieve this in swift 3? Is it possible to set it in storyboard itself?

You can use the built-in, attributedPlaceholder property of UITextField.
Like this:
let passwordAttriburedString = NSMutableAttributedString(string: "Password")
let asterix = NSAttributedString(string: "*", attributes: [.foregroundColor: UIColor.red])
passwordAttriburedString.append(asterix)
self.textField.attributedPlaceholder = passwordAttriburedString

Related

How can I set the normal color for a floating placeholder in a MDCMultilineTextField?

I'm having troubles changing the color of the placeholder of a MDCMultilineTextField in the non-active state. I'm using a MDCTextInputControllerUnderline and I've managed to personalize every other part of the textfield, with the exception of the floating placeholder when not active (see pictures below, I need it to be white).
I've tried setting the tintColor of the textfield, the textColor and the tintColor of the placeholderLabel, the normalColor, inlinePlaceholderColor, floatingPlaceholderNormalColor and floatingPlaceholderActiveColor of the controller, but nothing seems to work. What am I missing? What's the attribute to change to set the color of the placeholder?
As suggested in the comment, it was a matter of setting the Attributed Placeholder instead of the regular one.
So, in the end, it was just a matter of doing this:
let stringAttr = [NSAttributedStringKey.foregroundColor: UIColor.white]
let attributedPlaceholder = NSAttributedString(string: placeholder, attributes: stringAttr)
textField.attributedPlaceholder = attributedPlaceholder

How do I add an outline to my label in Swift?

I'm new to Stackoverflow.
I am currently developing a mobile application using XCode for iOS.
However I'm trying to set add a white outline/stroke to my label but I do not know hot to. I have tried searching these forums but could not find any solution in swift.
I have tried using the shadow property, but it's not satisfactory.
How do I add an outline to my label in Swift?
Edit: The text should look like the text in this picture: http://cf.chucklesnetwork.com/items/7/5/7/4/4/original/yo-dawg-i-heard-you-like-captions-so-i-put-captions-of-captions.jpg
You need to use NSAttributedString, set strokeColor and strokeWidth to set outline and foregroundColor to set text color. Try this:
let attrString = NSAttributedString(
string: "Write Something with Outline",
attributes: [
NSAttributedStringKey.strokeColor: UIColor.black,
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.strokeWidth: -2.0,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0)
]
)
yourLabel.attributedText = attrString
This will look like below:
You can use this OutlinedLabel. Unlike most examples, it actually outlines the text.
And you can even gradient the outline.
Just set the outline color and the line width:
label.outlineColor = .white
label.outlineWidth = 7
This will look like this

Programatically change some words' color in an UITextView

So I have an UITextView with some text and I want everytime the user inputs, for example, the word "while", after it has been written it should change into purple. Also, I have another UITextView just to display content, no user interaction enabled, and I want that everytime the view appears, all the "while" words to also be purple. How do I do this? Can you help me, please? Thank you!
Here's what I've tried so far:
let initialText = textView.text!
let string_to_color = "while"
let range = (initialText as NSString).range(of: string_to_color)
let attribute = NSMutableAttributedString.init(string: initialText)
attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.purple, range: range)
textView.attributedText = attribute
But it colors only the first word. This is for the user interaction disabled text field. I haven't figured out how to do it for the text field that contains the words that the user inputs.
You are not getting the range correctly, here is an example of using attributed strings in Swift 3.0:
// get initial text as a String type (you will get this from your textview)
let initialText = "Swift Attributed String"
// create an attribute for the text color, I chose blue color
let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
// create the attributed string and add the blue color attribute
let myString = NSMutableAttributedString(string: initialText, attributes: myAttribute )
// range starting at location 6 with a lenth of 10: "Attributed"
var myRange = NSRange(location: 6, length: 10)
// OR get range of specific string in initialText
let newRange = (initialText as NSString).range(of: "Attributed")
// change the range of the word "Attributed" to have red text color
myString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: newRange)
// create another attribute for highlighting
let anotherAttribute = [ NSBackgroundColorAttributeName: UIColor.yellow ]
// set the range of the "Attributed" part of the string to a yellow highlight
myString.addAttributes(anotherAttribute, range: newRange)
You can use this strategy to do whatever formatting you need to do with your string. Just make sure that the range that you are getting is correct.

Is it possible to add two labels inside an UIButton on Swift?

I have an UIButton on my Swift project and I would like to use a font-awesome icon combined with a text inside of it.
I know that I can set a title to the button but, as far I need that the icon and the text will be in a different text-size, I cannot combine them in the same title tag. I also have thought about to use an image (to set the font-awesome icon) with an attributed title but then I cannot change the color of the font-awesome icon.
I know that I can get this behaviour adding two labels inside an UIView but I would like to know if it is possible to get this on an UIButton.
Thanks in advance!
You can set attributed title of your button with attribute string something like,
let attributedTitle = NSAttributedString(string: "Click Here",
attributes: [NSForegroundColorAttributeName : UIColor.blueColor()])
button.setAttributedTitle(attributedTitle, forState: .Normal)
You can set multiple color,font,size for particular texts or part of string.
another example,
attributedTitle = NSMutableAttributedString(string: #"Click Here", attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
attributedTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:4))
button.setAttributedTitle(attributedTitle, forState: .Normal)
So, you can specify location by using NSRange and can set different color and font attributes for specified range!

How can I color a part of a WKInterfaceLabel

I have a sentence and I want to color some parts of this sentence.
For example "Turn on the lights" will become "Turn on(in blue) the lights(in red)"
Do you know if it's possible to make it with a unique label ?
If it's not possible, do you know how can I manage some labels side by side to print them correctly ? (with return line when text is too long, without white space...)
Just use an attributed string.
let s = NSMutableAttributedString(string: "Turn on", attributes: [NSForegroundColorAttributeName: UIColor.blueColor()])
s.appendAttributedString(NSAttributedString(string: " the lights", attributes: [NSForegroundColorAttributeName : UIColor.redColor()]))
label.setAttributedText(s)

Resources