IOS UILabel Text overhead/bottom-up implementation

Source: Internet
Author: User
Tags uikit

The default text position for IOS Uilabel controls is centered:

However, we often meet the need for text to top up, or down the bottom, but unfortunately, the IOS API does not provide the corresponding properties and methods, we need to manually set.

Use the category to add attributes Istop and Isbottom to Uilabel to control whether text is pinned and bottom-mounted.

Implementation: Use to add "\ n" to the front of the text to fill the text filled with the entire uilable control for the top/top effect

. h file

#import <UIKit/UIKit.h>@interface UILabel (TextAlign)@property (nonatomicassignBOOL isTop;@property (nonatomicassignBOOL isBottom;@end

. m file

#import "Uilabel+textalign.h"  @implementation UILabel (TextAlign)-(void) Setistop: (BOOL) Istop {if(Istop) {cgsizeFontSize = [ Self. TextSizewithfont: Self. Font];//height of the control divided by the height of the line text        intnum = Self. Frame. Size. Height/fontsize. Height;//Calculate the number of line breaks to add        intNewlinestopad = num- Self. NumberOfLines; Self. NumberOfLines=0; for(intI=0; i<newlinestopad; i++)//Add line break "/n" after text             Self. Text= [ Self. Textstringbyappendingstring:@"\ n"]; }}-(void) Setisbottom: (BOOL) Isbottom {if(Isbottom) {cgsizeFontSize = [ Self. TextSizewithfont: Self. Font];//height of the control divided by the height of the line text        intnum = Self. Frame. Size. Height/fontsize. Height;//Calculate the number of line breaks to add        intNewlinestopad = num- Self. NumberOfLines; Self. NumberOfLines=0; for(intI=0; i<newlinestopad; i++)//Add line feed "/n" before text             Self. Text= [NSStringstringwithformat:@"\n%@", Self. Text]; }}@end

How to use:
Import Header File

#import "UILabel+TextAlign.h"

Then set the properties

//置顶self.lb.isTopYES;//置底self.lb.isBottomYES;

IOS UILabel Text overhead/bottom-up implementation

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.