30

I am trying to add two buttons to my app to set the font size of a UITextview,and i found this function

textview.font.increaseSize(...) //and decreaseSize(...)

But i don't understand what I have to put inside the parentheses,i want to increase and decrease the font size by one point

Thanks for the answers

GioB
  • 973
  • 3
  • 11
  • 17
  • that looks an undocumented private method, which accidentally remained visible; you don't need to consider that you would be able to use it. – holex Feb 26 '15 at 12:13

5 Answers5

59

I don't think there's a method named increaseSize(). May be you've find some UIFont or UITextView category.

The official UIFont class document doesn't reveal any such method.

Additionally you can increase the font like this:

textview.font = UIFont(name: textview.font.fontName, size: 18)

The above statement will simply set the existing font size to 18, change it to whatever you want.

However if you want some method like you've posted, you can introduce your own category like this:

extension UITextView {
    func increaseFontSize () {
        self.font =  UIFont(name: self.font.fontName, size: self.font. pointSize+1)!
    }
}

Swift 2 & 3:

import UIKit
extension UITextView {
    func increaseFontSize () {
        self.font =  UIFont(name: (self.font?.fontName)!, size: (self.font?.pointSize)!+1)!
    }
}

and simply import this to wherever you want to use like this:

textview.increaseFontSize()

it'll increase the font by 1 every time you call it..

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
32

For the system font, this also works:

yourTextView.font = .systemFontOfSize(16)
AndyOS
  • 3,607
  • 3
  • 23
  • 31
22

Swift 4,5

yourTextView.font = .systemFont(ofSize: 16)
kuzdu
  • 7,124
  • 1
  • 51
  • 69
3

Swift 5

textView.font = UIFont.preferredFont(forTextStyle: .body) //.title, .headline, etc..
0

SWIFT 5

You can also do

yourTextView.font = UIFont.systemFont(ofSize: CGFloat)
h3t1
  • 1,126
  • 2
  • 18
  • 29
PandaDev
  • 121
  • 11