Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
SSDynamicText
=============

# iOS 11 fully covers `SSDynamicText` functionality. Library won't be supported anymore.

---

[![Circle CI](https://circleci.com/gh/splinesoft/SSDynamicText.svg?style=svg)](https://circleci.com/gh/splinesoft/SSDynamicText) [![codecov.io](http://codecov.io/github/splinesoft/SSDynamicText/coverage.svg?branch=master)](http://codecov.io/github/splinesoft/SSDynamicText?branch=master)
[![Version](https://img.shields.io/cocoapods/v/SSDynamicText.svg)](http://cocoapods.org/pods/SSDynamicText)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
Expand All @@ -11,6 +15,63 @@ What's not so neat, though, is that `+[UIFont preferredFontForTextStyle:]` only

SSDynamicText is a collection of simple `UILabel`, `UIButton`, `UITextField`, and `UITextView` subclasses inspired by [this](http://stackoverflow.com/questions/18758227/ios7-can-we-use-other-than-helvetica-neue-fonts-with-dynamic-type/19024944#19024944) SO answer.

iOS 10 added to `UILabel`, `UITextField` and `UITextView` built-in dynamic font size support via [`adjustsFontForContentSizeCategory`](https://developer.apple.com/documentation/uikit/uicontentsizecategoryadjusting), so there's no need to manually update font after content size category change. Sadly accessibility content size categories work only for [`.body`](https://developer.apple.com/documentation/uikit/uifonttextstyle/1616682-body) style.

But finally in iOS 11 Apple introduced [`UIFontMetrics`](https://developer.apple.com/documentation/uikit/uifontmetrics) which allows to use custom fonts with all content size categories support.
It's supported by `UILabel` (so `UIButton` as well), `UITextField` and `UITextView`. Works for plain text and `NSAttributedString`.

```objc
// Plain text
UIFont *font = [UIFont fontWithName:@"Courier" size:28.0f];
label.font = [UIFontMetrics.defaultMetrics scaledFontForFont:font];
label.adjustsFontForContentSizeCategory = YES;
label.text = @"Plan text";

// Attributed text
UIFont *biggerFont = [UIFont fontWithName:@"Courier" size:28.0f];
UIFont *scaledBiggerFont = [[UIFontMetrics metricsForTextStyle:UIFontTextStyleTitle1] scaledFontForFont:biggerFont];
UIFont *smallerFont = [UIFont fontWithName:@"Courier" size:13.0f];
UIFont *scaledSmallerFont = [[UIFontMetrics metricsForTextStyle: UIFontTextStyleFootnote] scaledFontForFont:smallerFont];

NSAttributedString *biggerAttributedText = [[NSAttributedString alloc] initWithString:@"Bigger text"
attributes:@{ NSAccessibilityFontTextAttribute : scaledBiggerFont }];
NSAttributedString *smallerAttributedText = [[NSAttributedString alloc initWithString:@"smaller text"
attributes:@{ NSAccessibilityFontTextAttribute : scaledSmallerFont }];

NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
[attributedText appendAttributedString:biggerAttributedText];
[attributedText appendAttributedString:smallerAttributedText];

label.adjustsFontForContentSizeCategory = YES;
label.attributedText = attributedText;
```

```swift
// Plain text
let font = UIFont(name: "Courier", size: 17.0)!
label.font = UIFontMetrics.default.scaledFont(for: font)
label.adjustsFontForContentSizeCategory = true
label.text = "Plan text"

// Attributed text
let biggerFont = UIFont(name: "Courier", size: 28.0)!
let scaledBiggerFont = UIFontMetrics(forTextStyle: .title1).scaledFont(for: biggerFont)
let smallerFont = UIFont(name: "Courier", size: 13.0)!
let scaledSmallerFont = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: smallerFont)

let biggerAttributedText = NSAttributedString(string: "Bigger text",
attributes: [.font: scaledBiggerFont])
let smallerAttributedText = NSAttributedString(string: "smaller text",
attributes: [.font: scaledSmallerFont])

let attributedText = NSMutableAttributedString()
attributedText.append(biggerAttributedText)
attributedText.append(smallerAttributedText)

label.adjustsFontForContentSizeCategory = true
label.attributedText = attributedText
```

## Requirements

Xcode 7.0+ with iOS 7.0+ SDK.
Expand Down