Attributed text with UITextField - ios

I am trying to use attributed text with UITextField. I have got my placeholder text customised but it's ignoring the values I set for the main text property.
nameTextField.attributedText = NSAttributedString(string: "", attributes: [NSForegroundColorAttributeName : UIColor.white, NSParagraphStyleAttributeName : paraStyle, NSFontAttributeName : UIFont.init(name: "HelveticaNeue-Bold", size: 16)]);
Strangely there is also an issue in the storyboard. If I set the values on the text field it loses them as soon as I click away.
Anyone experienced this before?

You want to set the typingAttributes, not the attributedString.
Try this:
func textFieldDidBeginEditing(_ textField: UITextField) {
let paraStyle: NSParagraphStyle = NSParagraphStyle()
textField.typingAttributes = [NSForegroundColorAttributeName : UIColor.white, NSParagraphStyleAttributeName : paraStyle, NSFontAttributeName : UIFont.init(name: "HelveticaNeue-Bold", size: 16)]
}

Solution:
textField.defaultTextAttributes = yourAttributes // call in viewDidLoad
#DonMag's solution works but causes memory usage issues and app starts to lag (a lot). So using typingAttributes, at some point in time, the memory usage went up and never stopped and caused the app to freeze.

Related

UITextField stroke attribute opaque in Swift

I am running into a strange issue while working on developing a Meme app. I am trying to create a UITextField that has a black outline with filled white text. I am able to create just a white text field and another that is outlined in black, however I cannot get both simultaneously. Any help would be much appreciated.
Here is the attributes I am assigning to the text field:
let attribs = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeColorAttributeName: UIColor.blackColor(),
NSStrokeWidthAttributeName: 1.0
]
Here is what the app looks like:
Apple has a page describing exactly the problem you were facing: Drawing attributed strings that are both filled and stroked. The trick is to change the stroke width to negative:
Swift 4:
let attributes: [NSAttributedStringKey: Any] = [
.foregroundColor: UIColor.white,
.font: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
.strokeColor: UIColor.black,
.strokeWidth: -1 // Change here
]
Swift 2:
let attributes = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
NSStrokeColorAttributeName: UIColor.blackColor(),
NSStrokeWidthAttributeName: -1 // Change here
]
.strokeWidth = 0 yields fill without stroke.
.strokeWidth = positiveNumber yields stroke without fill.
.strokeWidth = negativeNumber yields both stroke and fill.

Way stroke outline number using swift

is there anyway to draw or stroke a number using swift for iOS? The number has to be in the outline style. Thanks in advance.
Use an NSAttributedString. UITextField can display attributed strings using the attributedText property.
let number = 5 // Whatever number you are looking to output
let font = UIFont.systemFontOfSize(12.0)
let attribs = [
NSForegroundColorAttributeName: UIColor.whiteColor(),
NSStrokeColorAttributeName: UIColor.blackColor(),
NSFontAttributeName: font,
NSStrokeWidthAttributeName: 1.0
]
let outlinedNumber = NSAttributedString(string: "\(number)", attributes: attribs)
Play with the various attributes to get the effect you want.

WatchKit : Attributed string 'String ' contains NSFontAttributeName that isn't a UIFont (SanFranciscoDisplay-Light). Removed

I am getting the following error
Attributed string 'String ' contains NSFontAttributeName that isn't a
UIFont (SanFranciscoDisplay-Light). Removed
Which doesn't make any sense to me because it should be a valid font according to this. http://iosfonts.com. Here is my code.
atrString = NSAttributedString(string: atrString.string, attributes:
[NSForegroundColorAttributeName: UIColor.lightGrayColor(),
NSFontAttributeName:"SanFranciscoDisplay-Light"])
I am trying to access the San Francisco font from my watch kit code. Any tips or suggestions are appreciated.
NSFontAttributeName is a key that should contain a value of type UIFont.
Try UIFont(name: "SanFranciscoDisplay-Light", size: 25) with the size being whatever font size you want.
atrString = NSAttributedString(string: atrString.string, attributes: [NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName:UIFont(name: "SanFranciscoDisplay-Light", size: 25)])

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
]

Setting navigation bar text font in Xcode 6.1

I am using a non-default font in my apps navigation bar, and this worked absolutely fine prior to Xcode 6.1. Now I get an error on the line of code where I am defining the font type and colour.
This is my code:
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)]
self.navigationController?.navigationBar.titleTextAttributes = attributes
What do I need to change to get this to work again?
UIFont(name:size:) returns an optional, which cannot be used as the value.Change it to this:
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)!]
By doing this you unwrap the UIFont? value to NSFontAttributeName. I believe you have to make sure the font is actually there to avoid crash.

Resources