详解UILabel的adjustsFontSizeToFitWidth值

简介:

详解UILabel的adjustsFontSizeToFitWidth值

UILabel有一个属性值,叫adjustsFontSizeToFitWidth,看着名字就知道,他是用来让文字自动适应UILabel宽度的。

先看看显示效果:

测试用源码:

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

以下是核心代码:

 

目录
相关文章
给UILabel控件添加一个分类
给UILabel控件添加一个分类
90 0
给UILabel控件添加一个分类
|
Swift
Swift - 如何让UIView,UILabel和UIImageView之间类型互相转化
Swift - 如何让UIView,UILabel和UIImageView之间类型互相转化
143 0
UITextView根据NSString计算Size
UITextView根据NSString计算Size
63 0
同一个UILabel不同的字体颜色
同一个UILabel不同的字体颜色
66 0
三种方法设置UITextField的占位文字颜色
三种方法设置UITextField的占位文字颜色
240 0
|
开发工具
UIView的clipsTobounds属性
UIView的clipsTobounds属性
121 0
UIView的clipsTobounds属性
UITextField 自定义使用
UITextField自定义使用(一)UITextField自定义使用(二)
515 0
|
数据安全/隐私保护