IT story

UIButton의 텍스트 정렬 센터를 만드는 방법은 무엇입니까?

hot-time 2020. 6. 27. 10:54
반응형

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의 변경 사항을 보려면 다음을 수행해야합니다. (이 단계를 건너 뛸 수 있음을 기억하십시오)

  1. 버튼을 디자인 할 수 있도록 UIButton을 서브 클래 싱하십시오.

    import UIKit
    @IBDesignable class UIDesignableButton: UIButton {}
    

  2. Assign this designable subclass to the buttons you're modifying:

Interface Builder를 사용하여 버튼의 클래스를 변경하는 방법 표시

  1. Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):

Interface Builder에서 디자인 가능 및 기본 버튼 비교



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

반응형