Custom Uilabel Set the vertical direction of the home, centered, in the bottom

Source: Internet
Author: User

The Uilabel property of the iOS system frame textalignment only adjusts horizontally to the center, left and right, without vertical adjustment. Therefore, to customize a class that inherits from Uilabel, the text is redrawn in the implementation file of the class, and the vertical orientation is adjusted.

Create a new class file that inherits from Uilabel, and the header file is as follows:

#import <uikit/uikit.h>typedef Ns_enum (nsinteger,verticalalignment) {    verticalalignmenttop,    Verticalalignmentmiddle,    Verticalalignmentbottom}; @interface Fsverticallyalignedlabel:uilabel@property ( nonatomic,assign) VerticalAlignment VerticalAlignment; @end

In the. m file, implement the VerticalAlignment setting method

@implementation fsverticallyalignedlabel-(ID) initWithFrame: (CGRect) frame{if (self = [Super Initwithframe:frame]) {    Self.verticalalignment = Verticalalignmentmiddle; } return self;}    /** * Set Property method * * @param verticalalignment vertically adjust position */-(void) Setverticalalignment: (verticalalignment) verticalalignment{        _verticalalignment = VerticalAlignment; [Self setneedsdisplay];} /** * Calculates the rectangle area of the text * * @param bounds label Rectangle area * @param numberoflines number of lines * * @return returns the rectangular area occupied by the text */-(cgrect) textr Ectforbounds: (CGRect) Bounds Limitedtonumberoflines: (nsinteger) numberoflines{cgrect textrect = [super        Textrectforbounds:bounds Limitedtonumberoflines:numberoflines];  Adjust the vertical position by setting the Y value of the font area switch (self.verticalalignment) {Case VERTICALALIGNMENTTOP:TEXTRECT.ORIGIN.Y =            BOUNDS.ORIGIN.Y;        Break  Case VERTICALALIGNMENTMIDDLE:TEXTRECT.ORIGIN.Y = bounds.origin.y + (bounds.size.height-textrect.size.height)            /2.0;Break            Case VERTICALALIGNMENTBOTTOM:TEXTRECT.ORIGIN.Y = bounds.origin.y + bounds.size.height-textrect.size.height;    Break } return textrect;} Overriding the parent class method-(void) Drawtextinrect: (cgrect) rect{cgrect actualrect = [self textrectforbounds:rect limitedtonumberoflines:    Self.numberoflines]; [Super Drawtextinrect:actualrect];} @end

Custom Uilabel Set the vertical direction of the home, centered, in the bottom

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.