UILabel 詳解


UILabel 多行文字自動換行 (自動折行)

1
.UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 300, 180)]; 2. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 150)]; 3. label.text = @"where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?"; 4. //清空背景顏色 5. label.backgroundColor = [UIColor clearColor]; 6. //設置字體顏色為白色 7. label.textColor = [UIColor whiteColor]; 8. //文字居中顯示 9. label.textAlignment = UITextAlignmentCenter; 10. //自動折行設置 11. label.lineBreakMode = UILineBreakModeWordWrap; 12. label.numberOfLines = 0;

 

iOS的UILabel設置居上對齊,居中對齊,居下對齊

在iOS中默認的UILabel中的文字在豎直方向上只能居中對齊,博主參考國外網站,從UILabel繼承了一個新類,實現了居上對齊,居中對齊,居下對齊。具體如下:

[cpp]  view plain copy
  1. //  
  2. //  myUILabel.h  
  3. //    
  4. //  
  5. //  Created by yexiaozi_007 on 3/4/13.  
  6. //  Copyright (c) 2013 yexiaozi_007. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. typedef enum  
  11. {  
  12.     VerticalAlignmentTop = 0, // default  
  13.     VerticalAlignmentMiddle,  
  14.     VerticalAlignmentBottom,  
  15. } VerticalAlignment;  
  16. @interface myUILabel : UILabel  
  17. {  
  18. @private  
  19. VerticalAlignment _verticalAlignment;  
  20. }  
  21.   
  22. @property (nonatomic) VerticalAlignment verticalAlignment;  
  23.   
  24. @end  

 

[cpp]  view plain copy
  1. //  
  2. //  myUILabel.m  
  3. //    
  4. //  
  5. //  Created by yexiaozi_007 on 3/4/13.  
  6. //  Copyright (c) 2013 yexiaozi_007. All rights reserved.  
  7. //  
  8.   
  9. #import "myUILabel.h"  
  10.   
  11. @implementation myUILabel  
  12. @synthesize verticalAlignment = verticalAlignment_;  
  13.   
  14. - (id)initWithFrame:(CGRect)frame {  
  15.     if (self = [super initWithFrame:frame]) {  
  16.         self.verticalAlignment = VerticalAlignmentMiddle;  
  17.     }  
  18.     return self;  
  19. }  
  20.   
  21. - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {  
  22.     verticalAlignment_ = verticalAlignment;  
  23.     [self setNeedsDisplay];  
  24. }  
  25.   
  26. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {  
  27.     CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];  
  28.     switch (self.verticalAlignment) {  
  29.         case VerticalAlignmentTop:  
  30.             textRect.origin.y = bounds.origin.y;  
  31.             break;  
  32.         case VerticalAlignmentBottom:  
  33.             textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;  
  34.             break;  
  35.         case VerticalAlignmentMiddle:  
  36.             // Fall through.  
  37.         default:  
  38.             textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;  
  39.     }  
  40.     return textRect;  
  41. }  
  42.   
  43. -(void)drawTextInRect:(CGRect)requestedRect {  
  44.     CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];  
  45.     [super drawTextInRect:actualRect];  
  46. }  
  47.   
  48.   
  49. @end  

 

在使用時:

 

[cpp]  view plain copy
    1. lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];  
    2. UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明圖片作為label的背景色  
    3. lbl_mylabel.backgroundColor = color;  
    4. lbl_mylabel.textAlignment = UITextAlignmentLeft;  
    5. lbl_mylabel.textColor = UIColor.whiteColor;  
    6. lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;  
    7. lbl_mylabel.numberOfLines = 0;  
    8. [lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];  
    9. [self addSubview:lbl_mylabel]; 

 

 

ios UILabel 變量名不能為title

-[UILabel copyWithZone:]: unrecognized selector sent to instance

 
遇到了這樣一個錯誤,找了半天沒找到是什么錯誤,於是,Google搜索,打開第一個鏈接http://stackoverflow.com/questions/10784207/uilabel-copywithzone-unrecognized-selector-sent-to-instance
 
UILabel 設置過長文本中間為省略號

 label.lineBreakMode = NSLineBreakByTruncatingMiddle;

 
http://blog.csdn.net/zhaopenghhhhhh/article/details/16331041
·UILable是iPhone界面最基本的控件,主要用來顯示文本信息。
·常用屬性和方法有:
1、創建
CGRect rect = CGRectMake(100, 200, 50, 50);
UILabel *label = [[UILabel alloc] initWithFrame:rect];
2、text //設置和讀取文本內容,默認為nil
label.text = @”文本信息”; //設置內容
NSLog(@”%@”, label.text); //讀取內容
3、textColor //設置文字顏色,默認為黑色
lable.textColor = [UIColor redColor];
4、font //設置字體大小,默認17
label.font = [UIFont systemFontOfSize:20]; //⼀一般方法
label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法
label.font = [UIFont fontWithName:@"Arial" size:16]; //指定
字體的方法
//還有⼀一種從外部導入字體的方法。
5、textAlignment //設置標簽文本對齊方式。
label.textAlignment = NSTextAlignmentCenter; //還有
NSTextAlignmentLeft、 NSTextAlignmentRight.
6、numberOfLines //標簽最多顯示行數,如果為0則表示多行。
label.numberOfLines = 2;
7、enabled //只是決定了Label的繪制方式,將它設置
為NO將會使文本變暗,表示它沒有激活,這時向它設置顏色值是無效的。
label.enable = NO;
8、highlighted //是否高亮顯示
label.highlighted = YES;
label.highlightedTextColor = [UIColor orangeColor]; //高亮
顯示時的文本顏色
9、ShadowColor //設置陰影顏色 
[label setShadowColor:[UIColor blackColor]];
10、ShadowOffset //設置陰影偏移量
[label setShadowOffset:CGSizeMake(-1, -1)];
11、baselineAdjustment //如果adjustsFontSizeToFitWidth屬性設
置為YES,這個屬性就來控制文本基線的行為。
label.baselineAdjustment = UIBaselineAdjustmentNone;
UIBaselineAdjustmentAlignBaselines = 0,默認,文本最上端與中線對齊。
UIBaselineAdjustmentAlignCenters,  文本中線與label中線對齊。
UIBaselineAdjustmentNone, 文本最低端與label中線對齊。
12、Autoshrink //是否自動收縮
Fixed Font Size 默認,如果Label寬度小於文字長度時時,文字大小不自動縮放
minimumScaleFactor 設置最小收縮比例,如果Label寬度小於文字長度時,文字
進行收縮,收縮超過比例后,停止收縮。
minimumFontSize 設置最小收縮字號,如果Label寬度小於文字長度時,文字字號
減小,低於設定字號后,不再減小。//6.0以后不再使用了。
label.minimumScaleFactor = 0.5;
13、adjustsLetterSpacingToFitWidth //改變字母之間的間距來適應Label大小
myLabel.adjustsLetterSpacingToFitWidth = NO;
14、 lineBreakMode //設置文字過長時的顯示格式             
label.lineBreakMode = NSLineBreakByCharWrapping;以字符為顯示單位顯
示,后面部分省略不顯示。
label.lineBreakMode = NSLineBreakByClipping;剪切與文本寬度相同的內
容長度,后半部分被刪除。
label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字
以……方式省略,顯示尾部文字內容。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;中間的內容
以……方式省略,顯示頭尾的文字內容。
label.lineBreakMode = NSLineBreakByTruncatingTail;結尾部分的內容
以……方式省略,顯示頭的文字內容。
label.lineBreakMode = NSLineBreakByWordWrapping;以單詞為顯示單位顯
示,后面部分省略不顯示。
15、 adjustsFontSizeToFitWidth //設置字體大小適應label寬度  
label.adjustsFontSizeToFitWidth = YES;
16、attributedText:設置標簽屬性文本。
NSString *text = @"first";
NSMutableAttributedString *textLabelStr =
[[NSMutableAttributedString alloc]
initWithString:text];
[textLabelStr
setAttributes:@{NSForegroundColorAttributeName :
[UIColor lightGrayColor], NSFontAttributeName :
[UIFont systemFontOfSize:17]} range:NSMakeRange(11,
10)];
label.attributedText = textLabelStr;
17、豎排文字顯示每個文字加一個換行符,這是最方便和簡單的實現方式。
label.text = @"請\n豎\n直\n方\n向\n排\n列";
label.numberOfLines = [label.text length];


18、計算UIlabel 隨字體多行后的高度
CGRect bounds = CGRectMake(0, 0, 200, 300);
heightLabel = [myLabel textRectForBounds:bounds
limitedToNumberOfLines:20]; //計算20行后的Label的Frame
NSLog(@"%f",heightLabel.size.height);
19、UILabel根據字數多少自動實現適應高度
UILabel *msgLabel = [[UILabel alloc]
initWithFrame:CGRectMake(15, 45, 0, 0)];
msgLabel.backgroundColor = [UIColor lightTextColor];
[msgLabel setNumberOfLines:0];
msgLabel.lineBreakMode = UILineBreakModeWordWrap;
msgLabel.font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(290, 1000);
msgLabel.text = @"獲取到的deviceToken,我們可以通過webservice服務提
交給.net應用程序,這里我簡單處理,直接打印出來,拷貝到.net應用環境中使
用。";
CGSize msgSie = [msgLabel.text sizeWithFont:fonts
constrainedToSize:size];
[msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];


20、漸變字體Label
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage
imageNamed:@"btn.png"]];
NSString *title = @"Setting";
UILabel *titleLabel = [[UILabel alloc]
initWithFrame:CGRectMake(0, 0, 80, 44)];
titleLabel.textColor = titleColor;
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
[titleLabel release];
21、Label添加邊框
titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
titleLabel.layer.borderWidth = 2;

 

 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM