iOS Programming Dynamic Type 1 

Dynamic Type is a technology introduced in iOS 7 that helps realize this goal by providing specifically designed text styles that are optimized for legibility.

Dynamic Type 是从iOS7引入的技术来帮助实现这个目标通过提供专门设计的text styles 为了优化清晰度。

The Dynamic Type system is centered around text styles.

Dynamic Type系统是以text style系统为中心的。

When a font is requested for a given text style, the system will use the user's preferred text size in association with the text style to return an appropriately configured font.

当字体请求一个给定的文本类型,系统就会使用用户喜欢的text size 与相关的text style 返回一个恰当的配置好的字体。

1 Using Preferred Fonts 使用偏好字体

Implementing Dynamic Type is straightforward. At its most basic level, you get a UIFont for a specific text style and then apply that font to something that displays text, such as a UILabel.

你得到一个指定的text style 的UIFont,并实现那个font到一些东西,比如说UILabel到一些事情上。

You are going to need to update some attributes of the labels programmatically soon, so add outlets to each of the labels to the class extension in BNRDetailViewController.m.

 

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *serialNumberLabel;

@property (weak, nonatomic) IBOutlet UILabel *valueLabel;

Now that there is an outlet to each of the labels, add a method that sets the font for each to use the preferred Body style.

现在每个label都有一个outlet,添加一个方法为每个label 设置字体来使用perfered 字体格式。

- (void)updateFonts

{
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

self.nameLabel.font = font;

self.serialNumberLabel.font = font;

self.valueLabel.font = font;

self.dateLabel.font = font;

self.nameField.font = font;

self.serialNumberField.font = font;

self.valueField.font = font;

}

 

Now call this method at the end of viewWillAppear: to update the labels before they are visible

在能看到之前调用这个方法在viewWillAppear的末尾。

[self updateFonts];

 

The preferredFontForTextStyle: method will return a preconfigured font that is customized for the user's preferences.

preferredFontForTextStyle返回一个预先定义好的字体适应用户的preferences.

 

Press the Home button (or use Home from the Hardware menu), and open Apple's Settings application. Under General, select Text Size, and then drag the slider all the way to the left to set the font size to the smallest value

Now, go back into Homepwner. If you return to the BNRDetailViewController, you will notice that the interface has not changed at all! Why is this?

Since viewWillAppear: is not called when the application returns from the background, your interface is not getting updated.

由于viewWillAppear并没有被调用当application 从后台返回,你的界面没有被更新。

2 Responding to User Changes 响应用户的改变

When the user changes the preferred text size, a notification gets posted that the application's objects can register to listen for.

当用户改变了preferred text size ,一个通知被传递,应用对象可以注册来监听这件事。

This is the UIContentSizeCategoryDidChangeNotification, and this is a great time to update the user interface.

这个通知就是UIContentSizeCategoryDidChangeNotification,这个时候是更新用户界面的好时机。

In BNRDetailViewController.m, register for this notification in initForNewItem: and remove the class as an observer in dealloc.

// Make sure this is NOT in the if (isNew ) { } block of code

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

[defaultCenter addObserver:self selector:@selector(updateFonts)

name:UIContentSizeCategoryDidChangeNotification

object:nil];

 

 

- (void)dealloc

{
NSNotificationCenter *defaultCenter = [NSNotificationCenter

defaultCenter];

[defaultCenter removeObserver:self]; }

 

3 Updating Auto Layout

What you need to do is utilize the intrinsicContentSize of the labels to allow them to resize themselves to exactly the size they need to be.

你需要做的就是利用label的intrinsicContentSize允许他们根据确切的大小来调整他们自己。

Open BNRDetailViewController.xib. In the canvas, select each of the four labels, one by one, and remove their explicit width and height constraints.

4 Content Hugging and Compression Resistance Priorities revisited

every view has a preferred size, which is its intrinsicContentSize.

每个view 都有衣蛾首选尺寸,就是它的intrisicContentSize.

This gives each view an implied width and height constraint.

这个每个view 一个隐含的宽和高限制。

For a view to grow smaller than its intrinsicContentSize in a given dimension, there has to be a constraint with a higher priority than that view's Content Compression Resistance Priority.

Let's inspect the layout of the Name label and corresponding text field in the horizontal direction.

检查Name label的layout和对应的在水平方向上的text field。

影响这些views的限制是

nameLabel.leading = superview.leading + 8 

nameField.leading = nameLabel.trailing + 8 

nameField.trailing = superview.trailing - 8

This gives us a visual format string that looks like:

H:|-8-[nameLabel]-8-[nameField]-8-|

Notice that there are no constraints directly impacting the widths of the views. Because of this, both views want to be at the width of their intrinsicContentSize. One or both of the labels will have to stretch in order to satisfy the existing constraints.

注意到对于views的宽度都没有具体的限制。就是因为这个,这两个view 都想是他们instrinsicContentSize的宽度。一个或两个labels需要延长以满足限制。

So which view will get stretched? Since both views want to be wider than their intrinsicContentSize, the view with the lower Content Hugging Priority will stretch.

所以那个拥有lower content hugging priority 的view将会延长。

If you compare the UILabel and the UITextField, you will see that the label has a Content Hugging Priority of 251 whereas the text field's is 250. Since the label wants to "hug" more, the label will be the width of its intrinsicContentSize and the text field will stretch enough to satisfy the equations.

如果你比较UILable 和UITextField,你会看到label有一个content Hugging priority of 251,而text field 是250.因为label 更想hug ,label将会是intrinsicContentSize的宽度,而text field 将会延长来满足等式关系。

Remember that the goal is to have all of the text fields aligned.

目标是让所有的text field 对其

The way that you will accomplish this is by having the three top labels be the same width.

方法是你通过让三个label有相同的宽度。

Select the Name, Serial, and Value labels together and open the Pin menu. Select Equal Widths and from the Update Frames drop-down choose All Frames in Container. Finally, click Add 2 Constraints.

 

 

iOS Programming Dynamic Type 1的更多相关文章

  1. iOS Programming Dynamic Type 2

    iOS Programming Dynamic Type  2       You will need to update two parts of this view controller for ...

  2. 理解iOS 8中的Self Sizing Cells和Dynamic Type

    http://www.cocoachina.com/ios/20140922/9717.html 在iOS 8中,苹果引入了UITableView的一项新功能--Self Sizing Cells,对 ...

  3. iOS Programming UIStoryboard 故事板

    iOS Programming UIStoryboard In this chapter, you will use a storyboard instead. Storyboards are a f ...

  4. iOS Programming Web Services and UIWebView

    iOS Programming Web Services and UIWebView The work is divided into two parts. The first is connecti ...

  5. iOS Programming UINavigationController

    iOS Programming UINavigationController the Settings application has multiple related screens of info ...

  6. ios Programming:The Big Nerd Ranch Guid(6th Edition) (Joe Conway & AARON HILLEGASS 著)

    Introduction (已看) Prerequisites What Has Changed in the Sixth Edition? Our Teaching Philosophy How t ...

  7. Working with the Dynamic Type in C#

    Working with the Dynamic Type in C# https://www.red-gate.com/simple-talk/dotnet/c-programming/workin ...

  8. iOS Programming Controlling Animations 动画

    iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...

  9. iOS Programming NSUserDefaults

    iOS Programming NSUserDefaults  When you start an app for the first time, it uses its factory settin ...

随机推荐

  1. dm385的分辨率切换

    建议用两个RSZ的输出来完成切换分辨率功能,帧率可以通过软件丢帧来实现. 两个SWMS增加了两个1080p60的读和写,对系统影响是比较大的. http://www.deyisupport.com/q ...

  2. 矩阵经典题目七:Warcraft III 守望者的烦恼(矩阵加速递推)

    https://www.vijos.org/p/1067 非常easy推出递推式f[n] = f[n-1]+f[n-2]+......+f[n-k]. 构造矩阵的方法:构造一个k*k的矩阵.当中右上角 ...

  3. 2016/04/26 流程 数据库lcdb 四个表 1,用户表users 2,流程表(设定有哪些流程)liucheng 3,流程发起者表(记录谁发起到哪里) 4,流程经过的人员表 flowpath (order排序)

    流程:      十一 个页面 1,denglu.php(登录) <!DOCTYPE html> <html lang="en"> <head> ...

  4. tomcat项目重复加载问题

    主要是通过配置<Tomcat安装目录>/conf/server.xml文件 步骤: 1.打开server.xml,在</Host>的上一行添加内容格式如下 <Contex ...

  5. rip是典型的距离矢量动态路由协议。Ospf是链路状态型的协议

    网络工程师十个常见面试问题-看准网 https://m.kanzhun.com/k-mianshiwenti/1465113.html 两者都属于IGP协议,rip是典型的距离矢量动态路由协议.Osp ...

  6. Using Python with TurboGears A complete web framework integrating several Python projects

    Using Python with TurboGears TurboGears is a Python web framework based on the ObjectDispatch paradi ...

  7. [BZOJ2144]国家集训队 跳跳棋

    题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...

  8. (续)linux SD卡初始化---mmc_sd_init_card函数

    mmc_sd_init_card剩下的关于UHS-I的分支结构. uhs-I的初始化流程图如图: 红线标出的部分是已经做了的事,与上一篇那个流程图是一致的,之后就是if分支中做的事. if分支中的函数 ...

  9. YTU 2573: 连续奇数和

    2573: 连续奇数和 时间限制: 1 Sec  内存限制: 128 MB 提交: 63  解决: 37 题目描述 小明看到一本书上写着:任何数字的立方都可以表示为连续奇数的和. 比如: 2^3 = ...

  10. 数据库sqlite3的使用-Navicat的安装

    一:Navicat Navicat是一款著名的数据库管理软件,支持大部分主流数据库(包括SQLite) 1.Navicat的安装 (1)下载该软件后,先打开该软件 (2)把文件拖入到应用程序拷贝 (3 ...