adolsai's echo

Objective-C Learning Notes - UILabel

UILabel

    Base Attributes:

  • font

  • size

  • backgroundColor & align

    • UITextAlignmentLeft

    • UITextAlignmentCenter

    • UITextAlignmentRight

  • textColor

  • adjustsFontSizeToFitWidth


动态实现:

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建 UILabel 对象实例
    UILabel *label = [[UILabel alloc]initWithFrame:self.view.bounds];
    //赋值 label 显示的文本内容
    label.text = @"This is a UILabel Demo,";
    //设置字体和大小
    label.font = [UIFont fontWithName:@"Arial" size:35];
    //设置自提颜色
    label.textColor = [UIColor yellowColor];
    //设置字体对齐方式
    label.textAlignment = NSTextAlignmentCenter;
    //设置该 UILabel 背景颜色
    label.backgroundColor = [UIColor blackColor];
    //设置分词???
    label.lineBreakMode = NSLineBreakByWordWrapping;
    //行数限制???
    label.numberOfLines = 0;
    
    //设置 frame
    CGSize size = [label.text sizeWithFont:label.font constrainedToSize:self.view.bounds.size lineBreakMode:label.lineBreakMode];
    
    CGRect rect = label.frame;
    rect.size.height = size.height;
    label.frame = rect;
    
    [self.view addSubview:label];
    // Do any additional setup after loading the view, typically from a nib.
}



UILabelx 类实现垂直方向对齐:

UILabelx.h 文件:

#import <UIKit/UIKit.h>
//我记得这个好像叫枚举类型
typedef enum
{
    //垂直方向靠上对齐
    VerticalAlignmentTop,
    //垂直方向居中对齐
    VerticalAlignmentMiddle,
    //垂直方向靠底对齐
    VerticalAlignmentBottom
} VerticalAlignment;

@interface UIlabelEx : UILabel {     VerticalAlignment _verticalAlignment; } @property (nonatomic,assign) VerticalAlignment verticalAlignment; @end


UILabelx.m 文件:(妈的排版吃粑粑!!干嘛总省略在 self 后面的空格,你们就不能找个懂代码的来排版吗???)

#import "UIlabelEx.h"
@implementation UIlabelEx
@synthesize verticalAlignment = _verticalAlignment;

//使用frame构造实例 -(id) initWithFrame:(CGRect)frame {     if (self == [super initWithFrame:frame])     {         self.verticalAlignment = VerticalAlignmentMiddle;     }          return self; } //设置文本显示类型 -(void)setVerticalAlignment:(VerticalAlignment)verticalAlignment{     _verticalAlignment = verticalAlignment;     [self setNeedsDisplay]; } //重写父类 (CGRect) textRectForBounds: (CGRect)bounds limitedToNumberOfLines:(NSIterger)numberOfLines -(CGRect) textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines{     CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];     //针对枚举变量 verticalAlignment 值计算 label text 的y 轴位置     switch (self.verticalAlignment) {         //默认顶置, y 轴不变         case VerticalAlignmentTop:             textRect.origin.y = bounds.origin.y;             break;                      //底置, y 轴下移(整个 bounds 高度 + textRect 高度)         case VerticalAlignmentBottom:             textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;             break;                      //居中, y 轴下移(整个 bounds 高度 - textRect 高度)/2         case VerticalAlignmentMiddle:         default:             textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height)/2.0;     }     return textRect;      } //重写父类 -(void) drawTextInRect:(CGRect)rect //这啥? -(void) drawTextInRect:(CGRect)rect{     CGRect realRect = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];     [super drawTextInRect:realRect]; } @end


设置字体:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *label = [[UILabel alloc] init];
    
    label.frame = self.view.bounds;
    
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    label.text = @"good";
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.backgroundColor = [UIColor blackColor];
    
    label.textColor = [UIColor whiteColor];
    
    label.font = [UIFont fontWithName:@"Zapfino" size:48];
    
    [self.view addSubview:label];
    
    // Do any additional setup after loading the view, typically from a nib.
}

文本水平对齐:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"UITextAlignment";
    
    self.view.backgroundColor = [UIColor blackColor];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 30)];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 320, 30)];
    
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 90, 320, 30)];
    
    label1.textAlignment = NSTextAlignmentLeft;
    label2.textAlignment = NSTextAlignmentCenter;
    label3.textAlignment = NSTextAlignmentRight;
    
    label1.text = @"UITextAlignmentLeft";
    label2.text = @"UITextAlignmentCenter";
    label3.text = @"UITextAlignmentRight";
    
    label1.backgroundColor = [UIColor yellowColor];
    label2.backgroundColor = [UIColor yellowColor];
    label3.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:label1];
    [self.view addSubview:label2];
    [self.view addSubview:label3];
    
    // Do any additional setup after loading the view, typically from a nib.
}


标签:#ios #object-c #uilabel