Public attributes and extended attributes of IOS UILabel

Source: Internet
Author: User

Public attributes and extended attributes of IOS UILabel

Preface

During IOS development, UILabel is a commonly used control and a frequently used control. Generally, five or six sentences of code are required to create a UILabel. If we need to create dozens of UILabel, We need to write 50 or 60 sentences of code. In fact, many codes are repeated, we can write similar code to a public method to improve work efficiency and reduce code duplication. Some attributes officially provided by UILabel have great limitations. Some extended attributes that need to be used during project development are also summarized here based on personal experience.

 

1. Create a UILabel public Method

1. The declaration method in the header file is as follows:

 

+ (UILabel *)commonLabelWithFrame:(CGRect)frame                             text:(NSString*)text                            color:(UIColor*)color                             font:(UIFont*)font                    textAlignment:(NSTextAlignment)textAlignment;

2. implement this method in the source file:

 

 

+ (UILabel *)commonLabelWithFrame:(CGRect)frame                             text:(NSString*)text                            color:(UIColor*)color                             font:(UIFont*)font                    textAlignment:(NSTextAlignment)textAlignment{    UILabel *label = [[UILabel alloc] initWithFrame:frame];    label.text = text;    label.textColor = color;    label.font = font;    label.textAlignment = textAlignment;        label.backgroundColor = [UIColor clearColor];        return label;}


 

Ii. dynamically set UILabel height

1. the header file declaration method is as follows:

 

/*** Create a dynamic height UILabel ** @ param pointX Label abscissa * @ param pointY Label ordinate * @ param width Label width * @ param strContent content * @ param color font color * @ param font size * @ param textAlignmeng aligment method ** @ return returns a UILabel */+ (UILabel *) usage :( CGFloat) pointX pointY :( CGFloat) pointY width :( CGFloat) width strContent :( NSString *) strContent color :( UIColor *) color font :( UIFont *) font textAlignmeng :( usage) textAlignmeng;

2. implement this method in the source file:

 

 

// Dynamically set the Label height + (UILabel *) dynamicHeightLabelWithPointX :( CGFloat) pointX pointY :( CGFloat) pointY width :( CGFloat) width strContent :( NSString *) strContent color :( UIColor *) color font :( UIFont *) font textAlignmeng :( NSTextAlignment) textAlignmeng {NSMutableParagraphStyle * paragraphStyle = [[externalloc] init]; paragraphStyle. lineBreakMode = NSLineBreakByWordWrapping; NSDictionary * attributes =@{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle. copy}; CGSize labelSize = [strContent boundingRectWithSize: CGSizeMake (width, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin attributes: attributes context: nil]. size; UILabel * myLabel = [[UILabel alloc] initWithFrame: CGRectMake (pointX, pointY, width, labelSize. height)]; [myLabel setNumberOfLines: 0]; myLabel. text = strContent; myLabel. font = font; myLabel. textColor = color; return myLabel ;}

3. Test results:

 

 

-(Void) viewDidLoad {[super viewDidLoad]; NSString * str = @ at the beginning of June, China Resources HUAFA consortium won the Shanghai Zhabei block for 8.795 billion yuan, with the floor price of 38061 yuan/square meter, it refresh its own Shanghai total price "land King" record set in March. On the same day, the China Merchants Ping An Alliance won a high premium rate of nearly 23 thousand at a floor price of up to 90% yuan/square meter. This is not only the first-tier market, but also the second-tier markets such as Hangzhou and Suzhou were also concentrated in the early June S. The floor price of low density residential plots next to Xixi Wetland in Hangzhou is 9975 RMB/square meter, with a premium rate of 33%. This is the highest premium residential land in Hangzhou since the Spring Festival in 2014 .; UILabel * label = [LTLabel dynamicHeightLabelWithPointX: 5 pointY: 20 width: self. view. frame. size. width-10 strContent: str color: [UIColor blackColor] font: [UIFont systemFontOfSize: 20.0] textAlignmeng: NSTextAlignmentLeft]; label. backgroundColor = [UIColor groupTableViewBackgroundColor]; [self. view addSubview: label];}

 

(1) The font size is 15 and the interval from the margin is 5. The test result is as follows:

(2) The font size is 20 and the padding interval is 5. The test result is as follows:

 

(3) The font size is 20 and the padding interval is 50. The test result is as follows:

 

(4) If the font size is 20 and the padding interval is 5, the text content is added. The test result is as follows:

 

 

3. Set alignment of UILabel

Some Alignment Methods provided by UILabel are not described here. Here we mainly add Alignment Methods not provided by the official team. Three common alignment modes are provided: vertical top alignment, center alignment on top, and right alignment on top.

 

1. the header file declaration method is as follows:

 

@ Interface DpLabel: UILabeltypedef enum {verticalignmenttop = 0, // default vertical top aligntop = 0, // The top alignment center = 0, // The top alignment is right aligned} verticalignment; @ property, assign) verticalignment;

2. source file implementation:

 

 

# Import DpLabel. h @ implementation DpLabel @ synthesize verticalignment;-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// Initialization code verticalignment = align;} return self;}-(verticalignment) verticalignment {return align;}-(void) align :( align) align {verticalignment = align; [self setNeedsDisplay];}-(CGRect) returns :( CGRect) bounds rows :( NSInteger) numberOfLines {CGRect rc = [super limit: bounds limitedToNumberOfLines: numberOfLines]; switch (Limit) {case verticalignmenttop: rc. origin. y = bounds. origin. y; break; case verticalignmentbottom: rc. origin. y = bounds. origin. y + bounds. size. height-rc. size. height; break; default: rc. origin. y = bounds. origin. y + (bounds. size. height-rc. size. height)/2; break;} return rc;}-(void) drawTextInRect :( CGRect) rect {CGRect rc = [self textRectForBounds: rect limitedToNumberOfLines: self. numberOfLines]; [super drawTextInRect: rc];} // method for adjusting the line spacing in text/* usage ** text parameter: text Content ** height parameter: line spacing ** name parameter: Your UIlable object */-(void) getlable_height :( NSString *) text uiheight :( NSInteger) height uilable :( UILabel *) name {NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString: text]; optional * paragraphStyle = [[paialloc] init]; [paragraphStyle setLineSpacing height]; // adjust the row spacing [attributedString addattri: NSParagraphStyleAttributeName value: paragraphStyle range: NSMakeRange (0, [text length])]; name. attributedText = attributedString;} @ end

3. Test Results

 

 

-(Void) viewDidLoad {[super viewDidLoad]; DpLabel * label = [[DpLabel alloc] initWithFrame: CGRectMake (20,120, self. view. frame. size. width-40, 50)]; label. text = @ test alignment; label. textAlignment = verticalignmenttop; label. backgroundColor = [UIColor redColor]; [self. view addSubview: label];}


 

(1) Test the vertical top alignment method. The test results are as follows:

(2) Test the top center alignment method. The test results are as follows:

(3) Align the top right of the test. The test results are as follows:

 

 

 

 

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.