IOS Programming Dynamic Type 1, iosprogramming

Source: Internet
Author: User

IOS Programming Dynamic Type 1, iosprogramming

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 is a technology introduced from iOS7 to help achieve this goal by providing specially designed text styles in order to optimize clarity.

The Dynamic Type system is centered around text styles.

The Dynamic Type system is centered on the text style system.

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.

When the font requests a given text type, the system returns an appropriate configured font with the text size and related text style you like.

1 Using Preferred Fonts preference font

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.

You get a UIFont of the specified text style and implement that font to something, such as UILabel to something.

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.

Now each label has an outlet. Add a method to set the font for each label to use the perfered font format.

-(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

You can see that calling this method is at the end of viewWillAppear.

[Self updateFonts];

 

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

PreferredFontForTextStyle returns a pre-defined font for your preferences.

 

Press the Home button (or use Home from the Hardware menu), and open the 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.

Because viewWillAppear is not called, when the application returns from the background, your interface is not updated.

2 Responding to User Changes respond to User Changes

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

When the user changes the preferred text size and a notification is passed, the application object can be registered to listen for the event.

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

This notification is UIContentSizeCategoryDidChangeNotification, which is a good time to update the user interface.

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

Nsicationicationcenter * defaultCenter = [NSNotificationCenter defacenter center];

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

Name: UIContentSizeCategoryDidChangeNotification

Object: nil];

 

 

-(Void) dealloc

{
Nsicationicationcenter * defacenter center = [NSNotificationCenter

DefaultCenter];

[Defacenter center 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.

What you need to do is to use the label's intrinsicContentSize to allow them to adjust themselves based on the exact size.

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.

Each view has a moth preferred size, that is, its intrisicContentSize.

This gives each view an implied width and height constraint.

Each view has an implicit width and height limit.

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 ction.

Check the layout of the Name label and the corresponding text field in the horizontal direction.

The limit that affects these views is:

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.

Note that there is no specific limit on the width of views. Because of this, the two views both want to be the width of their instrinsicContentSize. One or two labels must be extended to meet the limits.

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.

So the view with lower content hugging priority will be extended.

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.

If you compare UILable and UITextField, you will see that the label has a content Hugging priority of 251, while the text field is 250. because the label is more like the hug, the label will be the width of the intrinsicContentSize, and the text field will be extended to satisfy the equality relationship.

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

The target is to allow all text fields

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

The method is to make the three labels have the same width.

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.