IOS UILabel text top/bottom implementation, iosuilabel

Source: Internet
Author: User

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/

 

 

Related Article

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.