Keyboard Adjustments and Text Attributes L3.4.2

Setting font style and color

The UITextField class comes with a defaultTextAttributes property. This property takes a dictionary of attributes chosen from those specified in the NSAttributedString class. We set this property once using a dictionary. From then on text will be displayed with the defined characteristics.

documentation

Some useful attributes

  • NSStrokeColorAttributeName - This will give us our black outline.
  • NSForegroundColorAttributeName - This will provide the white fill.
  • NSFontAttributeName - Here we select the font name.
  • NSStrokeWidthAttributeName - Here we can control the outline width.

In practice

let myTextAttributes:[String:Any] = [  
  NSStrokeColorAttributeName: /* TODO: fill in appropriate UIColor */,
  NSForegroundColorAttributeName: /* TODO: fill in appropriate UIColor */,
  NSFontAttributeName: UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
  NSStrokeWidthAttributeName: /* TODO: fill in appropriate Float */]

Setting the defaultTextAttributes property

Once myTextAttributes are defined we can use that dictionary to set each textField’s defaultTextAttributes.

someTextField.defaultTextAttributes = myTextAttributes  
someOtherTextField.defaultTextAttributes = myTextAttributes  

Adjusting the Keyboard and NSNotification

Instances when you need to type on the lower part of the screen and cannot scroll down to see what you are typing you may need to move the view up.

This can be done with the keyboardWillShow() function.

func keyboardWillShow(_ notification:Notification) {  
  view.frame.origin.y = 0 -
 getKeyboardHeight(notification)
}

The point at which the viewframe.origin.y value is 0 is at the very top of the screen.

The above code can be coupled with a call to the NSNotificationCenter which provide a way to announce notifications throughout a program across classes. i.e. the keyboard appearing or disappearing.

In the same way we subscribe to mailing lists, Objects need to subscribe to or "observe" notifications.

Every iOS program has one default NotificationCenter, NotificationCenter.default which comes with a number of useful stock notifications, like .UIKeyboardWillShow. We can also create custom notifications using post(name:object:).

func subscribeToKeyboardNotifications() {  
  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
}

The view controller is signing up to be notified when the key board event UIKeyboardWillShow is coming up.

Each notification has three properties: a name, an optional userInfo dictionary, and an optional object.

Notifications carry messages in a userInfo dictionary. The UIKeyboardWillShowNotification carries the UIKeyboardFrameEndUserInfoKey in it's dictionary.

func getKeyboardHeight(_ notification:Notification) -> CGFloat {  
  let userInfo = notification.userInfo
  let keyboardSize = userInfo!
[UIKeyboardFrameEndUserInfoKey] as! NSValue // of CGRect
  return keyboardSize.cgRectValue.height
    }

Notifications need to be unsubscribed from as well. To do that we use the removeObserver method.

func unsubscribeFromKeyboardNotifications() {  
  NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
}