0

UIButtonタイトルラベルのテキストを上から下に設定する方法を知りたいと思います。

テキストは「PressMe」です

見せたい

"
p
r
e
s
s

m
e
"

これは私がしました

CGAffineTransform newTransform = CGAffineTransformMakeRotation(90 * (M_PI / 180));
    self.activeButton.transform = newTransform;

ただし、テキストではなくボタンの方向を変更しただけです

4

1 に答える 1

3

テキストを縦に回転させることは、各文字を別々の行に書くこととは異なります。これは、私があなたの質問からあなたの意図として集めたものです。

そのためには、実際に各文字を別々の行に書く必要があります!Alt / Optionを押しながらInterfaceBuilderのテキストフィールドでEnterキーを押すと、UIButtonテキストに新しい行を作成できます。これは[ユーティリティ]パネルのテキストフィールドプロパティである必要があることに注意してください。ボタンをダブルクリックしてテキストを編集している場合、新しい行を追加することはできません。

これを行ったら、「改行」モードを「文字の折り返し」または「ワードラップ」に変更して、複数の行を表示できるようにします。

編集:コードでボタンを操作しようとしている可能性があることに気付いたので、通常のボタンのテキストを文字ごとに垂直方向に間隔を空けるように変換するこの部分を作成しました。

// Create a temporary NSString to store the new formatted text string
// Set it to the first character so we can have a simple loop from the second to the last character
NSString *newText = [NSString stringWithFormat:@"%C",[button.titleLabel.text characterAtIndex:0]];
for(int i=1;i<button.titleLabel.text.length;i++) {
    // Format newText to include a newline and then the next character of the original string
    newText = [NSString stringWithFormat:@"%@\n%C",newText,[button.titleLabel.text characterAtIndex:i]];
}
// We must change the word wrap mode of the button in order for text to display across multiple lines.
button.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
// .. and for an unknown reason, the text alignment needs to be reset. Replace this if you use something other than center alignment.
button.titleLabel.textAlignment = NSTextAlignmentCenter;
// newText now contains the properly formatted text string, so we can set this as the button label
[button setTitle:newText forState:UIControlStateNormal];
于 2012-10-18T08:12:56.147 に答える