有问题,需要为UIButton的标题分配一些文本。将按钮的换行模式设置为NSLineBreakByCharWrapping,以使字符串仅由每行末尾的字符分隔。但是我需要在该行的末尾插入一个连字符以显示单词的连续性。
这是我尝试过的-
// Initialize the button
titleButton.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
titleButton.titleLabel.backgroundColor = [UIColor yellowColor];
[titleButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
// Make the string here
NSMutableString *titleString = [[NSMutableString alloc] initWithString:@"abcdefghijklmnopqrs tuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"];
// Insert hyphens
int titleLength = [titleString length];
int hyphenIndex = 19;
while (hyphenIndex<titleLength) {
UniChar charatIndex = [titleString characterAtIndex:hyphenIndex];
if ((charatIndex - ' ') != 0) { // Check if its not a break bw two words
[titleString insertString:@"-" atIndex:hyphenIndex]; // else insert an hyphen to indicate word continuity
}
hyphenIndex += 19; //Since exactly 20 char are shown in single line of the button's label.
}
//Set the hyphenated title for the button
[titleButton setTitle:titleString forState:UIControlStateNormal];
[titleString release];
这是我能得到的最接近的。
任何帮助将不胜感激。
最佳答案
您的第二个单词的长度大于行的长度(按钮的宽度),这就是这种情况的原因。
关于iphone - 知道当UILabel的多行属性打开时在哪里拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14233356/