iOS Label居上

来源:互联网 发布:什么是销存软件 编辑:程序博客网 时间:2024/05/01 22:43


#import <UIKit/UIKit.h>

typedef enum

{

    VerticalAlignmentTop = 0,// default

    VerticalAlignmentMiddle,

    VerticalAlignmentBottom,

} VerticalAlignment;




@interface MyLabel : UILabel

{

 @privateVerticalAlignment _verticalAlignment;

}


@property (nonatomic)VerticalAlignment verticalAlignment;


#import "MyLabel.h"


@implementation MyLabel


@synthesize verticalAlignment =verticalAlignment_;


- (id)initWithFrame:(CGRect)frame {

    if (self = [superinitWithFrame:frame]) {

        self.verticalAlignment =VerticalAlignmentMiddle;

    }

    returnself;

}


- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {

    verticalAlignment_ = verticalAlignment;

    [selfsetNeedsDisplay];

}


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

    CGRect textRect = [supertextRectForBounds:boundslimitedToNumberOfLines:numberOfLines];

    switch (self.verticalAlignment) {

        caseVerticalAlignmentTop:

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

            break;

        caseVerticalAlignmentBottom:

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

            break;

        caseVerticalAlignmentMiddle:

            // 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 = [selftextRectForBounds:requestedRectlimitedToNumberOfLines:self.numberOfLines];

    [superdrawTextInRect:actualRect];

}


使用

lbl_mylabel = [[UILabelalloc] initWithFrame:CGRectMake(20,50, 150, 60)];

    UIColor *color = [UIColorredColor];//使用半透明图片作为label的背景色

    lbl_mylabel.backgroundColor = color;

    lbl_mylabel.textColor =UIColor.whiteColor;

    lbl_mylabel.numberOfLines =0;

    lbl_mylabel.text =@"//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用//使用";

//    [lbl_mylabel set/VerticalAlignment:VerticalAlignmentTop];

    [self.viewaddSubview:lbl_mylabel];

    // Do any additional setup after loading the view, typically from a nib.


0 0
原创粉丝点击