iOS界面編程-UIButton

一、介紹

   按鈕在界面編程中非常常見,從移動端到客戶端到pc端軟件開發。按鈕包含外表設置以及按鈕點擊事件以及點擊過程中按鈕狀態的改變。在ios中UIbutton 繼承自UICotrol類->UIView-UIResponder->NSObject,可以響應用戶點擊操作,我們還可以設置按鈕的標題、按鈕的背景圖片、按鈕的顏色、大小、外表等。

二、相關方法及屬性

創建按鈕

    + (instancetypenonnull)buttonWithType:(UIButtonType)buttonType

配置按鈕標題

1、titleLable  用來顯示一個按鈕的currentTitle屬性的值

@property(nonatomic, readonly, strong) UILabel *titleLabel

var titleLabel: UILabel? { get }  雖然是隻讀屬性,但是她自己的屬性卻是可以讀寫的

  • UIButton *button                  = [UIButton buttonWithType: UIButtonTypeSystem];
  • button.titleLabel.font            = [UIFont systemFontOfSize: 12];
  • button.titleLabel.lineBreakMode   = NSLineBreakByTruncatingTail;
  • let button = UIButton.buttonWithType(.System) as  a href="" UIButton /a 
  • button.titleLabel.font = UIFont.systemFontOfSize(12)
  • button.titleLabel.lineBreakMode = .ByTruncatingTail

不要使用label對象去設置文字顏色或者陰影顏色職能通過

- (void)setTitleColor:(UIColor * nullable)color forState:(UIControlState)state

- (void)setTitleShadowColor:(UIColor * nullable)color forState:(UIControlState)state

這兩個方法去設置不能通過titleLabel.text設置按鈕的文字,只能通過- (void)setTitle:(NSString * nullable)title forState:(UIControlState)state方法去設置

獲取相關屬性- (NSString * nullable)titleForState:(UIControlState)state

2、獲取及獲取相關狀態下的屬性

- (NSAttributedString * nullable)attributedTitleForState:(UIControlState)state

- (void)setAttributedTitle:(NSAttributedString * nullable)title forState:(UIControlState)state

- (UIColor * nullable)titleColorForState:(UIControlState)state 

- (UIColor * nullable)titleShadowColorForState:(UIControlState)state

3、配置按鈕的顯示

@property(nonatomic) BOOL adjustsImageWhenHighlighted 配置是否黨按鈕高亮時候是否更輕,默認YES

@property(nonatomic) BOOL adjustsImageWhenDisabled  如果button是disable狀態設置按鈕顏色更淡

- (UIImage * nullable)backgroundImageForState:(UIControlState)state 獲取相關狀態下的背景圖片

- (UIImage * nullable)imageForState:(UIControlState)state設

- (void)setBackgroundImage:(UIImage * nullable)image forState:(UIControlState)state

4、配置邊緣鑲嵌

@property(nonatomic) UIEdgeInsets contentEdgeInsets

@property(nonatomic) UIEdgeInsets titleEdgeInsets

@property(nonatomic) UIEdgeInsets imageEdgeInsets

5、獲取當前狀態

@property(nonatomic, readonly) UIButtonType buttonType

@property(nonatomic, readonly, strong) NSString *currentTitle

@property(nonatomic, readonly, strong) NSAttributedString *currentAttributedTitle

@property(nonatomic, readonly, strong) UIColor *currentTitleColor

@property(nonatomic, readonly, strong) UIColor *currentTitleShadowColor

6、獲取尺寸

- (CGRect)backgroundRectForBounds:(CGRect)bounds

- (CGRect)contentRectForBounds:(CGRect)bounds

- (CGRect)titleRectForContentRect:(CGRect)contentRect

- (CGRect)imageRectForContentRect:(CGRect)contentRect

7 數據類型

typedef enum {

   UIButtonTypeCustom = 0,

   UIButtonTypeSystem,

   UIButtonTypeDetailDisclosure,

   UIButtonTypeInfoLight,

   UIButtonTypeInfoDark,

   UIButtonTypeContactAdd,

   UIButtonTypeRoundedRect,

} UIButtonType;


三 、實際例子

 UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 128, self.view.bounds.size.width - 20, 44);
    backButton.titleLabel.font= [UIFont systemFontOfSize:30];
//    [backButton setBackgroundColor:[UIColor redColor]];
    [backButton setTitle:@"back" forState:UIControlStateNormal];
    backButton.titleLabel.text= @"overwrite";
//    backButton.reversesTitleShadowWhenHighlighted=YES;
    UIImage *blueBImage = [UIImage imageNamed:@"blueButton.png"];
    UIImage *stretchImage = [blueBImage resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [backButton setBackgroundImage:stretchImage
                        forState:UIControlStateNormal];
//    backButton.adjustsImageWhenHighlighted=NO;
//    backButton.adjustsImageWhenDisabled = YES;
    backButton.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 10);
    backButton.titleEdgeInsets = UIEdgeInsetsMake(40, 0, 0, 0);
    backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 0, 0);

    
    [backButton setTitleColor:[UIColor  blackColor] forState:UIControlStateNormal];
    [backButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton addTarget:self
                      action:@selector(backButtonClicked:)
            forControlEvents:UIControlEventTouchUpInside];
   CGRect test = [backButton titleRectForContentRect:backButton.bounds];
    
都是系統提供的方法和屬性 ,可以去試試









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章