⑴ iOS的UI開發中Button的基本編寫方法解析
一、簡單說明
 
 一般情況下,點擊某個控制項後,會做出相應反應的都是按鈕
 
 按鈕的功能比較多,既能顯示文字,又能顯示圖片,還能隨時調整內部圖片和文字的位置
 
 二、按鈕的三種狀態
 
 normal(普通狀態)
 
 默認情況(Default)
 
 對應的枚舉常量:UIControlStateNormal
 
 highlighted(高亮狀態)
 
 按鈕被按下去的時候(手指還未松開)
 
 對應的枚舉常量:UIControlStateHighlighted
 
 disabled(失效狀態,不可用狀態)
 
 如果enabled屬性為NO,就是處於disable狀態,代表按鈕不可以被點擊
 
 對應的枚舉常量:UIControlStateDisabled
 
 三、注意點
 
 (1)從Xcode5開始,圖片資源都放到Images.xcassets中進行管理,可以使用拖拽的方式添加項目中用到的圖片到Images.xcassets中
 
 (2)若干多個控制項共用一段代碼,通常使用tag。
 
 四、代碼示例
 
 (1)
 
 復制代碼 代碼如下:
 
 #import "LFViewController.h"
 
 @interface LFViewController ()
 
 @property (weak, nonatomic) IBOutlet UIButton *headImageView;
 
 @end
 
 @implementation LFViewController
 
 // 在OC中,絕大多數的控制項的監聽方法的第一個參數就是控制項本身
 
 //- (IBAction)left:(UIButton *)button {
 
 //
 
 // NSLog(@"----");
 
 //}
 
 - (IBAction)move
 
 {
 
 // 通過frame修改head的位置
 
 // 在OC中,不允許直接修改「對象」的「結構體屬性」的「成員」
 
 // 允鍵塌許修改「對象」的'「結構體屬性」
 
 // 1. 取出結構體屬性
 
 CGRect rect = self.headImageView.frame;
 
 // 2. 修改結構體成員
 
 rect.origin.y -= 20;
 
 // 3. 設置對象的結構體屬性
 
 self.headImageView.frame = rect;
 
 }
 
 稿談圓(2)
 
 復制代碼 代碼如下:
 
 #import "LFViewController.h"
 
 使用git
 
 1. 創建項目時,勾選git
 
 2. 開發告一段落後,選擇"Source Control""Commit",並編寫注釋
 
 // 枚舉類型實侍早質上就是一個整數,作用就是用來替代魔法數字
 
 // 枚舉類型中,指定了第一個整數之後,後面的數字會遞增
 
 typedef enum
 
 kMovingDirTop = 10,
 
 kMovingDirBottom,
 
 kMovingDirLeft,
 
 kMovingDirRight,
 
 } kMovingDir;
 
 #define kMovingDelta 50
 
 @interface LFViewController ()
 
 @property (weak, nonatomic) IBOutlet UIButton *headImageView;
 
 @end
 
 @implementation LFViewController
 
 - (IBAction)move:(UIButton *)button
 
 // CGRect rect = self.headImageView.frame;
 
 CGPoint p = self.headImageView.center;
 
 // magic number魔法數字,其他程序員看到代碼的時候,不知道是什麼意思
 
 switch (button.tag) {
 
 case kMovingDirTop:
 
 p.y -= kMovingDelta;
 
 break;
 
 case kMovingDirBottom:
 
 p.y += kMovingDelta;
 
 break;
 
 case kMovingDirLeft:
 
 p.x -= kMovingDelta;
 
 break;
 
 case kMovingDirRight:
 
 p.x += kMovingDelta;
 
 break;
 
 [UIView beginAnimations:nil context:nil];
 
 [UIView setAnimationDuration:1.0];
 
 self.headImageView.center = p;
 
 [UIView commitAnimations];
 
 - (IBAction)zoom:(UIButton *)button
 
 CGRect rect = self.headImageView.bounds;
 
 // 在C語言中,關於bool的判斷:非零即真
 
 if (button.tag) {
 
 rect.size.width += 50;
 
 rect.size.height += 50;
 
 rect.size.width -= 50;
 
 rect.size.height -= 50;
 
 // 首尾動畫
 
 // beginAnimations表示此後的代碼要「參與到」動畫中
 
 [UIView beginAnimations:nil context:nil];
 
 [UIView setAnimationDuration:2.0];
 
 self.headImageView.bounds = rect;
 
 // self.headImageView.alpha = 0;
 
 // commitAnimations,將beginAnimation之後的所有動畫提交並生成動畫
 
 [UIView commitAnimations];
 
 @end
 
 五、補充筆記
 
 1. IBAction的參數
 
 - (IBAction)left:(UIButton *)button
 
 (1) 在OC中,絕大多數的控制項監聽方法的第一個參數就是控制項本身
 
 (2) 默認連線時的參數類型是id
 
 (3) 如果要在監聽方法中,方便控制項的使用,可以在連線時或者連線後,修改監聽方法的參數類型
 
 2. 修改對象的結構體成員
 
 在OC中,不允許直接修改「對象」的「結構體屬性」的「成員」,但是允許修改「對象」的「結構體屬性」
 
 修改結構體屬性的成員方法如下:
 
 (1)使用臨時變數記錄對象的結構體屬性
 
 (2) 修改臨時變數的屬性
 
 (3)將臨時變數重新設置給對象的結構體屬性
 
 3. 在程序開發中需要避免出現魔法數字(Magic Number)
 
 使用枚舉類型,可以避免在程序中出現魔法數字
 
 (1)枚舉類型實質上就是一個整數,其作用就是用來替代魔法數字
 
 (2)枚舉類型中,指定了第一個整數之後,後面的數字會遞增
 
 4. frame & bounds & center
 
 1> frame可以修改對象的位置和尺寸
 
 2> bounds可以修改對象的尺寸
 
 3> center可以修改對象的位置
 
 5. 首尾式動畫
 
 復制代碼 代碼如下:
 
 // beginAnimations表示此後的代碼要「參與到」動畫中
 
 [UIView beginAnimations:nil context:nil];
 
 // setAnimationDuration用來指定動畫持續時間
 
 [UIView setAnimationDuration:2.0];
 
 self.headImageView.bounds = rect;
 
 ......
 
 // commitAnimations,將beginAnimation之後的所有動畫提交並生成動畫
 
 [UIView commitAnimations];
 
 下面來羅列一下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; // 標題間距
⑵ iOS 調整UIButton 圖片(imageView)與文字(titleLabel)的位置
 UIButton可以同時設置Title和Image,UIButton有兩個屬性:titleEdgeInsets(top,left,bottom,right)和imageEdgeInsets(top,left,bottom,right),通過設置這兩個,就可以實現所有需要的Button的樣式
   UIButton 的 默認狀態下imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0);titleEdgeInsets = UIEdgeInsetsMake(0,0,0,0);  圖片在左文字在右,而且整體水平和垂直居中  。比如下面這個圖文按鈕:
為了最美觀一點,可以設置圖標與文字間距 。如下圖:
設置圖片在右文字在左:
設置圖片在上,文字在下:
設置圖片左對齊:
設置文字右對齊:
設置文字左對齊,圖片右對齊:
⑶ ios中怎麼設置uibutton上字體的大小
1.在IOS程序中設置UIButton的字體大小:
btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//設置按鈕上的自體的大小
//[btn setFont: [UIFont systemFontSize: 14.0]];    //這種可以用來設置字體的大小,但是可能會在將來的SDK版本中去除改方法
//應該使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
//最後將按鈕加入到指定視圖superView
[superView addSubview: btn];
附:創建按鈕的兩種方法:
1、動態創建
btnfont = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnfont setFrame:CGRectMake(100, 10, 120, 40)];
[btnfont addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[btnfont setTitle:@"字體" forState:UIControlStateNormal];
btnfont.backgroundColor=[UIColor clearColor];
[self.view addSubview:btnfont];
2、在xib文件中已經創建好,通過tag獲取按鈕
UIButton *testButton= (UIButton*)[self.view viewWithTag:100];
[testButton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
注冊事件
-(void) test: (id) sender{
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"ceshi" message:@"test11111" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease];
[av show];
}
2.設置UIButton的文字顯示位置、字體的大小、字體的顏色
btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//設置按鈕上的自體的大小
//[btn setFont: [UIFont systemFontSize: 14.0]];    //這種可以用來設置字體的大小,但是可能會在將來的SDK版本中去除改方法
//應該使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
//最後將按鈕加入到指定視圖superView
[superView addSubview: btn];
tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];
這樣初始化的button,文字默認顏色是白色的,所有如果背景也是白色的話,是看不到文字的,
btn.contentHorizontalAlignment= ;//設置文字位置,現設為居左,默認的是居中
[btn setTitle:@「title」forState:UIControlStateNormal];// 添加文字
有些時候我們想讓UIButton的title居左對齊,我們設置
btn.textLabel.textAlignment = UITextAlignmentLeft
是沒有作用的,我們需要設置
btn.contentHorizontalAlignment = ;
但是問題又出來,此時文字會緊貼到做邊框,我們可以設置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距離做邊框保持10個像素的距離。
設置UIButton上字體的顏色設置UIButton上字體的顏色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
⑷ ios 怎麼讓button中的文字緊靠下邊框
如果是xib上拖拽的,點擊button,右側第四個檢查器,type選擇custom;如果是代碼創族咐建的UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 如果button是文正攜字, [button setTitle:@"哈哈舉穗伏" forState:UIControlStateNormal]
⑸ iOS button 按鈕 設置漸變或者layer之後無法展示文字或者圖片
CAGradientLayer *gl = [CAGradientLayer layer];
  
  gl.frame = CGRectMake(0,0,100,24);
  
 gl.startPoint = CGPointMake(0, 0);
  
  毀坦粗 gl.endPoint = CGPointMake(1, 1);
  
   gl.colors = @[( __bridge   id )[UIColor colorWithRed:0/255.0 green:168/255.0 blue:79/255.0 alpha:1.0].CGColor,( __bridge   id )[UIColor colorWithRed:26/255.0 green:186/255.0 blue:74/255.0 alpha:1.0].CGColor];
  
 gl.locations = @[@(0.0),@(1.0f)];
  
  [_continueStudyBtn.layer addSublayer:gl];
  
 1.解決方法信搏。背景放在最底層纖鎮
  
    [_continueStudyBtn.layer insertSublayer:gl atIndex:0];
  
     [_continueStudyBtn bringSubviewToFront:_continueStudyBtn.imageView];
⑹ ios當button載入完成已後,怎麼改變button的文字
如果你用的是IB,選中按鈕在右側屬性界面進行設置:按鈕類型(Type)設置為自定義,BackgroundImage選擇你的圖片,Title內輸入你要顯示的文字 代碼實現的話如下: //按鈕初始化 UIButton *myButton =[UIButton buttonWithType:UIButtonTypeCustom]; (注意此處使用便利構造器初始化的button,不需release) //設置按鈕圖片 [myButton setBackgroundImage:[UIImage imageNamed:@"myPic.png"] forState:UIControlStateNormal]; //設置文字 [myButton setTitle:@"確定" forState:UIControlStateNormal];
⑺ iOS開發 UIButton的基本屬性
Button就是一個按鈕,就是手機界面中可以點擊的那種(點擊之後跳轉入另一個界面) 
  
 Button定義時有許多類型可以選擇
一般情況下使用custom屬於自定義型 
  
  1、定義一個Button按鈕  (UIButton *按鈕名;)     並且將其定義為自定義類型 
  
  2、button的大小  (按鈕名+.frame) 
  
  3、button顯示的字體名稱加狀態 
  
        (1)  一般使用這三種狀態  ([按鈕名 setTitle: forState:]) 
  
  參數一:設置按鈕標題(String類型) 
  
  參數二:設置按鈕顯示標題時所處的狀態(系統自帶) 
  
       (2)  這種定義標題的方式  不常用  (按鈕名+.titleLabel.text) 
  
  4、button的背景顏色  (按鈕名+.backgroundColor) 
  
   5、設置Button的背景圖片 
  
  ([按鈕名 setBackgroundImage:  forState:]) 
  
  參數一:添加的圖片 參數二:按鈕所處狀態 
  
  注意 
  
     背景圖像會根據按鈕的尺寸拉伸
  
     按鈕圖像會居中顯示在按鈕中央位置
  
     如果同時設置了按鈕的圖像和文字
  
     按鈕區域足夠大,會並列顯示圖像和文字
  
     如果區域不夠大,優先顯示圖像
  
 6、 設置Button的title顏色 
  
  ([按鈕名 setTitleColor: forState:]) 
  
  參數一:選擇顏色 參數二:按鈕所處的類型 
  
 7、 給Button添加點擊事件 
  
  ([按鈕名 addTarget: action:  forControlEvents:]) 
  
  參數  一:  對象  (self為自身)        
  
  參數  二:  @selector(點擊事件的方法名稱) 
  
  參數  三:  為事件出發的類型 例如touch觸碰,upinside點擊 
  
  下面是在ViewDidLoad外面重新定義的一個方法用於點擊了按鈕 
  
  點擊事件函數(當按鈕點擊時,運行函數) 
  
  一般頁面之間的跳轉,也在點擊函數中添加 
  
  8、顯示在當先視圖下 
                                                         首先是為normal下  
                                                                     高亮狀態下 
以上就是按鈕的一些最普通的屬性
  
 謝謝觀看!若是有錯誤,可以私信!謝謝
⑻ iOS開發 設置button的image的位置
開巧搜發過程中經常遇到通過imageEdgeInsets和titleEdgeInsets去孝野歷設置button中的image,title的位置,來達到想要的效果。但因為對其原理的不了解,經常碰壁,設置了卻達不到自己想要的效果。終於找到這篇文章,文章作者解析的很詳細,這里記錄一下,方便自己日後脊或查閱。
   1.常用的button樣式,圖片在上,文字在下
⑼ ios開發怎麼讓button上的image緊跟著字
button有一個屬圓逗性,用來設置圖片和文春橘字的偏移量的
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
根據你的需要自己調試到合適的位置即可。橘森賣
⑽ IOS UIButton 文字不全,被壓縮了。比如"P..y"
如果你沒有使用AutoLayout
你需要改變button.frame = CGRectMake(xx, xx, 能夠顯示全文字的合適寬度, 比字體大2.0f的高度)
如果你使用了AutoLayout
你需要改變button的width Constraint至能夠顯示全文字的合適寬度.