• <acronym id="immdh"><label id="immdh"></label></acronym>
    QQ咨詢
    官方微信掃一掃
    宏順傳媒官方微信二維碼

    宏順視角

    關注互聯網,關注技術開發,透析與分享移動互聯網行業最新動態


    iOS中UILabel設置居上對齊、居中對齊、居下對齊及文字置頂顯示

    2017-12-19 08:28:16

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

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

    具體如下:

    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

    在使用時:

    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 讓文字置頂顯示

    我們經常會遇到將Label中文字置頂,也就是將文字頂到Lable框的最頂端顯示的需求,UILabel是無法對內容文字進行置頂處理的,所以,如果我們不對Label加以額外的設置,就會出現如下情況:

     

    相關閱讀




    在線咨詢
    在線客服
    微信咨詢
    咨詢
    試用
    4.0
    国内女人牲交视频播放_欧美阿v视频在线观_人妻中文無碼久熱絲襪_XXXX性按摩BBBB