UILabel

一、簡介

<<UILabel類實現一個只讀的文本視圖。您可以使用這個類的靜態文本,如你可能會使用它來識別你的用戶界面的其他部分,繪制一個或多個行。基本UILabel類提供控制文本的外觀,包括它是否使用了一層陰影,或汲取的一大亮點。如果需要,您可以自定義文本的外觀進一步通過繼承

<<UILabel(標簽) : 是顯示文本的控件.在App中UILabel是出現頻率最高的控件之一.

<<繼承關系:UILabel --> UIView?-->UIResponder-->NSObject

<<UILabel是UIView的子類,作為子類一般是為了擴充父類的功能,UILabel擴展了文字顯示的功能,UILabel是能顯示文字的視圖.


格式為

1--> 設置文字(屬性的作用

typedef NS_ENUM(NSInteger, NSTextAlignment) {

NSTextAlignmentLeft? ? ? = 0,? ? // Visually left aligned

#if TARGET_OS_IPHONE

NSTextAlignmentCenter? ? = 1,? ? // Visually centered

NSTextAlignmentRight? ? = 2,? ? // Visually right aligned

#else /* !TARGET_OS_IPHONE */

NSTextAlignmentRight? ? = 1,? ? // Visually right aligned

NSTextAlignmentCenter? ? = 2,? ? // Visually centered

#endif

NSTextAlignmentJustified = 3,? ? // Fully-justified. The last line in a paragraph is natural-aligned.

NSTextAlignmentNatural? = 4,? ? // Indicates the default alignment for script

}(如果屬性有枚舉類型的話,這里會有枚舉類型說明

label.text? = @"Hello World!!!"; ? (這是具體的例子

@property(nullable, nonatomic,copy) ? NSString ? ? ?*text;// 設置顯示文字,?默認是空的 ??(這是屬性的說明


二、UILabel的文本屬性(屬性的順序與蘋果API一致)

1-->設置文字

label.text? = @"Hello World!!!";

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

2-->設置字號//一般方法

label.font = [UIFont systemFontOfSize:30];

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

3-->文字字體加粗//系統加粗方法

[Label setFont:[UIFont boldSystemFontOfSize:25]];

4-->設置文字字體和字號

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

5-->設置文字字體加粗

[Label setFont:[UIFont fontWithName:@"Helvetica-Bold"size:25]];

6-->設置文字顏色

label.textColor =? [UIColor blueColor];

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

7-->陰影顏色--設置文字的陰影顏色

label.shadowColor = [UIColor redColor];

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


8--> shadowOffset?陰影偏移(必須先設置文字的陰影顏色)--讓文字在原有的基礎上偏移

label.shadowOffset = CGSizeMake(2,2);

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

9-->文字對齊方式

typedef NS_ENUM(NSInteger, NSTextAlignment) {

NSTextAlignmentLeft? ? ? = 0,? ? // 居左對齊

#if TARGET_OS_IPHONE

NSTextAlignmentCenter? ? = 1,? ? //居中對齊

NSTextAlignmentRight? ? = 2,? ? //?居右對齊

#else /* !TARGET_OS_IPHONE */

NSTextAlignmentRight? ? = 1,? ? //居右對齊

NSTextAlignmentCenter? ? = 2,? ? //居中對齊

#endif

NSTextAlignmentJustified = 3,? ? //合理鋪滿 等同于居左

NSTextAlignmentNatural? = 4,? ? //默認 等同于居左

}

label.textAlignment = NSTextAlignmentCenter;

@property(nonatomic)? ? ? ? NSTextAlignment? ? textAlignment;//? 對齊方式,默認是NSTextAlignmentNatural?(iOS 9之前, 默認是 NSTextAlignmentLeft)

注意:

默認都是豎直居中的

UILabel不能設置豎直方向的排列布局,但是可以通過sizeToFit改變label的frame來實現曲線救國。

10-->斷行模式

typedef NS_ENUM(NSInteger, NSLineBreakMode) {

NSLineBreakByWordWrapping = 0,? ? //以單詞為顯示單位顯示,后面部分省略不顯示。

NSLineBreakByCharWrapping, //以字符為顯示單位顯示,后面部分省略不顯示。

NSLineBreakByClipping, //剪切與文本寬度相同的內容長度,后半部分被刪除。

NSLineBreakByTruncatingHead, //前面部分文字以……方式省略,顯示尾部文字內容。

NSLineBreakByTruncatingTail, //結尾部分的內容以……方式省略,顯示頭的文字內容

NSLineBreakByTruncatingMiddle //中間的內容以……方式省略,顯示頭尾的文字內容。

} NS_ENUM_AVAILABLE(10_0, 6_0);

label.lineBreakMode = NSLineBreakByClipping;

@property(nonatomic)? ? ? ? NSLineBreakMode? ? lineBreakMode(換行方式);// 默認是 NSLineBreakByTruncatingTail.用于多行和多行文本 字符截斷類型 設置文字過長時的顯示格式?

例子:

NSLineBreakByClipping--會出現顯示半個字的情況

NSLineBreakByTruncatingHead--沒顯示玩的文字會以省略號形式代替顯示(省略號出現在左下角)

NSLineBreakByTruncatingTail--(省略號出現在右下角)

NSLineBreakByTruncatingTail--(省略號出現在最后一行的中間位置)

三、UILabel的富文本屬性

1-->attributedText更改任意文字的顏色和字體大小

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];-->先把label上的文字賦值給可變字符串

[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5,10)];-->設置更改后的顏色和改變文字的區域

[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(20, 25)];-->設置更改后的字體大小和改變文字的區域

label.attributedText = str;-->把改后的字符串重新賦值給label

@property(nullable, nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0);?

四、UILabel的高亮屬性

1-->highlighted高亮顯示時候的文本顏色

label.highlightedTextColor=[UIColor blueColor];//高亮顯示時候的文本顏色

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

2-->highlighted是否高亮顯示

label.highlighted=YES;

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

3-->設置是否能與用戶進行交互

label.userInteractionEnabled = YES;

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

4-->設置label中的文字是否可變

label.enabled = YES;

@property(nonatomic,getter=isEnabled)? ? ? ? ? ? ? ? BOOL enabled; // 設置label中的文字是否可變,默認值是YES

五、UILabel的換行屬性

1-->設置文字換行

label.numberOfLines = 0;

@property(nonatomic) NSInteger numberOfLines;//換行?默認值是1行。0值意味著沒有限制

注意:

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

// Δ 這里需要去理解一下

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

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

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

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

六、UILabel的自適應屬性

1-->設置字體自適應adjustsLetterSpacingToFitWidth

label.adjustsLetterSpacingToFitWidth=YES;

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

2-->控制文本基線

typedef NS_ENUM(NSInteger, UIBaselineAdjustment) {

UIBaselineAdjustmentAlignBaselines = 0, // default. used when shrinking text to position based on the original baseline?默認,文本最上端與中線對齊

UIBaselineAdjustmentAlignCenters,//文本中線與label中線對齊

UIBaselineAdjustmentNone,//文本最低端與label中線對齊。

};

label.baselineAdjustment=UIBaselineAdjustmentAlignBaselines;

@property(nonatomic) UIBaselineAdjustment baselineAdjustment; // 默認是UIBaselineAdjustmentAlignBaselines,如果adjustsFontSizeToFitWidth屬性設置為YES,這個屬性就來控制文本基線的行為

3-->minimumScaleFactor設置最小收縮比例

label.minimumScaleFactor=0.5;

@property(nonatomic) CGFloat minimumScaleFactor NS_AVAILABLE_IOS(6_0); // 默認是0

注意:

Fixed?Font?Size默認,如果label寬度小于文字長度時,文字大小不自動縮放

minimumScaleFactor設置最小收縮比例,如果Label寬度小于文字長度時,文字進行收縮,收縮超過比例后,停止收縮。

4-->設置多行label的最大寬度的

label.allowsDefaultTighteningForTruncation=YES;這個屬性是用來設置多行label的最大寬度的

@property(nonatomic) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE_IOS(9_0); // 默認是NO

注意:

當自動布局的時候約束這個label的時候這個屬性會起作用

在自動布局添加約束中,若文本超過了指定的最大寬度的時候?文本會另起一行?從而增加了label的高度

七、UILabel的圖和定位覆蓋方法

1-->計算UILabel隨字體多行后的高度

CGRect bounds = CGRectMake(0, 0, 200, 300);

heightLabel = [myLabel textRectForBounds:bounds

limitedToNumberOfLines:20]; //計算20行后的Label的Frame

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

2-->描邊label

- (void)drawTextInRect:(CGRect)rect {

CGSizeshadowOffset =self.shadowOffset;

UIColor*textColor =self.textColor;

?CGContextRef c = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(c,self.outlineWidth);

?CGContextSetLineJoin(c, kCGLineJoinRound);?

CGContextSetTextDrawingMode(c, kCGTextStroke);

self.textColor=self.outlineColor;?

[superdrawTextInRect:rect];

?CGContextSetTextDrawingMode(c, kCGTextFill);

self.textColor= textColor;

self.shadowOffset= CGSizeMake(0,0);?

[superdrawTextInRect:rect];

self.shadowOffset= shadowOffset;

}

- (void)drawTextInRect:(CGRect)rect;

八、UILabel的auto layou屬性

1-->設置了auto layou的適配寬度

label.preferredMaxLayoutWidth =label.frame.size.width;

iOS6開始 UILabel下面需要設置preferredMaxLayoutWidth ,設置了autolayout和numberofline的UIlabel才顯示多行

九、UILabel的邊框圓角屬性

1-->borderWidth設置邊框寬度

label.layer.borderWidth=2;

2-->borderColor設置邊框顏色

label.layer.borderColor=[UIColor blueColor].CGColor

3-->設置圓角顏色

label.backgroundColor=[UIColor blueColor];;

4-->設置圓角半徑

label.layer.cornerRadius =5;

十、UILabel的邊框圓角拓展

第一種方法:通過設置layer的屬性

最簡單的一種,但是很影響性能,一般在正常的開發中使用很少.

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100,100,100,100)];

//只需要設置layer層的兩個屬性

//設置圓角

imageView.layer.cornerRadius = imageView.frame.size.width /2;

//將多余的部分切掉

imageView.layer.masksToBounds =YES;

[self.view addSubview:imageView];


第二種方法:使用貝塞爾曲線UIBezierPath和Core Graphics框架畫出一個圓角

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100,100,100,100)];

imageView.image= [UIImage imageNamed:@"1"];//開始對imageView進行畫圖UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO,1.0);//使用貝塞爾曲線畫出一個圓形圖[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];

[imageView drawRect:imageView.bounds];

imageView.image=UIGraphicsGetImageFromCurrentImageContext();//結束畫圖UIGraphicsEndImageContext();

[self.view addSubview:imageView];


第三種方法:使用CAShapeLayer和UIBezierPath設置圓角

首先需要導入<AVFoundation/AVFoundation.h>

UIImageView*imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100,100,100,100)];

imageView.image= [UIImage imageNamed:@"1"];

UIBezierPath*maskPath =[UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];

CAShapeLayer*maskLayer =[[CAShapeLayer alloc]init];

//設置大小

maskLayer.frame =imageView.bounds;

//設置圖形樣子

maskLayer.path =maskPath.CGPath;

imageView.layer.mask=maskLayer;

[self.view addSubview:imageView];


這三種方法中第三種最好,對內存的消耗最少啊,而且渲染快速。

出處:http://www.lxweimin.com/p/f091fe0d06ed

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • UILabel是一個常用的控件,它的屬性設置的方法在純代碼中經常使用。在storyboard中,使用UILabel...
    坤哥lqk閱讀 862評論 0 2
  • 對于UILabel其實我覺得并沒有太多的要素需要仔細去弄明白的,因為至今為止,我所了解到的label屬性不外乎就是...
    懶惰的習慣閱讀 578評論 0 0
  • 不知道有多少人聽過這樣的傳聞: 據說,在某些學校門口,有一些豪車的車頂會放著一瓶或兩瓶飲料。如果有人上去拿瓶子就意...
    朱三萌閱讀 619評論 6 24
  • 什么是市場營銷?其基本目的是什么? 以前理解的市場營銷是商品和顧客的關系,是推銷的這個過程。今天看到他的準確定義不...
    向應昌閱讀 1,335評論 1 3
  • 老爹是20多年的老煙民了。 都說夫妻之間互補是最和諧的,作為家里兩位大寶貝的婚姻見證者和副產品,我深以為然。 老爹...
    16118f1f5e2c閱讀 339評論 8 9