Uilabel and UIButton

Source: Internet
Author: User

First, UILabel1, UILabel: tags, mainly used to display text. Create steps: (1) Open space and initialize (if this class has an initialization method, use its own, otherwise, use the negative parent class). UILabel *textlabel = [[UILabel alloc]initwithframe:cgrectmake (130, 280, 100, 80)]; (2) Set the relevant properties of text control Textlabel.backgroundcolor = [uicolor redcolor];//background color textlabel.text = @ "Prompts you to confirm the purchase!";//Textlabel.textcolor = [uicolorgreencolor];//font Color

Textlabel.backgroundcolor = [Uicolor colorwithred:60/255.0 green:50/255.0 blue:300/255.0 alpha:1];

Textlabel.linebreakmode =nslinebreakbycharwrapping;
Textlabel.numberoflines = 0;

Textlabel.font = [Uifont systemfontofsize:25];//Setting the font size
Textlabel.textalignment = nstextalignmentcenter;//Sets the middle format of the string, and you can also set the format to be left-aligned or right-aligned (3) to the parent view [self.view Addsubview: Textlabel]; (4) Release the basic properties of UILabel first create a UILabel object UILabel *label1 = [[UILabel alloc]initwithframe:cgrectmake (130, 360, 100, 50)];
Label1.backgroundcolor = [Uicolor Bluecolor];

Alignment, the default is left-justified
Label1.textalignment = nstextalignmentright;//Right-justified
Label1.textalignment = nstextalignmentleft;//Left justified label1.textalignment = nstextalignmentcenter;//Center 1, Text Text Property Label1.Text = @ "hint";//Set content 2, textcolor text color label1.textcolor = [Uicolor yellowcolor];//Set text color 3, Font Font Property Label1.font = [Uifont systemfontofsize:20];//Setting the text size
    Label1.font = [uifont boldsystemfontofsize:20];//font plus rent     label1.font = [Uifont fontwithname:@ "Heleretica-blod" size:30];//custom font The first parameter represents the font name, the second parameter represents the font size  4, Linebreakmode Line break mode Label1.linebreakmode = Nslinebreakbycharwrapping;label1.numberoflines = 0;//line wrap setting, set to 0 automatically define how many lines, that is, wrap the line, If the specific number of rows is given, the specific number of rows is generated  5, shadow property Label1.shadowcolor = [Uicolor bluecolor];//set shadow color Label1.shadowoffset = Cgsizemake (2, 3);//  set Shadow  6, highlighted set highlight label1.highlighted = yes;//Highlight    Label1.highlightedtextcolor = [Uicolor orangecolor];//sets the color of the text when highlighting  7, Nslinebreakbytruncatingheadlabel1.linebreakmode = nslinebreakbytruncatinghead;//text is super long, the middle content is ... The method is omitted, showing the text content of the kinsoku.  8, nslinebreakbytruncatinghead//in front part of the text with ... Way omitted, display trailing text content  9, nslinebreakbytruncatingtail//end part text with ...  10,. adjustsfontsizetofitwidthlabel1.adjustsfontsizetofitwidth = yes;//Font size fits the label width, Fonts are automatically resized according to the label size       three, UIButton1, UIButton button control 2, create UIButton (1) Create UiBUtton object (if this class has an initialization method, use its own, otherwise, use the negative parent class). UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];  (2) The Settings button now displays the related properties Button.frame = CGRectMake (100,  [button settitle:@ "button" Forstate:uicontrolstatenormal]; [button settitle:@ "Check" ForState: Uicontrolstateselected]; [button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];//Set text color         Button.titleLabel.font = [uifont boldsystemfontofsize:30];//setting font size  [ Button settitle:@ "Highlight" forstate:uicontrolstatehighlighted];//set the highlight  //here to set the background picture [button setbackgroundimage:[ UIImage imagenamed:@ "" "forstate:uicontrolstatenormal];//set the background map, the picture automatically fills the button, this time the title will be on the background map   Button.imageedgeinsets = Uiedgeinsetsmake (-30, 0, 0, 0);//Set the offset of the picture
        button.titleedgeinsets = uiedgeinsetsmake (100,-160, 0, 0);// Set the offset of the caption button.imageedgeinsets = Uiedgeinsetsmake (-30, 0, 0, 0);//Set the image's offset     // The above offset is set to allow the picture and text to have a good visual effect, users can set their own  button.selected = no;//Settings button is selected         NSLog (@ "---%d", button.isselected);//Gets the selected state of the button (whether selected)     (3) Adds a Click event for the button [button addtarget:self action: @selector (buttonclicked) forcontrolevents:uicontroleventtouchupinside]; //How to implement a click event//Note here is a method defined-(void) buttonclicked{    NSLog (@ "button click event");    (4) Add a button to the view to display the [Self.view addsubview:button];  (5) button without releasing (because you are using a button created by the class method)  3, Basic properties of UIButton (1) [Button settitle:@ "buttons" forstate:uicontrolstatenormal];  (2) [Button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];//Set text color   (3) Button.titleLabel.font = [Uifont boldsystemfontofsize:30 ];//Set Font size   (4) [Button settitle:@ "Highlight" forstate:uicontrolstatehighlighted];//set highlight   (5) button. selected = no;//Set button is selected   4, create multiple uibutton  example: #import "ViewController.h"

@interface Viewcontroller () {
UILabel *_label;
}

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect];
Button.backgroundcolor = [Uicolor Orangecolor];
Button.frame = CGRectMake (170, 100, 50, 50);
[Button addtarget:self action: @selector (buttonClicked1:) forcontrolevents:uicontroleventtouchupinside];
Button.tag = 1;
[Self.view Addsubview:button];

UIButton *button1 = [UIButton buttonwithtype:uibuttontyperoundedrect];

Button1.backgroundcolor = [Uicolor Redcolor];
Button1.frame = CGRectMake (170, 180, 50, 50);
[Button1 addtarget:self Action: @selector (buttonClicked1:) forcontrolevents:uicontroleventtouchupinside];
Button1.tag = 2;
[Self.view Addsubview:button1];


_label = [[UILabel alloc]initwithframe:cgrectmake (190, 250, 50, 50)];

[Self.view Addsubview:_label]; }//Click implementation Method-(void) buttonClicked1: (UIButton *) btn{
_label.text= [NSString stringwithformat:@ "%zi", Btn.tag];
NSLog (@ "button%zi is clicked", Btn.tag);
Run Result: Example://Create object UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect];//Set Properties Button.backgroundcolor = [ Uicolor Graycolor];
Button.frame = CGRectMake (170, 300, 80, 80);    Assignment [button settitle:@] "Forstate:uicontrolstatenormal";     [Button settitle:@ "check" forstate:uicontrolstateselected];    button.selected = NO;    Add Event [button addtarget:self action: @selector (BUTTONCLICKED1) forcontrolevents:uicontroleventtouchupinside];    Add Picture [button Setbackgroundimage:[uiimage imagenamed:@] forstate:uicontrolstatenormal];    Set Picture Position button.imageedgeinsets = Uiedgeinsetsmake (30, 0,90, 0); Add to Parent view [Self.view Addsubview:button]; Ways to implement clicks
-(void) buttonclicked1{
NSLog (@ "button is clicked");
The result of the operation:

Uilabel and UIButton

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.