iOS之UILabel的基本用法

  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
相關文章
相關標籤/搜索