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