UIButton의 텍스트 정렬 센터를 만드는 방법은 무엇입니까? IB 사용
IB를 중심으로 사용하여 UIButton의 제목을 설정할 수 없습니다. 내 제목은 여러 줄입니다.
이렇게해서
하지만 난 이걸 좋아해
나는 이것에 공간을 주었지만 그렇게하고 싶지 않습니다. 어떤 경우에는 정확하게 정렬되지 않고 정렬을 설정하는 UILabel 속성이 있지만 그 코드를 작성하고 싶지 않습니다. IB에서 모든 것을 설정하고 싶습니다.
감사
이것은 정확히 당신이 기대했던 것을 만들 것입니다 :
목표 -C :
[myButton.titleLabel setTextAlignment:UITextAlignmentCenter];
iOS 6 이상의 경우
[myButton.titleLabel setTextAlignment: NSTextAlignmentCenter];
tyler53의 답변에 설명 된 대로
빠른:
myButton.titleLabel?.textAlignment = NSTextAlignment.Center
라인을 사용하십시오 :
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
내용을 가로로 배치해야합니다.
레이블 내부의 텍스트를 가운데로 설정하려면 다음을 사용하십시오.
[labelOne setTextAlignment:UITextAlignmentCenter];
IB를 사용하려면 XCode 4에 링크 된 작은 예제가 있지만 세부 정보를 충분히 제공해야합니다 (또한 속성 화면 위에 속성 탭이 표시됩니다. XCode에서 동일한 탭을 찾을 수 있습니다) 3.x) :
해결책 1
스토리 보드에서 키 경로를 설정할 수 있습니다
텍스트를 여러 줄 제목으로 설정하십시오 (예 : hello
⌥ +↵ multiline
텍스트를 다음 줄로 이동 하려면 ⌥ + 를 눌러야 ↵합니다.
그런 다음 키 경로를 추가하십시오.
titleLabel.textAlignment
같은 Number
가치 1
, 1
수단 NSTextAlignmentCenter
titleLabel.numberOfLines
등의 Number
가치는 0
, 0
라인의 수를 의미
이것은 IB / Xcode에 반영되지 않지만 런타임 (장치 / 시뮬레이터) 중앙에 있습니다.
Xcode의 변경 사항을 보려면 다음을 수행해야합니다. (이 단계를 건너 뛸 수 있음을 기억하십시오)
버튼을 디자인 할 수 있도록 UIButton을 서브 클래 싱하십시오.
import UIKit @IBDesignable class UIDesignableButton: UIButton {}
Assign this designable subclass to the buttons you're modifying:
- Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):
Solution2
If you want to write the code, then do the long process
1.Create IBOutlet
for button
2.Write code in viewDidLoad
btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0
Solution3
In newer version of xcode (mine is xcode 6.1) we have property attributed title
Select Attributed
then select the text and press centre option below
P.S. The text was not coming multiline for that I have to set the
btn.titleLabel.numberOfLines = 0
For UIButton you should use:-
[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
For ios 8 and Swift
btn.titleLabel.textAlignment = NSTextAlignment.Center
or
btn.titleLabel.textAlignment = .Center
For those of you who are now using iOS 6 or higher, UITextAlignmentCenter has been deprecated. It is now NSTextAlignmentCenter
EXAMPLE: mylabel.textAlignment = NSTextAlignmentCenter;
Works perfectly.
For swift 4, xcode 9
myButton.contentHorizontalAlignment = .center
Assuming that btn refers to a UIButton, to change a multi-line caption to be centered horizontally, you can use the following statement in iOS 6 or later:
self.btn.titleLabel.textAlignment = NSTextAlignmentCenter;
UITextAlignmentCenter
is deprecated in iOS6
Instead you can use this code:
btn.titleLabel.textAlignment=NSTextAlinmentCenter;
Actually you can do it in interface builder.
You should set Title to "Attributed" and then choose center alignment.
For Swift 3.0
btn.titleLabel?.textAlignment = .center
Try Like this :
yourButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
yourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
For Swift 4:
@IBAction func myButton(sender: AnyObject) {
sender.titleLabel??.textAlignment = NSTextAlignment.center
sender.setTitle("Some centered String", for:UIControlState.normal)
}
UIButton will not support setTextAlignment. So You need to go with setContentHorizontalAlignment for button text alignment
For your reference
[buttonName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
You can do this from storyboard. Select your button. Set Line Break 'Word Wrap', Set your title 'Plain' to 'Attributed'. Select 'Center alignment'. This part is important => Click ...(More) Button. And select line breaking mode to 'Character Wrap'.
참고 URL : https://stackoverflow.com/questions/5712937/how-to-make-uibuttons-text-alignment-center-using-ib
'IT story' 카테고리의 다른 글
파이썬이 설치된 곳을 찾으십시오 (기본 디렉토리가 아닌 경우) (0) | 2020.06.27 |
---|---|
“android.app.Application을 전송할 수 없습니다”로 사용자 정의 전역 응용 프로그램 클래스가 중단됨 (0) | 2020.06.27 |
C ++에서 atan과 atan2의 차이점은 무엇입니까? (0) | 2020.06.27 |
Javascript로 쿼리 문자열을 작성하는 방법 (0) | 2020.06.27 |
Ruby 또는 Rails를 사용하여 URL에서 URL 매개 변수를 추출하는 방법은 무엇입니까? (0) | 2020.06.27 |