Swift

How to limit number of characters for your textfield?

Hey! Limiting number of characters is really easy if you have standard keyboard. The link below offers so many options for this prob:
http://stackoverflow.com/questions/7010547/uitextfield-text-change-event

But these options werent fit for me. In my case, I had custom keyboard…
The solution I found was pretty satisfying.
I wrote a function to check max length of a text field. Then I checked this func every time I added character to my textfield.

  @IBAction func addBtnLabel(sender: AnyObject) {
         txtFieldPlateNumber.text =  txtFieldPlateNumber.text?.stringByAppendingString(sender.titleLabel!!.text!)        checkMaxLength(txtFieldPlateNumber as! UITextField, maxLength: 14)
     }

    func checkMaxLength(textField: UITextField!, maxLength: Int) {
         if (textField.text?.utf16.count > maxLength) {
             deleteLastCharacter()
         }
     }

//To delete backwards.
 func deleteLastCharacter(){

        if (txtFieldPlateNumber == 0) {
             // textField is empty
             print("text field is empty")
         }
         else{
             var name: String = txtFieldPlateNumber.text!
             var truncated = String(name.characters.dropLast())                       txtFieldPlateNumber.text = truncated
        }
     }

   Thinking simple, creates simple solutions. 🙂

Swift

Create Custom Keyboard Just For Your App!

I’m building an iOS app using a Storyboard. And I had to add custom keyboard for my own app. For designing keyboard I had soo many difficulties(Autolayout is an art, not a science.) But finally I fixed. Then I looked everywhere to use keyboard like a subview. (I did not want to add keyboard like target but just subview.) Soo, Here is what I ended up doing to accomplish everything. The only thing you need to consider in addition to this is to think a basic subview and remove classic keyboard from UItextField.

1- So, I added -Content View- to my View Controller in StoryBoard and I added all buttons that I need for my own keyboard. Then in my ViewController class, to make it add buttons  to UITextField I wrote:

@IBAction func addBtnLabel(sender: AnyObject) {
         txtFieldNumber.text =  txtFieldNumber.text?.stringByAppendingString(sender.titleLabel!!.text!
 }

Then for my delete button I wrote:

@IBAction func deleteBtnLabel(sender: AnyObject) {

        

        if (txtFieldNumber == 0) {

            // textField is empty

            print("text field is empty")

        }

        else{

            //txtFieldNumber.text =  txtFieldNumber.text?.

            var name: String = txtFieldNumber.text!

            var truncated = String(name.characters.dropLast())

            txtFieldNumber.text = truncated

        }

    }

Also do NOT forget the removing keyboard! I wrote some sort of trick to remove standard keyboard in viewDidLoad:

  keyboardToPost.backgroundColor = UIColor(red: 98/255 , green: 102/255 , blue: 102/255 , alpha: 1.0)        

        // initialize custom keyboard

        let keyboardView = Keyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

        txtFieldNumber.inputView = keyboardView

Thats what I ended up.
All files are in my github page with full version of it: https://github.com/rozeridilar/customKeyboard
If you have any questions, don’t hesitate to contact me!

Swift

Controlling App Fonts in Swift

Hi! After I added custom fonts to my Swift – IOS project, I had some problems to reach my custom fonts.All I did was to add .otf folders to under the resources folder in my project. Then I added them via info.plist under — Fonts provided by application–. But then somehow project did not recognize some of my .otf fonts. So I wanted to reach all fonts that I can use for my project. Then I added below function to my project in somewhere just to make it run:

 //   TO GET  FONT NAMES

        for family: String in UIFont.familyNames()

        {

            print("\(family)")

            for names: String in UIFont.fontNamesForFamilyName(family)

            {

                print("NAMES == \(names)")

            }

        }