改變button的圖片和文字位置 UIEdgeInsetsMake(, , ,

UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)

這個一直搞不太懂,看文檔也沒看太明白,網上看了好多資料也都不是太滿意 ,今天自己着重研究了下


    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, 200, 200, 200)];

    [button setTitle:@"我是按鈕" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"refresh_02a.png"] forState:UIControlStateNormal];

    button.backgroundColor = [UIColor yellowColor];

    NSLog(@"%@,%@",button.titleLabel,button.imageView);

    button.titleEdgeInsets = UIEdgeInsetsMake(29, -22, -29, 22);

    button.imageEdgeInsets = UIEdgeInsetsMake(-15, 36, 15, -36);

     NSLog(@"%@,%@",button.titleLabel,button.imageView);

    [self.view addSubview:button];


這個是原碼

UIEdgeInsetsMake(給上部分加的距離>, <給左部分加的距離>, <給下部分加的距離>, <給右部分加的距離)

正數就是加負數就是減

因爲image和lable的寬高是不能變的   所以最好一邊加了相對方向就減相同的數

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