詳解UILabel的adjustsFontSizeToFitWidth值測試
UILabel有一個屬性值,叫adjustsFontSizeToFitWidth,看着名字就知道,他是用來讓文字自動適應UILabel寬度的。spa
先看看顯示效果:code
測試用源碼:blog
// // RootViewController.m // Letters // // Created by YouXianMing on 14-9-14. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 30)]; label.layer.borderWidth = 1.f; CGPoint point = self.view.center; point.y = 70; label.center = point; label.text = @"YouXianMing NoZuoNoDie"; label.textAlignment = NSTextAlignmentCenter; label.adjustsFontSizeToFitWidth = YES; label.baselineAdjustment = UIBaselineAdjustmentAlignCenters; [self.view addSubview:label]; } { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)]; label.layer.borderWidth = 1.f; CGPoint point = self.view.center; point.y = 120; label.center = point; label.text = @"YouXianMing NoZuoNoDie"; label.textAlignment = NSTextAlignmentCenter; label.adjustsFontSizeToFitWidth = YES; label.baselineAdjustment = UIBaselineAdjustmentAlignCenters; [self.view addSubview:label]; } { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)]; label.layer.borderWidth = 1.f; CGPoint point = self.view.center; point.y = 170; label.center = point; label.text = @"YouXianMing NoZuoNoDie"; label.textAlignment = NSTextAlignmentCenter; label.adjustsFontSizeToFitWidth = YES; label.baselineAdjustment = UIBaselineAdjustmentAlignCenters; [self.view addSubview:label]; } { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; label.layer.borderWidth = 1.f; CGPoint point = self.view.center; point.y = 220; label.center = point; label.text = @"YouXianMing NoZuoNoDie"; label.textAlignment = NSTextAlignmentCenter; label.adjustsFontSizeToFitWidth = YES; label.baselineAdjustment = UIBaselineAdjustmentAlignCenters; [self.view addSubview:label]; } } @end
如下是核心代碼:源碼