iOS Uilabel settings are aligned, centered, aligned

Source: Internet
Author: User

The text in the default Uilabel in iOS can be aligned vertically in the vertical direction, and I inherit a new class from Uilabel, enabling alignment, alignment, and alignment. Specific as follows:

1. Create a new class VerticalAlignmentLabel.h inherit from Uilabel

2.

//

VerticalAlignmentLabel.h

Inface

//

Created by Huangzengsong on 15/5/10.

Copyright (c) 2015 Huangzs. All rights reserved.

//

#import <UIKit/UIKit.h>

typedef enum

{

Verticalalignmenttop = 0,//default

Verticalalignmentmiddle,

Verticalalignmentbottom,

} VerticalAlignment;

@interface Verticalalignmentlabel:uilabel

{

@private

VerticalAlignment _verticalalignment;

}

@property (nonatomic) VerticalAlignment verticalalignment;

@end

3.

//

verticalalignmentlabel.m

Inface

//

Created by Huangzengsong on 15/5/10.

Copyright (c) 2015 Huangzs. All rights reserved.

//

#import "VerticalAlignmentLabel.h"

@implementation Verticalalignmentlabel

@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];

}

/*

Only override Drawrect:if perform custom drawing.

An empty implementation adversely affects performance during animation.

-(void) DrawRect: (cgrect) Rect {

Drawing Code

}

*/

@end

4. Importing header files when calling

#import "VerticalAlignmentLabel.h"

5.

[Self. Detaillabel Setverticalalignment:verticalalignmenttop];

iOS Uilabel settings are aligned, centered, aligned

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.