Weird swift behaviour on UITextField - ios

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
]

Related

NSAttributedString text always sticks to bottom with big lineHeight

I'm trying to implement by-design labels coming from Sketch e.g. I need text styles with font size = 19 and line height = 50. So I ended up using NSAttributedString with NSMutableParagraphStyle but was stopped by problem with text being sticked to bottom of UILabel
I've already tried to use lineHeightMultiple and lineSpacing but those didn't give me the line height I wanted so I ended up using minimumLineHeight and maximumLineHeight equal the same
Here is my approach to make NSAttributedString
private static func makeAttributedString(
with attributes: TextAttributes,
text: String? = nil,
alignment: NSTextAlignment = .center
) -> NSAttributedString {
let font = UIFont(name: attributes.font.rawValue, size: attributes.fontSize)!
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
paragraph.paragraphSpacing = attributes.paragraph
paragraph.minimumLineHeight = attributes.lineHeight // equal 50 in my case
paragraph.maximumLineHeight = attributes.lineHeight // equal 50 in my case
let attributes: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.paragraphStyle: paragraph,
NSAttributedStringKey.foregroundColor: attributes.textColor,
NSAttributedStringKey.kern: attributes.kern,
NSAttributedStringKey.font: font
]
return NSAttributedString(string: text ?? "", attributes: attributes)
}
I expect result similar to design
but actually getting
Note: setting height constraint to 50 is not applicable because I also need multiline labels but there is the same bug with them
Seems like I've found some workaround myself, maybe it will help someone.
The method is about setting baselineOffset like this:
NSAttributedStringKey.baselineOffset: (attributes.lineHeight - font.lineHeight) / 4
Works like charm:
https://i.imgur.com/a2EOf5R.png

How to autoresize text with NSAttributedString

I have a button which opens a dropdown menu.
so, inside the button i have a text and a font awesome icon (down arrow)
i used attributed string to set the text and the icon separately. (had problems with the text being truncated)
now it looks fine, but i need to provide the font size as part of the attributes.
i want the font to auto resize - if the screen is larger and the button gets bigger - so should the text.
this is the attribute string part
let iconPart = NSMutableAttributedString(string: "\u{F107}", attributes: [NSAttributedStringKey.font: UIFont(name: "fontello", size: 23)!])
let textPart = NSMutableAttributedString(string: "Category ", attributes: [NSAttributedStringKey.font: UIFont(name: "Gotham", size: 19)!])
i've seen this solution:
let systemDynamicFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .subheadline)
let size = systemDynamicFontDescriptor.pointSize
let font = UIFont(name: "Gotham", size: size)
but this seems to keep the font size fixed according to the text style (.subheadline in this example)
is there a way of doing it?

how to change the default font and font size of NSMutableAttributedString

I'm using swift. Is there any way to change default font/ font size etc of an NSMutableAttributedString? Clearly I could set those values specifically for a given range - but that will then override any specific settings in the string.
You can use like that
let yourAttributes = [NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline), NSForegroundColorAttributeName: UIColor.blackColor()]
let yourString = NSAttributedString(string: "Here my string !", attributes: yourAttributes)
Thanks

Font attribute (NSFontAttributeName) not applied to attributed string

I'm trying to apply two attributes to a string, one for kerning (NSKernAttributeName) and one for the font (NSFontAttributeName). Although I've checked and rechecked 1000 times, I can only get the kerning attribute to be applied to the string. Here's my setup:
let runAtTitle = "RUN AT"
var attRunAt = NSMutableAttributedString(string: runAtTitle)
let font = UIFont(name: "HelveticaNeue-Bold", size: 76.0)!
let attrs = [NSFontAttributeName: font, NSKernAttributeName: -3.0]
attRunAt.addAttributes(attrs, range: NSMakeRange(0, attRunAt.length))
runAtLabel.attributedText = attRunAt
When I build the app, the kerning is applied to my string, but the font is not. It uses the default 12pt Helvetica. Please tell me I'm doing something wrong.
I just tried your code in Playground and it works fine. Most likely you are setting the text attribute of the label after setting the attributedText. That will revert the string to the normal label string with its attributes, or perhaps for some strange reason keep the kern attribute.
Here is what I get with your code:
and after adding
label.text = "RUN AT"

iOS: set textview font to body programmatically

I know how to set font programmatically when it comes to things like system fonts or custom fonts. But how do I specify the body font style? or caption or headline, for that matter?
Here is one way, you can modify it for your need.
attributes:#{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}
For the others, replace body with say headline, etc.
You can go for setting attributed string using NSAttributedString.
You alloc and init the NSAttributed string object set the attributes like body fonts and or headline etc.
Moreover, you can set the HTML text in the attributed text.
I hope the following links might help you...
Example of NSAttributedString with two different font sizes?
http://sketchytech.blogspot.in/2013/11/making-most-of-uitextview-in-ios-7.html
In Swift, you have to create a font with the class function called preferredFont from UIFont:
let label1 = UILabel()
label1.font = UIFont.preferredFont(forTextStyle: .body)
let label2 = UILabel()
label2.font = UIFont.preferredFont(forTextStyle: .caption1)
let label3 = UILabel()
label3.font = UIFont.preferredFont(forTextStyle: .headline)
You can find all the text styles here:
UIFont.TextStyle documentation

Resources