IOS Programming Dynamic Type 2, iosprogramming

Source: Internet
Author: User
Tags call back

IOS Programming Dynamic Type 2, iosprogramming

IOS Programming Dynamic Type 2

You will need to update two parts of this view controller for Dynamic Type: the rows of your table view will grow or shrink in response to the user changing the preferred text size, and the BNRItemCell will need to be updated similarly to how you updated the BNRDetailViewController.

The row of your table view will increase or decrease to respond to changes to the preferred text size. BNRItemCell will know how to update the data in BNRDetailViewController.

The goal is to have the table view row heights reflect the preferred Dynamic Type text size of the user.

The purpose is to make the table view row height reflect the preferred Dynamic Type text size.

If the user chooses a larger text size, the rows will be taller in order to accommodate the text.

If you select a larger text size, rows will become higher to adapt to text.

Since this is not a problem that Auto Layout will solve, the row heights will need to be set manually.

Because auto layout will solve, this is not a problem, and row heights will be set to manual.

To do this, you need a way of determining which text size the user has selected.

To do this, you need a way to determine which text size the user wants to choose.

UIApplication exposes the text size that user selected through its preferredContentSizeCategory property.

UIApplication reports the selected text size through its preferredContentSizeCategory attribute.

The method will return a constant NSString with the name of the content size category,

This method returns a string named content size category.

Which will be one of the following values:

(1) UIContentSizeCategoryExtraSmall

(2) UIContentSizeCategorySmall

(3) UIContentSizeCategoryMedium

(4) UIContentSizeCategoryLarge (Default)

(5) UIContentSizeCategoryExtraLarge

(6) UIContentSizeCategoryExtraExtraLarge

(7) UIContentSizeCategoryExtraExtraExtraLarge

Open BNRItemsViewController. m. Create a method that will update the table view row height based on the user-selected text size and call this method in viewWillAppear :.

 

[Self updateTableViewForDynamicTypeSize];

 

Just as you did with the BNRDetailViewController earlier, you need to have the BNRItemsViewController register itself as an observer for the UIContentSizeCategoryDidChangeNotification.

Like in BNRDetailViewController, You need to register BNRItemsViewController as the observer of UIContentSizeCategoryDidChangeNotification.

 

In BNRItemsViewController. m, register for the notification in init, and remove the view controller as an observer in dealloc. Finally, implement the notification call back to call the updateTableViewForDynamicTypeSize method that you just created.

 

Nsicationicationcenter * nc = [NSNotificationCenter defacenter center];

[Nc addObserver: self

Selector: @ selector (updateTableViewForDynamicTypeSize) name: UIContentSizeCategoryDidChangeNotification

Object: nil];

 

-(Void) dealloc

{
Nsicationicationcenter * nc = [NSNotificationCenter defacenter center];

[Nc removeObserver: self];

}

 

Dealloc

Deallocates the memory occupied by the receiver.

Release recipient's memory

Subsequent messages to the caller may generate an error indicating that a message was sent to a deallocated object (provided the deallocated memory hasn't been reused yet ).

The message sent to the receiver may generate an error, indicating that the message has been sent to the deallocated object.

You override this method to dispose of resources other than the object's instance variables,

In addition to the instance variables of the object, you also need to override this method.

For example:

-(Void) dealloc {

Free (myBigBlockOfMemory );

}

 

2.1 dealloc

In an implementation of dealloc, do not invoke the superclass's implementation.

In dealloc implementation, do not call the implementation of superclasses.

You shoshould try to avoid managing the lifetime of limited resources such as file descriptors using dealloc.

You should try to avoid managing the lifecycle of limited resources.

You never send a dealloc message directly. Instead, an object's dealloc method is invoked by the runtime.

You never directly send a dealloc message. The dealloc method of an object is called at runtime.

When not using ARC, your implementation of dealloc must invoke the superclass's implementation as its last instruction.

When ARC is not applicable, your dealloc must call its superclass implementation as its final command.

 

You don't need dealloc method in ARC.

You do not need to use dealloc in arc.

But if you want to do some cleanup tasks when your view is dismissing or released. It's the best place, In such case you can implement it.

But when your view is dismissing or released, you want to do some cleanup tasks. This is your best position. In this case, you can implement it.

 

You are running a timer in your view and it's updating your view. when you are dismissed the view you need to stop that timer. in that condition you can use dealloc method and stop timer there.

You run a timer in your view. When you dismissed this view, you need to stop timer. In this case, you need to use the dealloc method to stop timer here.

 

You are therefore allowed to subclass dealloc under ARC, but you are not allowed to call [super dealloc] from within the subclassed method.

[Super dealloc] cannot be called under ARC.

3. Updating BNRItemCell

Let's begin by implementing the Dynamic Type code to update the labels, which will closely follow what you did with BNRDetailViewController and BNRItemsViewController.

Let's start implementing Dynamic Type code to update labels.

Open BNRItemCell. m and make the following changes:

-(Void) updateInterfaceForDynamicTypeSize
{

UIFont * font = [UIFont preferredFontForTextStyle: UIFontTextStyleBody];

Self. nameLabel. font = font;

Self. serialNumberLabel. font = font;
Self. valueLabel. font = font;
}

 

-(Void) awakeFromNib
{
[Self updateInterfaceForDynamicTypeSize];
Nsicationicationcenter * nc = [NSNotificationCenter defacenter center]; [nc addObserver: self
Selector: @ selector (updateInterfaceForDynamicTypeSize) name: UIContentSizeCategoryDidChangeNotification

Object: nil];

}

 

-(Void) dealloc

{
Nsicationicationcenter * nc = [NSNotificationCenter defacenter center];

[Nc removeObserver: self];}

 

The only new piece of information here is the awakeFromNib method. this is called on an object after it has been unarchived from a NIB file, and is a great place to do any additional UI work that cannot be done within the XIB file.

AwakeFromNib is called by an object that has been taken out of a NIB file. This is a suitable place for additional UI work that cannot be done in XIB files.

You can do any additional configuration for the cell that cannot be done within the XIB file within this method.

You can do any additional configuration for this cell, which cannot be done in the XIB file.

4 Constraint outlets

It makes sense that the image shocould scale with the text size. Let's do this.

Scale down the image and text size together.

To update the position or size of a view, either in absolute terms or relative to another view, you should update the constraints on that view.

To update the position or size of a view, whether it is absolute or related to another view, you should update the constraints on these views.

In order to change the width and height of the image view, the constants on the respective constraints will need to be updated at run time.

In order to change the width and height of the image view, the corresponding restrictions should all change at runtime.

To do this, you will need to create an outlet to both the vertical and horizontal constraints.

To do this, you should create an outlet for the horizontal and vertical limits.

Constraints are objects (NSLayoutConstraint), so just like you can create outlets to views, the same can be done with constraints.

Constraints is an object (NSLayoutConstraint), So you create constraints just like creating an outlet of a view.

 

Q: If it was written by Programming, how can I set outlet?

 

In BNRItemCell. m, create and connect the outlet for these two constraints to the class extension. When you are done, the class extension shoshould look like this:

@ Interface BNRItemCell ()

@ Property (nonatomic, weak) IBOutlet NSLayoutConstraint * imageViewHeightConstraint;
@ Property (nonatomic, weak) IBOutlet NSLayoutConstraint * imageViewWidthConstraint;

@ End

With outlets to the size constraints of imageView created, you can now adjust imageView's size programmatically.

Due to the limitations of these imageviews, you can adjust the size of the imageView through programmatically.

In BNRItemCell. m, modify updateInterfaceForDynamicTypeSize to get the currently selected preferred text size, and use that to adjust the size of imageView.

Obtains the selected text size to adjust the size of the image view.

Static NSDictionary * imageSizeDictionary;

If (! ImageSizeDictionary ){

ImageSizeDictionary =@{ UIContentSizeCategoryExtraSmall: @ 40,

UIContentSizeCategorySmall: @ 40, UIContentSizeCategoryMedium: @ 40, UIContentSizeCategoryLarge: @ 40, comment: @ 45, comment: @ 55, comment: @ 65 };

NSString * userSize = [[UIApplication sharedApplication] preferredContentSizeCategory];

NSNumber * imageSize = imageSizeDictionary [userSize];

Self. imageViewHeightConstraint. constant = imageSize. floatValue;

Self. imageViewWidthConstraint. constant = imageSize. floatValue;

 

4 Placeholder constraints

Instead of updating both constraints, you will add one additional constraint to imageView that will constrain the imageView's width and height to be equal.

Instead of updating two constraints, you can add a constraint: constrain the imageView with the same width and height to the imageView.

You cannot create this constraint in Interface Builder, so return to BNRItemCell. m and create this constraint programmatically in awakeFromNib.

You cannot use Interface Builder, so you can return BNRItemCell. m and create these constraint in awakeFromNib through programming.

NSLayoutConstraint * constraint =
[NSLayoutConstraint constraintWithItem: self. thumbnailView

Attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual

ToItem: self. thumbnailView attribute: NSLayoutAttributeWidth

Multiplier: 1 constant: 0];

[Self. thumbnailView addConstraint: constraint];

 

Now, remove the imageViewWidthConstraint property and corresponding code.

There are now two constraints affecting the width of the imageView: the programmatic constraint you just created and the explicit width constraint in the XIB file.

Now you have two limits to affect the imageview width. The programming limitations you have created and the limitations explicitly specified in the XIB file.

This will create unsatisfiable (conflicting) constraints if the two constraints do not agree on a size.

If the two limits are not agreed, unsatisfiable constraints will appear.

Instead, you can make the width constraint a placeholder constraint.

You can create a width constraint as a placeholder constraint.

Placeholder constraints, as the name implies, are only temporary and are removed at build time, so they will not exist when the application is running.

Placeholder constraints, as the name implies, is only temporary and removed during build time, so they do not exist when the application runs.

N BNRItemCell. xib, select the width constraint on the imageView, and open the attributes inspector. Check the Placeholder box that says Remove at build time

 

 

 

 

 

 

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.