UIButton、UILabel、UITextField 初學者需要瞭解的基本定義和常用設定,uibuttonuilabel

來源:互聯網
上載者:User

UIButton、UILabel、UITextField 初學者需要瞭解的基本定義和常用設定,uibuttonuilabel

    以下是三個IOS開發中最常用的控制項,作為IOS基礎學習教程知識 ,初學者需要瞭解其基本定義和常用設定,以便在開發在熟練運用。 

     UIButton按鈕

  第一、UIButton的定義

  UIButton *button=[[UIButton buttonWithType:(UIButtonType);

  能夠定義的button類型有以下6種,

  typedef enum {

  UIButtonTypeCustom = 0,  自訂風格

  UIButtonTypeRoundedRect,  圓角矩形

  UIButtonTypeDetailDisclosure,  藍色小箭頭按鈕,主要做詳細說明用

  UIButtonTypeInfoLight,  亮色驚嘆號

  UIButtonTypeInfoDark,  暗色驚嘆號

  UIButtonTypeContactAdd,  十字加號按鈕

  } UIButtonType;

  第二、設定frame

  button1.frame = CGRectMake(20, 20, 280, 40);

  [button setFrame:CGRectMake(20,20,50,50)];

  第三、button背景色

  button1.backgroundColor = [UIColor clearColor];

  [button setBackgroundColor:[UIColor blueColor]];

  第四、state狀態

  forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現

  enum {

  UIControlStateNormal = 0, 常規狀態顯現

  UIControlStateHighlighted = 1 << 0, 高亮狀態顯現

  UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現

  UIControlStateSelected = 1 << 2, 選中狀態

  UIControlStateApplication = 0x00FF0000, 當應用程式標誌時

  UIControlStateReserved = 0xFF000000 為內部架構預留,可以不管

  };

  @property(nonatomic,getter=isEnabled)BOOL enabled;   // default is YES. if NO, ignores touch events and subclasses may draw differently

  @property(nonatomic,getter=isSelected)BOOL selected;  // default is NO may be used by some subclasses or by application

  @property(nonatomic,getter=isHighlighted)BOOL highlighted;

  第五 、設定button填充圖片和背景圖片

  [buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

  [buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

  第六、設定button標題和標題顏色

  [button1 setTitle: @"點擊" forState:UIControlStateNormal];

  [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

  第七、設定按鈕按下會發光

  button.showsTouchWhenHighlighted=NO;

  第八、添加或刪除事件處理

  [button1 addTarget:self action: @selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

  [btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

  第九、 設定按鈕內部圖片間距和標題間距

  UIEdgeInsets insets; // 設定按鈕內部圖片間距

  insets.top = insets.bottom = insets.right = insets.left = 10;

  bt.contentEdgeInsets = insets;

  bt.titleEdgeInsets = insets; // 標題間距

    第十、 其他

    // 設定按鈕為無效按鈕,如果按鈕無效了,按鈕就不再響應使用者了

     btn.enabled = YES;

     // 給按鈕添加手勢辨識器

     [btn addGestureRecognizer:tap];

    // 添加一個按鈕 ,樣本

    UIButton *calBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 200, 200, 40)];  // 按鈕大小

    calBtn.backgroundColor = [UIColor orangeColor];                  // 背景顏色

    [calBtn setTitle:@"點我,我就計算" forState:UIControlStateNormal];            // 設定預設狀態下的文字

  [calBtn setTitle:@"點我,我就計算" forState:UIControlStateHighlighted];    // 設定高亮狀態下的文字   

    [calBtn setBackgroundImage:[UIImage imageNamed:@"login_btn_n_Normal"] forState:UIControlStateNormal]; // 設定預設狀態下的背景圖片

    [calBtn setBackgroundImage:[UIImage imageNamed:@"logoff_btn_n_Highlighted"] forState:UIControlStateHighlighted];   // 設定高亮狀態下的背景圖片

    [self.view addSubview:calBtn];  // 最會一定要添加按鈕

 【注】圖片的名稱要提前修改好,最好在後面加上分辨是預設狀態還是高亮狀態的單詞

 

  UILabel標籤

   UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 300, 160)];  // 大小

    lbl.backgroundColor = [UIColor lightGrayColor]; // 背景顏色

    lbl.textColor = [UIColor blueColor];     // 字型顏色

    // lbl.shadowColor = [UIColor redColor];      // 陰影製作效果,不常用

    // lbl.shadowOffset = CGSizeMake(4, -10);

    lbl.text = @"宿舍的";    // 添加文字

    // 標籤內容對齊

    lbl.textAlignment = NSTextAlignmentCenter;

    // 設定標籤的行數,如果設定為0,表示可以有任意多行

    lbl.numberOfLines = 2;

    // 當標籤有多行時,設定換行方式 ,預設的是以單詞為單位

    lbl.lineBreakMode = NSLineBreakByTruncatingMiddle;  // 如果不能完全顯示,中間會有三個小點

    // 設定標籤高亮狀態

    lbl.highlighted = YES;

    // 設定標籤高亮時字型顏色

    lbl.highlightedTextColor = [UIColor purpleColor];

    // 允許使用者可以與標籤進行互動

    lbl.userInteractionEnabled = YES;       //允許使用者互動

    // 定義一個點選手勢辨識器對象

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lblClicked:)];

    // 在標籤上添加一個手勢辨識器

    [lbl addGestureRecognizer:tap];

    //  lbl.enabled = NO;

    lbl.adjustsFontSizeToFitWidth = YES;

     // lbl.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

    [self.view addSubview:lbl]; // 控制項最後都需要添加

【小結】下面的大家可以試著用一下,

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.