iOS開發UILabel的公共屬性及拓展屬性

IOS開發的過程當中,UILabel是很經常使用的一個控件,同時也是大量使用的一個控件。建立一個UILabel通常須要五六句代碼,若是咱們須要建立幾十個UILabel,就意味着咱們要寫五六十句代碼,其實不少代碼是重複的,咱們能夠把相似的代碼寫到一個公共的方法中,以提升工做效率和下降代碼重複。官方提供UILabel的一些屬性有很大的侷限性,有些在項目中開發中須要用到的一些拓展性的屬性,根據我的經驗,也順便一塊兒總結在這裏。html

 

1、建立UILabel公共的方法java

一、頭文件中聲明方法以下:app

 

?
1
2
3
4
5
+ (UILabel *)commonLabelWithFrame:(CGRect)frame
                              text:(NSString*)text
                             color:(UIColor*)color
                              font:(UIFont*)font
                     textAlignment:(NSTextAlignment)textAlignment;


二、源文件中實現該方法:iphone

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+ (UILabel *)commonLabelWithFrame:(CGRect)frame
                              text:(NSString*)text
                             color:(UIColor*)color
                              font:(UIFont*)font
                     textAlignment:(NSTextAlignment)textAlignment
{
     UILabel *label = [[UILabel alloc] initWithFrame:frame];
     label.text = text;
     label.textColor = color;
     label.font = font;
     label.textAlignment = textAlignment;
     
     label.backgroundColor = [UIColor clearColor];
     
     return label;
}



 

2、動態設置UILabel高度ide

一、頭文件申明方法以下:測試

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
  *  建立一個動態高度的UILabel
  *
  *  @param pointX        Label的橫座標
  *  @param pointY        Label的縱座標
  *  @param width         Label的寬度
  *  @param strContent    內容
  *  @param color         字體顏色
  *  @param font          字體大小
  *  @param textAlignmeng 對齊方式
  *
  *  @return 返回一個UILabel
  */
+ (UILabel *)dynamicHeightLabelWithPointX:(CGFloat)pointX
                                    pointY:(CGFloat)pointY
                                     width:(CGFloat)width
                                strContent:(NSString *)strContent
                                     color:(UIColor *)color
                                      font:(UIFont *)font
                             textAlignmeng:(NSTextAlignment)textAlignmeng;


二、源文件中實現該方法:字體

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//動態設置Label的高度
+ (UILabel *)dynamicHeightLabelWithPointX:(CGFloat)pointX
                                    pointY:(CGFloat)pointY
                                     width:(CGFloat)width
                                strContent:(NSString *)strContent
                                     color:(UIColor *)color
                                      font:(UIFont *)font
                             textAlignmeng:(NSTextAlignment)textAlignmeng
{
     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
     paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
     
     NSDictionary *attributes = @{NSFontAttributeName:font,
                        NSParagraphStyleAttributeName:paragraphStyle.copy};
     
     CGSize labelSize = [strContent boundingRectWithSize:CGSizeMake(width,MAXFLOAT)
                                                 options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:attributes
                                                 context:nil].size;
     
     UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(pointX, pointY, width, labelSize.height)];
     [myLabel setNumberOfLines: 0 ];
     myLabel.text = strContent;
     myLabel.font = font;
     myLabel.textColor = color;
     return myLabel;
 
}


三、測試結果:ui

 

 

?
1
2
3
4
5
6
7
8
9
10
11
- ( void )viewDidLoad {
     [ super viewDidLoad];
     
     NSString *str = @6 月初,華潤華髮聯合體以 87.95 億元拿下上海閘北地塊,地塊樓面價 38061 元/平方米,刷新了其自身於 3 月創下的上海總價「地王」紀錄。同日,招商平安聯合體則以高達 2.3 萬元/平方米的樓面價,競得寶山大場鎮地塊,創出近 90 %的高溢價率。不只是一線市場,杭州、蘇州等二線市場也在 6 月初集中推地。杭州西溪溼地旁低密度住宅地塊樓面價 9975 元/平方米,溢價率 33 %,成爲 2014 年春節以來杭州溢價率最高的住宅用地。;
     
    UILabel *label = [LTLabel dynamicHeightLabelWithPointX: 5 pointY: 20 width:self.view.frame.size.width- 10 strContent:str color:[UIColor blackColor] font:[UIFont systemFontOfSize: 20.0 ] textAlignmeng:NSTextAlignmentLeft];
     label.backgroundColor = [UIColor groupTableViewBackgroundColor];
     
     [self.view addSubview:label];
     
}

 

 

(1)字體大小爲15號,與邊距間隔爲5,測試結果以下:atom

\

(2)字體大小爲20號,於邊距間隔爲5,測試結果以下:spa

\

 

(3)字體大小爲20號,於邊距間隔爲50,測試結果以下:

\

 

(4)字體大小爲20號,於邊距間隔爲5,增長文本內容,測試結果以下:

\

 

 

3、設置UILabel的對齊方式

對於官方已經提供UILabel的一些對齊方式,在這裏就不作說明了,這裏主要補充官方沒有提供的對齊方式。主要提供了三種經常使用的對齊方式:垂直頂端對齊、頂端居中對齊、頂端靠右對齊。

 

一、頭文件申明方法以下:

 

?
1
2
3
4
5
6
7
8
9
@interface DpLabel : UILabel
 
typedef enum {
     VerticalAlignmentTop = 0 , //default 垂直頂端對齊
     VerticalAlignmentMidele,  //頂端居中對齊
     VerticalAlignmentBottom,  //頂端靠右對齊
}VerticalAlignment;
 
@property (nonatomic, assign) VerticalAlignment verticalAlignment;


二、源文件實現該方法:

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# import DpLabel.h
 
@implementation DpLabel
@synthesize verticalAlignment;
 
 
- (id)initWithFrame:(CGRect)frame
{
     self = [ super initWithFrame:frame];
     if (self) {
         // Initialization code
         verticalAlignment = VerticalAlignmentTop;
     }
     return self;
}
 
- (VerticalAlignment)verticalAlignment
{
     return verticalAlignment;
}
 
- ( void )setVerticalAlignment:(VerticalAlignment)align
{
     verticalAlignment = align;
     [self setNeedsDisplay];
}
 
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
     CGRect rc = [ super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
     switch (verticalAlignment) {
         case VerticalAlignmentTop:
             rc.origin.y = bounds.origin.y;
             break ;
         case VerticalAlignmentBottom:
             rc.origin.y = bounds.origin.y + bounds.size.height - rc.size.height;
             break ;
         default :
             rc.origin.y = bounds.origin.y + (bounds.size.height - rc.size.height)/ 2 ;
             break ;
     }
     
     return rc;
}
 
- ( void )drawTextInRect:(CGRect)rect
{
     CGRect rc = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];
     [ super drawTextInRect:rc];
}
 
//調整文本中的行距的方法
 
/*使用方法
  *
  *text參數 :文本內容
  *
  *height參數:行距
  *
  *name 參數:你使用的 UIlable 對象
  */
- ( void ) getlable_height :(NSString *) text uiheight:(NSInteger) height uilable:(UILabel*) name
{
     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
     
     [paragraphStyle setLineSpacing:height]; //調整行間距
     
     [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange( 0 , [text length])];
     name.attributedText = attributedString;
     
}
 
@end


三、測試結果

 

 

?
1
2
3
4
5
6
7
8
9
<span style= "font-size:18px;" >- ( void )viewDidLoad {
     [ super viewDidLoad];
     
     DpLabel *label = [[DpLabel alloc] initWithFrame:CGRectMake( 20 , 120 , self.view.frame.size.width- 40 , 50 )];
     label.text = @測試對齊方式;
     label.textAlignment = VerticalAlignmentTop;
     label.backgroundColor = [UIColor redColor];
     [self.view addSubview:label];
}</span>



 

(1)測試垂直頂端對齊方式,測試結果以下:

\

(2)測試頂端居中對齊方式,測試結果以下:

\

(3)測試頂端靠右對齊方式,測試結果以下:

\

轉自:http://www.2cto.com/kf/201506/408343.html

相關文章
相關標籤/搜索