如題,今天突發奇想,在iPhone中如果系統文字大小改變后,應用內文字大小會不會跟隨變化。於是我就進行了實驗。在設置中改變了文字大小之后,我發現基本上大部分的APP(系統除外)都不會跟隨改變。(如美團、餓了么、淘寶、支付寶等)
只有微信提示是否更換大小(微信內置了更改文字大小),QQ大部分跟隨系統改變。
那么它是如何做到的?
我發現可以根據一個iOS10新特性來實現這個功能。
下面貼代碼,用代碼說話:
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text = @"測試,金蝶雞MMIFMIIRFMIMIRIFMI";
// 設置文字字體跟隨
myLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
// 設置字體大小可以改變
myLabel.adjustsFontForContentSizeCategory = YES;
myLabel.numberOfLines = 0;
myLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:myLabel];
UIButton *MyButton = [UIButton buttonWithType:UIButtonTypeCustom];
MyButton.frame = CGRectMake(100, 350, 100, 100);
[MyButton setTitle:@"測試" forState:UIControlStateNormal];
MyButton.backgroundColor = [UIColor blueColor];
MyButton.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
MyButton.titleLabel.adjustsFontForContentSizeCategory = YES;
[self.view addSubview:MyButton];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(250, 350, 200, 100)];
textView.backgroundColor = [UIColor orangeColor];
textView.text = @"交分解誒偶記對將誒偶家的飢餓哦,djiejijd eij dijeiji";
textView.font =[UIFont preferredFontForTextStyle:UIFontTextStyleBody];
textView.adjustsFontForContentSizeCategory = YES;
[self.view addSubview:textView];
