IOS UILabel text top/bottom implementation, iosuilabel
The default text position of the iOS UILabel control is centered ,:
However, we often encounter this kind of requirement. We hope that the text can be topped up or down, but unfortunately, the iOS API does not provide the corresponding attributes and methods. We need to set them manually.
ExploitationCategory(Category) add attributes isTop and isBottom for UILabel to control whether the text is set to the top and bottom.
Implementation: Add "\ n" to the front of the text to fill the text with the entire UILable control to achieve the top/Top Effect
. H file
#import <UIKit/UIKit.h>@interface UILabel (TextAlign)@property (nonatomic, assign) BOOL isTop;@property (nonatomic, assign) BOOL isBottom;@end
. M file
# Import "UILabel + TextAlign. h "@ implementation UILabel (TextAlign)-(void) setIsTop :( BOOL) isTop {if (isTop) {CGSize fontSize = [self. text sizeWithFont: self. font]; // The height of the control divided by the height of a line of text int num = self. frame. size. height/fontSize. height; // calculate the number of linefeeds int newLinesToPad = num-self. numberOfLines; self. numberOfLines = 0; for (int I = 0; I <newLinesToPad; I ++) // Add a line break "/n" self. text = [self. text stringByAppendingString: @ "\ n"] ;}}-(void) setIsBottom :( BOOL) isBottom {if (isBottom) {CGSize fontSize = [self. text sizeWithFont: self. font]; // The height of the control divided by the height of a line of text int num = self. frame. size. height/fontSize. height; // calculate the number of linefeeds int newLinesToPad = num-self. numberOfLines; self. numberOfLines = 0; for (int I = 0; I <newLinesToPad; I ++) // Add the line break "/n" self. text = [NSString stringWithFormat: @ "\ n % @", self. text] ;}}@ end
Usage:
Import header files
#import "UILabel+TextAlign.h"
Then set properties
// Top self. lb. isTop = YES; // bottom self. lb. isBottom = YES;
Source code free: http://www.jinhusns.com/Products/Download/