Comprehensive Analysis of UILable

Source: Internet
Author: User

// Create uilabel
UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (20, 40,280, 80)];
 
// Set the background color
Label. backgroundColor = [UIColor grayColor];
 
// Set the tag
Label. tag = 91;
 
// Set content
Label. text = @ "Hello World ";
 
// Set the content font and font size
Label. font = [UIFont fontWithName: @ "Arial" size: 30];
 
// The adaptive size of text is valid only when numberOfLines is set to 1.
Label. adjustsFontSizeToFitWidth = YES;
 
// The minimum font size for adaptive text reduction is 0.0 by default. YES is required on the top.
Label. minimumFontSize = 12;
 
// Text color
Label. textColor = [UIColor blueColor];
 
// The maximum number of lines in text. If the value is 0, there is no limit on the maximum number of lines.
Label. numberOfLines = 2;
 
// Highlight text
Label. highlighted = YES;
 
// Variable Text
Label. enabled = YES;
 
// Set the background color of the label to transparent
Label. backgroundColor = [UIColor clearColor];
 
// Text shadow color
Label. shadowColor = [UIColor grayColor];
 
// The Shadow is biased toward the first horizontal> 0 to the right <0 to the left and the second vertical> 0 down <0 up
Label. shadowOffset = CGSizeMake (1.0, 1.0 );
 
// Whether to interact with the user
Label. userInteractionEnabled = YES;
 
// How the text exceeds the label boundary text
Label. lineBreakMode = UILineBreakModeTailTruncation;
 
/*
Typedef enum {
 
UILineBreakModeWordWrap = 0, with spaces as the boundary, retain the default way of the entire word
UILineBreakModeCharacterWrap retains the entire character
UILineBreakModeClip, till the Boundary
UILineBreakModeHeadTruncation, Which is omitted and replaced...
UILineBreakModeTailTruncation, Which is omitted and replaced...
UILineBreakModeMiddleTruncation, omitted in the middle, replaced...
 
} UILineBreakMode;
*/
 
// The baselineAdjustment value controls the baseline position of the text. Only label. adjustsFontSizeToFitWidth = YES; the valid adaptive size must be valid label. numberOfLines is 1
Label. baselineAdjustment = UIBaselineAdjustmentNone;
 
/*
Typedef enum {
 
UIBaselineAdjustmentAlignBaselines = 0. The upper part of the text is aligned with the label midline by default.
UIBaselineAdjustmentAlignCenters. The text midline is aligned with the label midline.
UIBaselineAdjustmentNone, text bottom label midline alignment
 
} UIBaselineAdjustment;
*/
 
// Set the text alignment mode.
Label. textAlignment = UITextAlignmentCenter;
 
/*
Typedef enum {
 
UITextAlignmentLeft = 0, left alignment default mode
UITextAlignmentCenter
UITextAlignmentRight, right-aligned
 
} UITextAlignment;
*/
 
 
// Add the label to the current window
[Self. window addSubview: label];
 
// Release the label
[Label release];
 
UILabel adaptive height and automatic line feed
 
// Initialize the label
UILabel * label1 = [[UILabel alloc] initWithFrame: CGRectMake (0, 0, 0)];
 
// Set the number of automatic lines and line breaks
[Label setNumberOfLines: 0];
Label. lineBreakMode = UILineBreakModeWordWrap;
 
// Test string
NSString * s = @ "this is a test !!! Adsfsaf this is a test forget this is a test my A This is a test Ah this is a test ah 00000000 Ah this is a test run... This is a test ";
UIFont * font = [UIFont fontWithName: @ "Arial" size: 12];
 
// Set the upper limit of a row
CGSize size = CGSizeMake (320,2000 );
 
// Calculate the actual frame size and change the frame size of the label to the actual size.
CGSize labelsize = [s sizeWithFont: font constrainedToSize: size lineBreakMode: UILineBreakModeWordWrap];
Label1.frame = CGRectMake (0, 0, labelsize. width, labelsize. height );

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.