Guidelines for using Font and button controls in IOS Application UI development _ios

Source: Internet
Author: User
Tags extend

The use of Uilabel
First, initialization

Copy Code code as follows:

Uilabel *mylabel = [[Uilabel alloc] Initwithframe:cgrectmake (40, 40, 120, 44)];

[Self.view Addsubview:mylabel];

Second, set the text

①, setting default text

Copy Code code as follows:

NSString *text = @ "Label text";
MyLabel.Text = text;

Effect:

②, set label text (this property is iOS6.0 before it appears, if not necessary, do not recommend the use of this property)

Copy Code code as follows:

NSString *text = @ "actually nothing";

nsmutableattributedstring *attributestring = [[Nsmutableattributedstring alloc] initwithstring:text];

[AttributeString setattributes:@{nsforegroundcolorattributename: [Uicolor Redcolor], NSFontAttributeName: [UIFont SYSTEMFONTOFSIZE:17]} range:nsmakerange (2, 1)];

Mylabel.attributedtext = attributestring;

Effect:

The effect of keyword red

Copy Code code as follows:

NSString *keyword = @ "Script";
NSString *result = @ "cloud-dwelling community";

Set Label text
nsmutableattributedstring *attritutestring = [[Nsmutableattributedstring alloc] initwithstring:result];

Get the location and length of the red flag
Nsrange range = [result Rangeofstring:keyword];

Set properties for label text
[Attritutestring setattributes:@{nsforegroundcolorattributename: [Uicolor Redcolor], NSFontAttributeName: [UIFont SYSTEMFONTOFSIZE:17]} Range:range];

Show on label
Label.attributedtext = attritutestring;

③, set the font, if you are using the text in ②, then you set the AttributeString properties have been set font and TextColor, the text when you set the text directly using the ①, set the Fonts method
Copy Code code as follows:

Mylabel.font = [Uifont systemfontofsize:13];

④, setting color
Copy Code code as follows:

Mylabel.textcolor = [Uicolor Bluecolor];

⑤, setting alignment
Copy Code code as follows:

mylabel.textalignment = nstextalignmentcenter;//Center

Nstextalignmentleft//left-aligned
Nstextalignmentcenter//Center
Nstextalignmentright//Right alignment
nstextalignmentjustified//the last line of natural alignment
Nstextalignmentnatural//default alignment script


Nstextalignmentjustified and nstextalignmentnatural use of the time will be an error, the program crashes, temporarily do not know when to use, want to know the advice, grateful.

⑥, Text clipping mode

Copy Code code as follows:

Nslinebreakbywordwrapping = 0,//with a space boundary, preserving the word
Nslinebreakbycharwrapping,//Keep whole character
Nslinebreakbyclipping,//simple tailoring, to the border
Nslinebreakbytruncatinghead,//In accordance with the "... Text "Show
Nslinebreakbytruncatingtail,//According to the words ... Text "Show
Nslinebreakbytruncatingmiddle//In accordance with "text ..." Display

Mylabel.linebreakmode = Nslinebreakbytruncatinghead;


⑦, setting the label enabled property
If set to No, the text color is dimmed to indicate that it is not available and the default is yes.
Copy Code code as follows:

mylabel.enabled = NO;

third, match the text on the label
①, whether to change font size based on text width
Copy Code code as follows:

Mylabel.adjustsfontsizetofitwidth = YES;

Suppose the text content is @ "Once in the moonlight looking at fireworks, had a total of watching the sunset gradually lowered", the label length is 200, the line does not appear, if you set this property to Yes, it will reduce the font size to display the entire content.
Before and after comparison:

②, change the spacing between letters to fit the label size
When this property is yes, the label may change the letter spacing of the label text so that the text fits more within the bounds of the label. The string of this property, regardless of the cropping pattern of the row of the current row. The default value for this property is no.

Copy Code code as follows:

Mylabel.adjustsletterspacingtofitwidth = NO;

Personal use, did not find any difference, do not know exactly when to play a role.
③, set to baseline line
Copy Code code as follows:

Mylabel.adjustsfontsizetofitwidth = yes;//Adjust Baseline Position This property must be set to YES

Mylabel.baselineadjustment = Uibaselineadjustmentalignbaselines;


This property has three values to choose from
Copy Code code as follows:

Uibaselineadjustmentalignbaselines//text is aligned to the top of the label, and the default value
Uibaselineadjustmentaligncenters//Text centerline aligns with label centerline
Uibaselineadjustmentnone//text is aligned to the bottom of the label

④, minimum font size, when the font is less than this minimum value, display this property value
Before iOS6.0: minimumfontsize

After iOS6.0: Minimumscalefactor

Mylabel.minimumscalefactor = 10.0;//Default value is 0, the current font size
⑤, number of lines

Copy Code code as follows:

Mylabel.numberoflines = 2;//label number of rows

⑥, highlighting
Copy Code code as follows:

mylabel.highlighted = yes;//is highlighted
Mylabel.highlightedtextcolor = [Uicolor redcolor];//highlight color; This property when you set the Titlelabel of a button, whether the highlighted is yes or no, The caption displays this highlight color when the button is pressed

⑦, Shadow
Copy Code code as follows:

Mylabel.shadowcolor = [Uicolor graycolor];//shadow color, default to nil
Mylabel.shadowoffset = Cgsizemake (1, 1);//The offset point of the shadow

Four, label location
①, calculating the height of a uilabel with multiple lines of font
Copy Code code as follows:

CGRect result,bounds;
bounds = CGRectMake (0, 0,200, 300);
Heightlabel = [MyLabel textrectforbounds:bounds limitedtonumberoflines:20];//calculates the frame of the label after 20 lines
NSLog (@ "%f", heightLabel.size.height);

②, drawing text to the specified range
Copy Code code as follows:

-(void) Drawtextinrect: (cgrect) rect
This method needs to be overloaded and then called by subclasses, and when overridden, call super can be drawn by default graphic properties, and if you completely rewrite the drawing function, you don't have to call super.

Simple use of UIButton
a button is one of the most frequently used controls, and UIButton is not difficult to use, but one thing to be aware of

The UIButton default type is the rounded Rect button, which is not accepted by the App Store, even if you add a background image to the button, no longer the appearance of rounded Rect, or be rejected by the App Store, In short, the type of button cannot be rounded Rect.
first, the appearance of UIButton

There are 6 types of 1.1 UIButton, as shown in the following figure

Copy Code code as follows:

Uibuttontypecustom
Uibuttontyperoundedrect
Uibuttontypedetaildisclosure
Uibuttontypeinfolight
Uibuttontypeinfodark
Uibuttontypecontactadd

1.2 Setting ButtonType

Copy Code code as follows:

UIButton *button = [[UIButton alloc] Initwithframe:cgrectmake (10, 10, 100, 44)];

[UIButton Buttonwithtype:uibuttontypecustom];

second, set the background picture

2.1 can set the normal state, not clickable state, selected state and other forms of the background picture

Copy Code code as follows:

Picture in the normal state
[Button setbackgroundimage:[uiimage imagenamed:@ "Normal.png"] forstate:uicontrolstatenormal];

After clicking the picture
[Button setbackgroundimage:[uiimage imagenamed:@ "Pressed.png"] forstate:uicontrolstateselected];

2.2 Stretches of picture, there is a picture of 26*46, but the button is 100*46, then the picture will be extended to the effect will be very good, so that the project can make the picture file is relatively small, thin for the project, QQ session bubble is also the use of this method to reach the effect of extension, first on the contrast chart

Copy Code code as follows:

UIImage *buttonnormal = [uiimage imagenamed:@ "Button-white-part"];

To extend the width, if you want to extend the height, change the value of the first and third parameters, this method is to ensure that the edge of the picture frame in the middle of the picture to the height or width of the extension
UIImage *stretchnormal = [Buttonnormal resizableimagewithcapinsets:uiedgeinsetsmake (0, 15, 0, 30)];

[Button Setbackgroundimage:stretchnormal Forstate:uicontrolstatenormal];

Third, button events

Button events There are many more, you can view the document itself

Copy Code code as follows:

[Button addtarget:self action: @selector (buttonpress:) forcontrolevents:uicontroleventtouchupinside];

Click event
-(Ibaction) ButtonPress: (ID) Sender {
NSLog ("@" button pressed!);
}

Above is the code Add.
Four, xib add

4.1 Drag a button in view to put the canvas

4.2 Setting the Button property

4.3 Creating mappings, and Button events
Select Xib and click on the Xcode upper right editor

, so that one interface is xib, and the other interface is its corresponding. h file.

Select the button to drag and drop a line to the. h location between @interface and @end, select Iboutlet if you want to create a map, create a button event to select Action, and then click Connet to complete

The Click event created in the. m file appears, and the corresponding action is done in the method.

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.