Uilabel is a commonly used control in iOS programming, and here's a way to share Uilabel's related property settings.
A lot of learning iOS6 programming starts with storyboard, and when you use Uilabel, you drag controls to the storyboard to build the implementation, if you want the code in the-(void) viewdidload to be like [_label initWithFrame: The CGRectMake (x,y,width,height)] method changes the size of the label that is dragged to the storyboard, because the program loads the code before the-(void) viewdidload, and then loads the storyboard. Instead, storyboard generates the label, which overrides the size set in the-(void) viewdidload, by the size it was set when dragging the control, so the size of the label should be dynamically created with the code Uilabel.
Uilabel * label = [[Uilabel alloc] Initwithframe:cgrectmake (50, 50, 200, 400)];
This enables the creation of a label in code where initWithFrame sets the position of the label and the size, where cgrectmake can declare cgrect rect =cgrectmake (50,50,200,400) by a separate declaration Then the variable rect is placed in the method initWithFrame to realize the cgrectmake of the four values of the RECT coordinates x value, coordinate y value, width, height. The second sentence [Self.view Addsubview:label] is to add a child view in the current view Self.view by calling the Addsubview method, just like a sticker on it, because the added order is different, and the later one added child view obscures the previous child view, which is also mentioned later when you add a background image to the label.
or use the following code to change the size of the label.
Label.frame = CGRectMake (97, 47, 223, 19);
Set Label (TAG)
Set the text content of a label
Or
NSString *labeltext = @ "ABCD";
Assign the value of a string to the label
Set the text type and size of a label
Label.font = [Uifont systemfontofsize:12];//with system default text setting size
Set the text color of a label
Label.textcolor = [Uicolor lightgraycolor];//textcolor to use Uicolor type
Set how text is aligned
There are three ways to set the TextAlignment: Nstextalignmentleft to the left, Nstextalignmentcenter to the center, nstextalignmentright to the right
If some articles are introduced with Uitextalignmentcenter/uitextalignmentleft/uitextalignmentright, which is the previous use of IOS6, IOS6 's latest usage has been changed
When the contents of the text are many and the label cannot be displayed in full, the label replaces the text with the ellipsis, and the following is the setting of the label text ellipsis
Label.linebreakmode =nslinebreakbycharwrapping;//where Linebreakmode optional value is
linbreakmode enum{
Nslinebreakbywordwrapping = 0,//preserves the entire word, nslinebreakbycharwrapping,//the entire character with a space boundary
nslinebreakbyclipping,/ /to the boundary
nslinebreakbytruncatinghead,//omit the beginning, replace nslinebreakbytruncatingtail,//with ellipsis, omit the
end, replace
with ellipsis nslinebreakbytruncatingmiddle//omit the middle, substituting ellipsis for
}
Set the number of lines of text
You can use numberoflines=0 when you want to set the number of rows that are unlimited
When the label size uses the SizeToFit method, the size is adjusted to take into account the value stored in the property. For example, if this property is set to the 3,sizetofit method, the label is resized so that it is large enough to display three lines of text.
Implementing multiple lines of text display
Commenttextlabel.linebreakmode = nslinebreakbycharwrapping;
Text automatically adjusts font size based on label size
Label.numberoflines =1;
Label.adjustsfontsizetofitwidth =yes;
The Adjustfontsizetofitwidth method enables the text to automatically adjust the font size according to the label size until the text reaches the maximum minimum size of the label text, the minimum value, and the smallest value of the string. If you use this method there is a big limit is that only when the numberoflines set to 1 o'clock to use
If the number of rows is more than 1 lines, to automatically adjust the font size function, there is no adaptive system method can be used, only the implementation of their own code, in the design, because to consider the actual size of the phone screen is limited, if the font is too small can affect the user experience, so to set a minimum size of the judgment, Less than the minimum font size to use the thumbnail display, the following code is mainly used to
Cgsize size = [text Sizewithfont:font constrainedtosize:cgsizemake (MB, 180) linebreakmode:nslinebreakbycharwrapping] ;
To get the height of the font in a certain font size, to determine the consistency with the label height, where text is the text of the input label, Sizwithfont set the font, constrainedtosize set the constraint text rectangle size parameters, where the width is consistent with the label , height set to high enough, much higher than the label, otherwise the text will appear incomplete problem, the role of Linebreakmode said above. If the calculated height exceeds the label height, the font size is reduced in a circular manner until the height fits and jumps out of the loop.
Float maxheight =50;//Set maximum height float minfontsize = 9;
float height;
int fontsize = 31;//Set the maximum font size nsstring *text = @ "Enter text content";
do {fontsize = fontSize-1;
Uifont *font =[uifont fontwithname:@ "Arial" size:fontsize]; Cgsize size = [text Sizewithfont:font constrainedtosize:cgsizemake (100, 180)/* Width is the same width as label, height should be higher than label height *
Linebreakmode:nslinebreakbycharwrapping];
Height = size.height;
NSLog (@ "height=%f,fontsize=%d,text=%@", Height,fontsize,text);
while (height > maxheight&&fontsize>minfontsize);
Uilabel *label = [[Uilabel alloc] Initwithframe:cgrectmake (50, 50, 100, 50)];
Label.text =text;
if (fontsize ==9) {//Determines whether the font is less than the minimum font size, use the system default thumbnail when it is less than the minimum font size Label.font = [Uifont fontwithname:@ ' Arial ' size:15];
} else{label.font = [Uifont fontwithname:@ "Arial" size:fontsize];
Label.linebreakmode = nslinebreakbycharwrapping;//Implement multiple lines of text display label.numberoflines = 0; } [Self.view Addsubview:label];
Automatically adjust label height based on number of text
In fact, the above method is used to reproduce the height of label
NSString *text =[[nsstring Alloc]init];
Text = @ "Enter text content";
Cgsize size = Cgsizemake (280, 180);
Uifont *fonts = [Uifont systemfontofsize:14.0];
Cgsize Msgsie = [text sizewithfont:fonts constrainedtosize:size linebreakmode:nslinebreakbycharwrapping];
Uilabel *textlabel = [[Uilabel alloc] init];
[Textlabel Setfont:[uifont boldsystemfontofsize:14]];
Textlabel.frame = CGRectMake (20,70, 280,msgsie.height);
Textlabel.text = text;
Textlabel.linebreakmode = nslinebreakbycharwrapping;//Implement multiple lines of text display
textlabel.numberoflines = 0;
[Self.view Addsubview:textlabel];
Set the border weight and color of the label, and add #import<quartzcore/quartzcore.h>
label.layer.borderColor = [uicolor to the corresponding file before setting. Lightgraycolor]. cgcolor;//border color, to cgcolor
label.layer.borderWidth = 1;//Border Width
Set the background color of a label
Label.backgroundcolor =[uicolor Yellowcolor];
Set the label background image
There are two ways to set up a background image, and the first method is described below:
Set a background image to place a picture of the same size as a label on the back of the label, and then set the label's background to Transparent so that the label has a background
Uilabel * label = [[Uilabel alloc] Initwithframe:cgrectmake (+, M)];
Uiimageview *imageview =[[uiimageview Alloc]init];
Imageview.frame =cgrectmake (M, m);
UIImage *image=[uiimage imagenamed:@ "1.jpg"];
Imageview.image =image;//imageview will change the size of the added picture according to its size so there is no need to set an additional image
Label.backgroundcolor = [Uicolor Clearcolor] ;
Label.text =@ "Hello World";
Label.font = [Uifont systemfontofsize:30];
Label.textcolor = [Uicolor yellowcolor];
[Self.view addsubview:imageview];//cannot be added in the wrong order, otherwise the picture will overwrite the label
This is a somewhat unorthodox method, the following to introduce a more standardized second method: set the picture with Uicolor, and then the Uicolor as background color, you can implement the label set background image
Uicolor * color = [Uicolor colorwithpatternimage:image];//image for background map to be added
uilabel * label = [[Uilabel alloc] Initwithf Rame:cgrectmake (M, M, MB)];
[Label Setbackgroundcolor:color];
[Self.view Addsubview:label];
But this method has a serious flaw, that is, when the size of the background picture is inconsistent with the size of the label, there will be partial interception of the background image or tile duplication of the situation, so the more perfect method is to first modify the background image size and label size consistent and then set the background color. You can set the image size with the following function
-(UIImage *) Scaleimage: (UIImage *) img tosize: (cgsize) itemsize{uiimage
;
Create a bitmap context and set it as the context
Uigraphicsbeginimagecontext (itemsize) that is currently in use;
CGRect imagerect=cgrectmake (0, 0, itemsize.width, itemsize.height);
Draw a Resize picture
[img Drawinrect:imagerect];
Create a resized picture from the current context
i=uigraphicsgetimagefromcurrentimagecontext ();
Make the current context out stack
uigraphicsendimagecontext ();
Returns the new resize picture return
i;
}
Then you can call it in the main function.
Cgsize size= Cgsizemake (MB);
UIImage *image =[uiimage imagenamed:@ "1.jpg"];
UIImage *laterimage =[self scaleimage:image tosize:size];
Uicolor * color = [Uicolor colorwithpatternimage:laterimage];
Uilabel * label = [[Uilabel alloc] Initwithframe:cgrectmake (n, M, MB)];
[Label Setbackgroundcolor:color];
[Self.view Addsubview:label];
Setting highlighting
Set Text Shadow
Label.shadowcolor =[uicolor Graycolor];
Set Shadow Size
Label.shadowoffset = Cgsizemake (2.0, 2.0);
Set Label fillet
Label.layer.cornerRadius = 10;
If you use this setting, first add #import<quartzcore/quartzcore.h> to the header file.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.