QQ咨詢
官方微信掃一掃
宏順傳媒官方微信二維碼

宏順視角

關(guān)注互聯(lián)網(wǎng),關(guān)注技術(shù)開(kāi)發(fā),透析與分享移動(dòng)互聯(lián)網(wǎng)行業(yè)最新動(dòng)態(tài)


iOS中UILabel設(shè)置居上對(duì)齊、居中對(duì)齊、居下對(duì)齊及文字置頂顯示

2017-12-19 08:28:16

在iOS中默認(rèn)的UILabel中的文字在豎直方向上只能居中對(duì)齊,博主參考國(guó)外網(wǎng)站,從UILabel繼承了一個(gè)新類,實(shí)現(xiàn)了居上對(duì)齊,居中對(duì)齊,居下對(duì)齊。具體如下:?123456789101112131415161718192021////myUILabel.h//////Createdbyyexiaozi_007on3/4/13.//Copyright(c)2013yexiaozi_007.Allr...

在iOS中默認(rèn)的UILabel中的文字在豎直方向上只能居中對(duì)齊,博主參考國(guó)外網(wǎng)站,從UILabel繼承了一個(gè)新類,實(shí)現(xiàn)了居上對(duì)齊,居中對(duì)齊,居下對(duì)齊。

具體如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//

// myUILabel.h

//

//

// Created by yexiaozi_007 on 3/4/13.

// Copyright (c) 2013 yexiaozi_007. All rights reserved.

//

#import <UIKit/UIKit.h>

typedef enum

{

 VerticalAlignmentTop = 0, // default

 VerticalAlignmentMiddle,

 VerticalAlignmentBottom,

} VerticalAlignment;

@interface myUILabel : UILabel

{

@private

VerticalAlignment _verticalAlignment;

}

@property (nonatomic) VerticalAlignment verticalAlignment;

@end

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

//

// myUILabel.m

//

//

// Created by yexiaozi_007 on 3/4/13.

// Copyright (c) 2013 yexiaozi_007. All rights reserved.

//

#import "myUILabel.h"

@implementation myUILabel

@synthesize verticalAlignment = verticalAlignment_;

  

- (id)initWithFrame:(CGRect)frame {

 if (self = [super initWithFrame:frame]) {

 self.verticalAlignment = VerticalAlignmentMiddle;

 }

 return self;

}

- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {

 verticalAlignment_ = verticalAlignment;

 [self setNeedsDisplay];

}

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

 CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];

 switch (self.verticalAlignment) {

 case VerticalAlignmentTop:

  textRect.origin.y = bounds.origin.y;

  break;

 case VerticalAlignmentBottom:

  textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;

  break;

 case VerticalAlignmentMiddle:

  // Fall through.

 default:

  textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;

 }

 return textRect;

}

-(void)drawTextInRect:(CGRect)requestedRect {

 CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];

 [super drawTextInRect:actualRect];

}

@end

在使用時(shí):

1

2

3

4

5

6

7

8

9

lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];

UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明圖片作為label的背景色

lbl_mylabel.backgroundColor = color;

lbl_mylabel.textAlignment = UITextAlignmentLeft;

lbl_mylabel.textColor = UIColor.whiteColor;

lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;

lbl_mylabel.numberOfLines = 0;

[lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];

[self addSubview:lbl_mylabel];

UILabel 讓文字置頂顯示

我們經(jīng)常會(huì)遇到將Label中文字置頂,也就是將文字頂?shù)絃able框的最頂端顯示的需求,UILabel是無(wú)法對(duì)內(nèi)容文字進(jìn)行置頂處理的,所以,如果我們不對(duì)Label加以額外的設(shè)置,就會(huì)出現(xiàn)如下情況:

 

相關(guān)閱讀




在線咨詢
在線客服
微信咨詢
咨詢
試用
4.0