想知道如何在自上而下的 UIButton 标题标签中设置文本。
上面的文字是“按我”
想展示
"
p
r
e
s
s
m
e
"
我做了这个
CGAffineTransform newTransform = CGAffineTransformMakeRotation(90 * (M_PI / 180));
self.activeButton.transform = newTransform;
但它只改变了按钮方向而不是文本
将文本垂直旋转不同于将每个字母写在单独的行上,这是我从您的问题中收集到的您的意图。
为此,您实际上需要将每个字母写在单独的行上!您可以在 UIButton 文本中创建新行,方法是在 Interface Builder 的文本字段中按 Enter 的同时按住 Alt/Option。请注意,这必须是实用程序面板中的文本字段属性,如果您通过双击按钮来编辑文本,则无法添加新行。
完成此操作后,将“换行”模式更改为 Character Wrap 或 Word Wrap 以显示多行。
编辑:我意识到您可能正在尝试在代码中使用您的按钮,所以我写了这篇文章,应该将常规按钮的文本转换为垂直间隔,一个字母一个字母:
// 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];