I set the font size to fit need to use the global reference file PCH file, if the PCH file already exists in your project, please ignore 1-2 steps.
Before Xcode6, the system will automatically create a PCH (precompile header) file with the name of the project, and during the development process, the header files that are widely used throughout the project can be included in the file. The compiler will automatically add the header files in the PCH file to all the source files, so that you can use the contents of the header file without using import when you need to use the relevant class, which is very convenient for programming, but after Xcode6, The system will no longer automatically create a PCH file, but sometimes it needs a PCH file, so how do I add a PCH file to Xcode6?
1. First, command+n, open the new File window: ios->other->pch file, create a PCH, add the header file name you need to introduce:
2. After adding the success, you need to manually set the link in the build, otherwise the link is not available:
Open Targets-->build Settings--Set prefix header (set PCH file path manually):
The default current project file is $ (srcroot), and the file path behind it can be viewed:
3. After setting up the PCH file, add the device-judged macro here first:
Equipment judgment
#define KDEVICE_IS_IPHONE5 ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 1136), [[UIScreen Mainscreen] currentmode].size): NO)
#define KDEVICE_IS_IPHONE6 ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake, 1334), [[UIScreen Mainscreen] currentmode].size): NO)
#define Kdevice_is_iphone6plus ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (1242, 2208), [[UIScreen Mainscreen] currentmode].size): NO)
4. There are also macros that judge the iOS8 and iOS9 systems (the font size on the iOS9 is slightly smaller than the font size on the iOS8):
Determine if the device system is 9.0 or above
#define IOS_VERSION_9_OR_ABOVE ([[[[Uidevice Currentdevice] systemversion] floatvalue] >= 9.0)? (YES):(NO))
Determine if the device system is above 9.0 (9.0 not included)
#define IOS_VERSION_8_OR_ABOVE ([[[[Uidevice Currentdevice] systemversion] floatvalue] <9.0)? (YES):(NO))
5. Preparation has been completed, next, the most central place to be, where I am using an inline function that can be implemented directly in the PCH file,
Returns different font sizes depending on the device
static inline int sizevalue (int sizefont) {
if (kdevice_is_iphone5)
{
if (Ios_version_9_or_above)
return sizefont-1;
return sizefont;
}
else if (kdevice_is_iphone6)
{
if (Ios_version_9_or_above)
return sizefont-1;
return sizefont;
}
else if (kdevice_is_iphone6)
{
if (Ios_version_9_or_above)
return sizefont+1;
return sizefont+2;
}
else if (kdevice_is_iphone6plus)
{
if (Ios_version_9_or_above)
return sizefont+2;
return sizefont+3;
}else
{
if (Ios_version_9_or_above)
return sizefont-1;
return sizefont;
}
}
Here I use is the most stupid one way to judge the device type, and then determine the iOS system, and then decide whether to set the original size of the base is increased or decreased.
(Because the width of the 4 and 5 devices is the same, so the font size of the individual settings are the same, on the 6 is on the basis of 4 on the base of the increase in the number of 2,6p on the basis of 4, the difference between iOS8 and iOS9, iOS9 the font size is smaller than the iOS8 of the size of a number, this is only personal Different font sizes can be added and decreased depending on the situation)
6. After writing the inline function, it is used, and here I use a macro as well:
Normal font
#define FONTSIZE (Sizefont) [Uifont Systemfontofsize:sizevalue (Sizefont)]
Bold font
#define FONTBOLDSIZE (Sizefont) [Uifont Boldsystemfontofsize:sizevalue (Sizefont)]
7. In the project we can use Xx.font = FontSize (Sizefont) to set the font, it can be automatically based on different devices and iOS system to adapt separately,
What you need to add here is that inline functions are functions that are decorated with the inline keyword. functions defined within a class are defaulted into inline functions. Designed for small functions that are frequently used and have little process.
This is a positive upgrade iOS9 encountered a font adaptation of the problem, then used this idea to solve, and now take out to share with you, technical shortcomings, but also please forgive me!
iOS Layout---PCH header file settings and font size adaptation