首页 > 技术文章 > iOS之UILabel的基本用法

chixuedong 2016-03-02 16:06 原文

  1 #import "ViewController.h"
  2 #import <CoreText/CoreText.h>
  3 @interface ViewController ()
  4 
  5 @property (strong, nonatomic) UILabel *label;
  6 
  7 @property (strong, nonatomic) UILabel *MuTabelLabel;
  8 
  9 @end
 10 
 11 @implementation ViewController
 12 
 13 - (void)viewDidLoad {
 14     [super viewDidLoad];
 15 
 16 /*----------------------------------------设置Label基本属性------------------------------------------------------*/
 17 
 18     //初始化,设置坐标
 19     self.label = [[UILabel alloc] initWithFrame:CGRectMake(25, 200, 325, 100)];
 20    
 21     //设置Label文字
 22     self.label.text = @"今天天气真冷,天气又要降温了,大家要注意保暖啊";
 23     
 24     //设置Label文字格式(居中对齐等)
 25     self.label.textAlignment = NSTextAlignmentCenter;
 26     
 27     //设置Label的行数(默认是1)
 28     self.label.numberOfLines = 2;
 29     
 30     //设置Label文字颜色
 31     self.label.textColor = [UIColor orangeColor];
 32     
 33     //设置Label的背景颜色
 34     self.label.backgroundColor = [UIColor grayColor];
 35     
 36     //设置Label的文字的阴影颜色
 37     self.label.shadowColor = [UIColor cyanColor];
 38   
 39     //设置Label文字的阴影部分的偏移量
 40     self.label.shadowOffset = CGSizeMake(5, 30);
 41     
 42     //默认是NSLineBreakByTruncatingTail。用于单和多行文本
 43     self.label.lineBreakMode = NSLineBreakByTruncatingTail;
 44     /*
 45          NSLineBreakByWordWrapping = 0,//以空格为边界,保留单词 (包装在单词边界)
 46          NSLineBreakByCharWrapping,    //保留整个字符 (包装在字符边界)
 47          NSLineBreakByClipping,        //简单剪裁,到边界为止
 48          NSLineBreakByTruncatingHead,  //按照"……文字"显示
 49          NSLineBreakByTruncatingTail,  //按照"文字……"显示
 50          NSLineBreakByTruncatingMiddle //按照"文字……文字"显示
 51      */
 52     
 53     //启用用户交互(触发touchBegan)
 54     self.label.userInteractionEnabled = YES;
 55     
 56     //更改标签如何绘制(默认是YES)
 57     self.label.enabled = YES;
 58     
 59  /*--------------------------------设置Label自动版式,属性文本的设置----------------------------------------------*/
 60     
 61     //属性字符串
 62     NSMutableAttributedString *String = [[NSMutableAttributedString alloc] initWithString:@"忽如一夜春风来,千树万树梨花开"];
 63     
 64     //设置属性字符串的字体大小(方法1)add方法
 65     [String addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(3, 4)];
 66     
 67     //设置属性字符串的颜色(方法2)set方法
 68     [String setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,nil] range:NSMakeRange(0, 4)];
 69     
 70     //设置属性字符串的下划线样式,需要导入<CoreText/CoreText.h>头文件
 71     [String addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 9)];
 72     
 73     //初始化一个新的Label
 74     self.MuTabelLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 400, 325, 100)];
 75     self.MuTabelLabel.backgroundColor = [UIColor cyanColor];
 76     
 77     //给Label添加属性文本
 78     self.MuTabelLabel.attributedText = String;
 79     
 80     //设置对齐基线,默认是否定的。如果是的,文本将缩小minFontSize基线
 81     self.MuTabelLabel.adjustsFontSizeToFitWidth = YES;
 82     /*  UIBaselineAdjustmentAlignBaselines //文本最上端与Label中线对齐,默认值
 83         UIBaselineAdjustmentAlignCenters   //文本中线与Label中线对齐
 84         UIBaselineAdjustmentNone           //文本最下端与Label中线对齐
 85      */
 86     
 87     //UILabel接受的最小字体
 88     self.MuTabelLabel.minimumScaleFactor = 12;
 89     
 90     //当父试图的interaction置为NO,子控件,子试图的也不会触发
 91     self.view.userInteractionEnabled = YES;
 92     
 93 /*-------------------------------添加到视图---------------------------------------------*/
 94     
 95     //加载到视图中
 96 //    [self.view addSubview:self.label];
 97 
 98     [self.view addSubview:self.MuTabelLabel];
 99 }
100 
101 //响应者链 :如果父试图的userInteractionEnable置为NO,那么子试图变无法响应(响应者链的原理)
102 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
103 {
104     NSLog(@"%@",[touches valueForKey:@"view"]);
105 }
106 
107 
108 
109 - (void)didReceiveMemoryWarning {
110     [super didReceiveMemoryWarning];
111     // Dispose of any resources that can be recreated.
112 }
113 
114 @end

 

推荐阅读