Share a vertical top alignment UILabel

Source: Internet
Author: User

1. header file
# Import <Foundation/Foundation. h>
Typedef enum verticalignment {
VerticalAlignmentTop,
Verticalignmentmiddle,
Verticalignmentbottom,
} VerticalAlignment;

@ Interface VerticallyAlignedLabel: UILabel
{
@ Private
Verticalignment _;
}

@ Property (nonatomic, assign) verticalignment;
@ End

. M file
# Import "VerticallyAlignedLabel. h"


@ Implementation VerticallyAlignedLabel

@ Synthesize verticalignment = verticalignment _;

-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self)
{
Self. verticalAlignment = verticalignmentmiddle;
}
Return self;
}

-(Void) setVerticalAlignment :( VerticalAlignment) verticalignment
{
VerticalAlignment _ = verticalignment;
[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

 


From dread
 

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.