Coder Social home page Coder Social logo

blackjacx / fittablefontlabel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tbaranes/fittablefontlabel

0.0 3.0 0.0 962 KB

UILabel (extension or subclass) that adjust the font size to fit a frame

License: MIT License

Swift 91.13% Ruby 6.10% Objective-C 2.77%

fittablefontlabel's Introduction

FittableFontLabel

Travis Language Platform CocoaPods Carthage compatible

UILabel can shrink font size as follows:

label.minimumScaleFactor = 0.3
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 1

But it won't always work as expected:

  • Doesn't fit the label height
  • Big top / bottom margins when the maxFontSize is huge
  • Not really customisable
  • ...

That's why FittableFontLabel exists:

  • Make the text fit the label size: width and height if multilines, width only if single lines
  • Works with one or several lines
  • Supports attributed string (custom line spacing...)
  • Customize maxFontSize without using default label font size
  • Auto-layout compliant
  • UILabel extension if we want to use UILabel
  • Customisable from xibs / storyboards when using the UILabel's subclass FittableFontLabel
  • ...

Multilines UILabel:

Multilines UILabel with attributed string (lines spacing):

Single line UILabel:

Usage

let aFittableFontLabel = FittableFontLabel(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
aFittableFontLabel.autoFittableFont = true
aFittableFontLabel.lineBreakMode = .ByWordWrapping
aFittableFontLabel.numberOfLines = 0 // or 1...
aFittableFontLabel.text = "?"
// Change the text, it will always fit the label frame!

Check the sample project for advanced usage.

Note: The label lineBreakMode must be set to NSLineBreakByWordWrapping in order to work.

Installation

  • iOS 8.0+

CocoaPods

Add pod FittableFontLabel to your Podfile.

Carthage

Add github "tbaranes/FittableFontLabel" to your Cartfile.

Swift Package Manager

FittableFontLabel is available on SPM. Just add the following to your Package file:

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/tbaranes/FittableFontLabel.git", majorVersion: 1)
    ]
)

Manual Installation

Just drag the Source/*.swift files into your project.

UILabel extension

func fontSizeToFit(maxFontSize: CGFloat = 100, 
					  minFontScale: CGFloat = 0.1,
					  rectSize: CGSize? = nil)

Adjust the font size to make the current text fit the label frame.

  • maxFontSize: the biggest font size to use during drawing. The default value is 100
  • minFontScale: the scale factor that determines the smallest font size to use during drawing. The default value is 0.1
  • rectSize: the size where the text must fit. The default value is the label bounds
func fontSizeThatFits(
        text string: String, 
        maxFontSize: CGFloat = 100, 
        minFontScale: CGFloat = 0.1,
        rectSize: CGSize? = nil) -> CGFloat

Returns the font size that can make the text parameter fit the label frame.

  • text: the text that needs to fit in the label
  • maxFontSize: the biggest font size that can be returned. The default value is 100
  • minFontScale: the scale factor that determines the smallest font size that can be returned. The default value is 0.1
  • rectSize: the size where the text must fit. The default value is the label bounds

FittableFontLabel

An UILabel subclass allowing you to automatize the process of adjusting the font size.

@IBInspectable public var autoAdjustFontSize: Bool = true

If true, the font size will be adjusted each time that the text or the frame change.

@IBInspectable public var maxFontSize = CGFloat.NaN

The biggest font size to use during drawing. The default value is the current font size

@IBInspectable public var minFontScale = CGFloat.NaN

The scale factor that determines the smallest font size to use during drawing. The default value is 0.1

@IBInspectable public var leftInset: CGFloat = 0
@IBInspectable public var rightInset: CGFloat = 0
@IBInspectable public var topInset: CGFloat = 0
@IBInspectable public var bottomInset: CGFloat = 0

These four properties allow you to set a marge in your label. That will change the rect where the font must fit. The default value is 0.

Contribution

  • If you found a bug, open an issue
  • If you have a feature request, open an issue
  • If you want to contribute, submit a pull request

Licence

FittableFontLabel is available under the MIT license. See the LICENSE file for more info.

fittablefontlabel's People

Contributors

madhavajay avatar sweeper777 avatar tbaranes avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.