iOS開發UI篇--UILabel

1、簡介

  •  UILabel類實現了一個只讀文本視圖。您能夠使用這個類來畫一個或多個行靜態文本,好比你可能使用肯定的其餘部分的用戶界面。UILabel類支持既簡單又複雜的樣式標籤的文本,還能夠控制外觀,好比標籤是否使用一個影子或吸引了一大亮點。
  •   在iOS程序中,看的見、摸得着的,都是UIView的子類。UILabel是一個用於顯示文字信息的標籤視圖類,即UIView的子類。

  

如下是關於UILabel的官方網址:https://developer.apple.com/reference/uikit/uilabelapp

 

 2、關於UILabel的蘋果官方API的解釋 less

     

1、訪問文本屬性

 

@property(nullable, nonatomic,copy)   NSString           *text; // 設置顯示文字, 默認是空的          ide

@property(null_resettable, nonatomic,strong) UIFont      *font;            //  設置字體(系統字體默認17號字體)字體

@property(null_resettable, nonatomic,strong) UIColor     *textColor;       //  字體的顏色(默認是黑色)ui

@property(nullable, nonatomic,strong) UIColor            *shadowColor;     //   陰影的顏色this

@property(nonatomic)        CGSize             shadowOffset;    // 陰影的偏移量,默認是 CGSizeMake(0, -1) -- a top shadow  atom

 

@property(nonatomic)        NSTextAlignment    textAlignment;   //  對齊方式,默認是左對齊spa

@property(nonatomic)        NSLineBreakMode    lineBreakMode(換行方式);   // 默認是 NSLineBreakByTruncatingTail. 用於單和多行文本  換行方式orm

 

 

2、富文本

是帶有屬性的字符串blog

 the underlying attributed string drawn by the label, if set, the label ignores the properties above.//底層帶屬性字符串的標籤,若是設置,能夠忽略了上面的屬性。至關於標籤的另外一種呈現方法。

@property(nullable, nonatomic,copy)   NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0);  // default is nil

  

關於富文本的使用會單獨作介紹。

 

3、高亮屬性

 the 'highlight' property is used by subclasses for such things as pressed states. it's useful to make it part of the base class as a user property

//設置高亮  

@property(nullable, nonatomic,strong)               UIColor *highlightedTextColor; // 高亮 狀態的字體顏色

@property(nonatomic,getter=isHighlighted) BOOL     highlighted;          //是否高亮

 

@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  // default is NO  設置是否能與用戶進行交互,默認沒有打開交互 

@property(nonatomic,getter=isEnabled)                BOOL enabled;                 // default is YES. changes how the label is drawn  設置label中的文字是否可變,默認值是YES

  

 

4、換行

 this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit

 

  •  這決定的行數和調用sizeToFit時該作什麼,默認值是1行。0值意味着沒有限制

// if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be

// truncated using the line break mode.

 

@property(nonatomic) NSInteger numberOfLines;//換行

 

 

 

5、自適應

  these next 3 property allow the label to be autosized to fit a certain width by scaling the font size(s) by a scaling factor >= the minimum scaling factor

  • 下面3個屬性,容許標籤經過一個比例因子(s)> =最低比例因子,自適應縮放字體大小獲得合理的寬度

 

 

@property(nonatomic) BOOL adjustsFontSizeToFitWidth;         // default is NO  設置字體大小適應label寬度     默認NO     

 

@property(nonatomic) UIBaselineAdjustment baselineAdjustment; // default is UIBaselineAdjustmentAlignBaselines 

 //若是adjustsFontSizeToFitWidth屬性設置爲YES,這個屬性就來控制文本基線的行爲

@property(nonatomic) CGFloat minimumScaleFactor NS_AVAILABLE_IOS(6_0); // default is 0.0

 

 

  Tightens inter-character spacing in attempt to fit lines wider than the available space if the line break mode is one of the truncation modes before starting to truncate.

 

  • 收緊inter-character間距在嘗試適應更普遍的比可用空間若是換行模式是一種截斷模式以前開始截斷。

 

// The maximum amount of tightening performed is determined by the system based on contexts such as font, line width, etc.

@property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE_IOS(9_0); // default is NO

 

  override points. can adjust rect before calling super.

 

 

6、Drawing and Positioning Overrides 圖和定位覆蓋

 

// label has default content mode of UIViewContentModeRedraw

 

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

- (void)drawTextInRect:(CGRect)rect;

 

 

7、auto layou相關的代碼(作屏幕適配的時候用)

 

 Support for constraint-based layout (auto layout)

  

// If nonzero, this is used when determining -intrinsicContentSize for multiline labels

@property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);

 

 

8、已經廢棄不用的

 

@property(nonatomic) CGFloat minimumFontSize NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED; // deprecated - use minimumScaleFactor. default is 0.0

 

// Non-functional.  Hand tune by using NSKernAttributeName to affect tracking, or consider using the allowsDefaultTighteningForTruncation property.

@property(nonatomic) BOOL adjustsLetterSpacingToFitWidth NS_DEPRECATED_IOS(6_0,7_0) __TVOS_PROHIBITED;

 

@end

 

NS_ASSUME_NONNULL_END

 

3、代碼例子

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self createLabel];

}

- (void)createLabel{

    

    //1. 建立一個標籤 設置大小, 位置

    

    UILabel *label = [[UILabel alloc] init];

    

    label.frame = CGRectMake(0, 0, 200, 400);

    

    // UILabel是繼承自UIView的, 全部的view都是矩形

    

    // CGRect 是一個結構體, 裏面包含了一個點, 和一個大小  點==用來肯定顯示的位置,表示一個視圖左上角的座標        大小==肯定範圍(寬高)

    

    

    

    //2. 設置背景顏色

    

    // 默認是透明的

    

    label.backgroundColor = [UIColor redColor];

    

 

    //3. 設置顯示文字

    

    NSString *text = @"hello world";

    

    label.text = text;

    

    //4. 設置顯示內容的顏色

    

    label.textColor = [UIColor blueColor];

         

    //5. 對齊方式

    

    /**

     

     NSTextAlignmentLeft      = 0,  // 居左對齊

     

     NSTextAlignmentCenter    = 1,  // 居中對齊

     

     NSTextAlignmentRight           // 居右對齊

     

     */

    

    label.textAlignment = NSTextAlignmentCenter;

    

    //6.設置字體(UIFont)(label默認的字體大小是17)

    

    label.font = [UIFont systemFontOfSize:30];      // 系統字體

    

    label.font = [UIFont boldSystemFontOfSize:30];  // 系統字體 加粗

    

    label.font = [UIFont italicSystemFontOfSize:30];// 系統字體 斜體(不支持中文...)

     

    // 自定義字體

    

    // 能夠獲取當前設備支持的全部的字體

    

    NSArray *allfonts = [UIFont familyNames];

    

    NSLog(@"%@", allfonts);

    

    // 使用自定義字體(非系統的字體)(也不支持中文)

    

    label.font = [UIFont fontWithName:@"Futura" size:30];

    

//    7. 字的陰影(shadow)

    

    label.shadowColor = [UIColor greenColor];   // 設置陰影的顏色

    

    label.shadowOffset = CGSizeMake(2, 2);      // 設置陰影的偏移量

    

 

    

//    8. 換行

    

    // 最大顯示的行數(默認是1)

    

    // Δ 這裏須要去理解一下

    

    //  1. 當label的內容足夠多, 並且, label足夠高, 最大顯示numberOfLines行

    

    //  2. 當label的內容足夠多, 可是, label的高度不夠高, 最大顯示label能容納多少行

    

    //  3. 當label的內容不夠多, 能顯示多少行, 顯示多少行

    

    // 0 表示不限制最大行數

    

    label.numberOfLines = 0;

         

//    9. 換行方式

    

    /**

     

     NSLineBreakByWordWrapping = 0, // 以單詞換行

     

     NSLineBreakByCharWrapping,      // 以字母換行(iOS 7無效)

     

     NSLineBreakByClipping,          //

     

     NSLineBreakByTruncatingHead,    // 以單詞換行, 省略最後一行開頭

     

     NSLineBreakByTruncatingTail,    // 以單詞換行, 省略最後一行結尾

     

     NSLineBreakByTruncatingMiddle   // 以單詞換行, 省略最後一行中間

     

     */

    

    label.lineBreakMode = NSLineBreakByTruncatingTail;

    

 

//    10.設置是否能與用戶進行交互

    label.userInteractionEnabled = YES;

    //  給label添加一個點擊手勢

    [label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(test)]];

    

//    11.設置label中的文字是否可變,默認值是YES

    label.enabled = YES;

//    12.設置字體大小適應label寬度     默認NO

    

    label.adjustsFontSizeToFitWidth = YES;

    

//    13.若是adjustsFontSizeToFitWidth屬性設置爲YES,這個屬性就來控制文本基線的行爲

     

    label.baselineAdjustment = UIBaselineAdjustmentNone;

 

    typedef enum {

        UIBaselineAdjustmentAlignBaselines,

        UIBaselineAdjustmentAlignCenters,

        UIBaselineAdjustmentNone,

    } UIBaselineAdjustment;

 

//    14.設置高亮

    label.highlighted = YES;

    label.highlightedTextColor = [UIColor orangeColor];

    

    

//     15.label根據文字改變高度

    

    [label sizeToFit];

     

//    16. 根據文字的內容, 文字的大小, 換行.. 來獲取須要佔用的大小

    

    // 1. 文字須要顯示的字體

    

    // 2. 最大顯示的寬度

    

    CGSize size = [text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

     NSLog(@"%@", NSStringFromCGSize(size));

    

    label.frame = CGRectMake(0, 0, size.width, size.height);

     

#pragma mark=====UILabel的另外一種展示形式--富文本====

     

//能夠設置其中某段內容的顯示樣式

 

// 1. string 表示要顯示的內容

// 2. attributes 表示這個字符串的屬性

 

    // NSAttributeString只能總體設置屬性

    // NSMutableAttributeString  可變, 能夠單獨給某一段設置屬性

    NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:text];

 

    /*  經常使用屬性key:

 

     NSFontAttributeName : 設置字體的大小

     

     NSForegroundColorAttributeName : 設置字體的鏤空顏色

     

     NSBackgroundColorAttributeName : 設置字體的背景顏色

     

     NSParagraphStyleAttributeName: 段落樣式屬性

     

     */

     

    

 // 1. 實例化一個段落樣式

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];

    

  // 2. 設置行間距

    style.lineSpacing = 5;

     

// 3. 給att添加一個屬性

 //  給某一個範圍的內容添加屬性,range:就是 NSAttributeString和NSMutableAttributeString 區別所在

    [att addAttributes:@{

                          

                         NSForegroundColorAttributeName:[UIColor purpleColor],

                         

                         NSFontAttributeName:[UIFont systemFontOfSize:20],

                         

                         NSBackgroundColorAttributeName: [UIColor greenColor],

                         

                         NSParagraphStyleAttributeName:style

                     

                         } range:NSMakeRange(0, text.length)];

         

    label.attributedText = att;

     

    [self.view addSubview:label];

     

}

 

 

- (void)test{

 

    NSLog(@" just a test");

 

}

 

 

博主的話

最近又寫了個demo,點擊標籤能夠調用相冊。地址:https://i.cnblogs.com/Files.aspx。 

相關文章
相關標籤/搜索