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

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!

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

Red Asterisk directly beside placeholder in input box in 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

What is UTF-8 character for "previous arrow", same as system back in UINavigationItem?

I just couldn't find utf-8 encoding for character like following:
I think that it is quite possible to create this in code, as a composed character from two dashes: / and \, but I do not know what characters to compose.
Maybe U+003C
print("\u{003C}") // <
Possibly U+2039.
print("\u{2039}") // ‹
Or U+276E.
print("\u{276E}") // ❮
I'm not certain if this will work for a navigation item, but you could make use of NSAttributedString to have string with different sizes of the substrings within it. This is described in detail in this great answer:
How do I make an attributed string using Swift?
As an example for your case:
let myAttributeBack = [ NSFontAttributeName: UIFont(name: "HelveticaNeue-UltraLight", size: 30.0)!]
let attrString = NSMutableAttributedString(string: "\u{276E} ", attributes: myAttributeBack)
let myAttributeText = [ NSFontAttributeName: UIFont(name: "HelveticaNeue-UltraLight", size: 15.0)!]
let backString = NSAttributedString(string: "back", attributes: myAttributeText)
attrString.appendAttributedString(backString)
myLabel.attributedText = attrString
However, as you write below, perhaps it's not possible to use attributed strings for title of a navigation bar item. Then I'd assume that the navigation bar example you showed above simply contains an image with the back bracket and a string "back" for the title text.
Is it this:
〈
The code is U+276C.
see https://en.wiktionary.org/wiki/%E3%80%88

Weird swift behaviour on UITextField

Scenario:
I'm setting the text font and size for my UITextField.
They also have placeholders.
The code bellow is in my UIViewController:
let font = UIFont(name: "FlamaSemicondensed-Book", size: 18)
let attributesDictionary = [NSForegroundColorAttributeName: DefaultSystemColor.DarkRed]
// Textfileds without borders (and placeholders)
UsernameTextField.borderStyle = UITextBorderStyle.None
UsernameTextField.font = font
UsernameTextField.textColor = DefaultSystemColor.DarkRed
UsernameTextField.attributedPlaceholder = NSAttributedString(string: "Email", attributes: attributesDictionary)
I'm configuring (in AppDelegate) a global UI setting, that formats all of my UILabels for a certain font size.
The code bellow is in my GlobalUISettings class:
let font = UIFont(name: "FlamaSemicondensed-Book", size: 13)!
var labelAppearace = UILabel.appearance()
labelAppearace.font = font
What's weird in here:
When this UITextField is selected and I'm typing the format is the one I set for the text field (the placeholder are OK too).
But when I leave the field it assumes the behaviour of the UILabel.
I know that because if I comment the UILabel format the textField works properly.
Does anybody have any idea why does it happens?
Try changing the let font to let labelFont because 'font' is global. It might be affecting.
This is correct behavior.
To change the font of the placeholder you have to add the NSFontAttributeName attribute with the desired font as value to the NSAttributedString you assign to attributedPlaceholder.
Example:
let attributesDictionary = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: DefaultSystemColor.DarkRed
]

Resources