iOS 에서 UIButton 의 3 대 UIEdgeInsets 속성 용법 을 자세히 설명 합 니 다.

4576 단어 iOSUIButton
UIEdgeInsets 가 뭐 예요?
UIEdgeInsets 가 뭐 예요?우리 눌 러 서 들 어가 보 자.

typedef struct UIEdgeInsets {
  CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;
UIEdgeInsets 는 구조 체 형식 입 니 다.안에 네 개의 인자 가 있 는데 그것 이 바로 top,left,bottom,right 이다.이 네 개의 매개 변 수 는 위의 경계,왼쪽 경계,아래 경계,오른쪽 경계 와 의 거 리 를 나타 낸다.
UIEdgeInsets 속성 세 개
여러분 이 발견 하 셨 는 지 모 르 겠 지만 UIButton 에는 세 개의 UIEdgeInsets 속성 이 있 습 니 다.각각:

@property(nonatomic)     UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero

@property(nonatomic)     UIEdgeInsets titleEdgeInsets;        // default is UIEdgeInsetsZero

@property(nonatomic)     UIEdgeInsets imageEdgeInsets;        // default is UIEdgeInsetsZero

contentEdgeInsets 뒤에 UI 가 있어 요.APPEARANCE_SELECTOR 가 무슨 뜻 이 죠?
알림:UIAPPEARANCE_SELECTOR 에 표 시 된 속성 은 외관 에이 전 트 를 통 해 맞 춤 형 제작 을 지원 합 니 다.
예 를 들 어 UIButton 의 contentEdgeInsets 속성 을 설정 하면 바로 호출 할 수 있 습 니 다.

[[UIButton appearance] setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

UIButton 만 들 기:

UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(50, 200, 200, 50);
[button setTitle:@"  UIButton" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
button.titleLabel.textAlignment = NSTextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.view addSubview:button];
관찰 할 수 있 도록 button 의 title 을 왼쪽 에 두 는 button 을 만 듭 니 다.
2016425143210943.png (146×97)
UIButton 의 contentEdgeInsets 속성

@property(nonatomic)     UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; 

// default is UIEdgeInsetsZero

contentEdgeInsets 에는 UIButton 의 title 을 가리 키 는 콘 텐 츠 가 있 습 니 다.
매개 변수 의미:
위 에서 말씀 드 렸 듯 이 UIEdgeInsets 는 구조 체 유형 입 니 다.안에 네 개의 인자 가 있 는데 그것 이 바로 top,left,bottom,right 이다.이 네 개의 매개 변 수 는 위의 경계,왼쪽 경계,아래 경계,오른쪽 경계 와 의 거 리 를 나타 낸다.
이 네 개의 매개 변수의 값 은 플러스 일 수도 있 고 마이너스 일 수도 있다.왼쪽 예 를 들 면:

left = 10; //          ,    10   
left = -10; //          ,    10   
픽 셀 오른쪽으로 20 개 이동

button.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
픽 셀 20 개,left=20 을 오른쪽으로 이동 하면 됩 니 다.
2016425143256442.png (154×80)
픽 셀 을 왼쪽으로 20 개 이동

button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
2016425143338999.png (146×80)
UIButton 의 titleEdgeInsets 속성
title EdgeInsets 와 contentEdgeInsets 의 역할 차이 가 많 지 않 습 니 다.콘 텐 츠 EdgeInsets 를 설정 하고 titleEdgeInsets 를 설정 하면 어떻게 될까요?

button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
button.contentEdgeInsets = UIEdgeInsetsMake(0, 20 , 0, 0);
효과 보기:
2016425143400935.png (152×97)
UIButton 의 imageEdgeInsets 속성
사진 이 있 는 button 만 들 기:

UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(50, 200, 200, 200);
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
[button setImage:[UIImage imageNamed:@"test"] forState:UIControlStateNormal];
[self.view addSubview:button];
실행 하기:
2016425143505909.png (169×161)
픽 셀 50 개 오른쪽으로 이동

button.imageEdgeInsets = UIEdgeInsetsMake(0, 50, 0, 0);
효과 보기:
2016425143526865.png (170×190)
픽 셀 50 개 왼쪽으로 이동

button.imageEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
효과 보기:
2016425143547866.png (181×178)
여러분 은 스스로 다른 세 개의 파 라 메 터 를 설정 하여 효과 가 어떤 지 보고 스스로 손 을 써 서 이해 하기 편리 합 니 다.

좋은 웹페이지 즐겨찾기