반응형
UIButton의 Label 제목에 대해 x,y 위치를 조정할 수 있습니까?
x,y 위치를 조정할 수 있습니까?titleLabel
상당한UIButton
?
내 코드는 다음과 같습니다.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[btn setTitle:[NSString stringWithFormat:@"Button %d", i+1] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.frame = ???
//make the buttons content appear in the top-left
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
//move text 10 pixels down and right
[button setTitleEdgeInsets:UIEdgeInsetsMake(10.0f, 10.0f, 0.0f, 0.0f)];
그리고 스위프트에서
//make the buttons content appear in the top-left
button.contentHorizontalAlignment = .Left
button.contentVerticalAlignment = .Top
//move text 10 pixels down and right
button.titleEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 0.0, 0.0)
스위프트 5
button.contentHorizontalAlignment = .left
button.contentVerticalAlignment = .top
button.titleEdgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 0.0, right: 0.0)
시각적으로 가장 쉬운 방법은 속성 검사기**(xib/storyboard를 편집할 때 표시됨)를 사용하여 "edge" 속성을 제목으로 설정하고, 삽입된 속성을 조정한 다음 "edge" 속성을 이미지로 설정하고, 그에 따라 조정하는 것입니다.일반적으로 코드화하는 것보다 더 낫습니다. 유지보수가 쉽고 시각적으로 잘 보이기 때문입니다.
UIButton에서 파생하여 다음 방법을 구현합니다.
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
편집:
@interface PositionTitleButton : UIButton
@property (nonatomic) CGPoint titleOrigin;
@end
@implementation PositionTextButton
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
contentRect.origin = titleOrigin;
return contentRect;
}
@end
내 프로젝트에는 다음과 같은 확장 유틸리티가 있습니다.
extension UIButton {
func moveTitle(horizontal hOffset: CGFloat, vertical vOffset: CGFloat) {
self.titleEdgeInsets.left += hOffset
self.titleEdgeInsets.top += vOffset
}
}
이것은 xib에서 할 수 있습니다.단추를 선택하고 "Size Inspector" 탭으로 이동한 후 세트를 설정합니다.
언급URL : https://stackoverflow.com/questions/2808078/is-it-possible-to-adjust-x-y-position-for-titlelabel-of-uibutton
반응형
'programing' 카테고리의 다른 글
우분투에서 R을 업그레이드하는 방법은? (0) | 2023.06.06 |
---|---|
Eclipse에서 빌딩 작업 공간 프로세스 사용 안 함 (0) | 2023.06.01 |
gem 이벤트 시스템 치명적 오류: 'sl/ssl.h' 파일을 찾을 수 없음 (0) | 2023.06.01 |
Android 에뮬레이터가 인터넷에 액세스할 수 없음 (0) | 2023.06.01 |
저장된 커밋되지 않은 변경 사항을 복구하는 방법 (0) | 2023.06.01 |