iphone - 知道当UILabel的多行属性打开时在哪里拆分字符串

标签 iphone ios uibutton uilabel

有问题,需要为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/

相关文章:

ios - 如何打印包含媒体和多部分表单数据的数据

android - 通过iOS或Android上的“添加到主屏幕”添加的Web应用程序的命名约定?

ios - 将事件从 UIButton 传递到 TabBarItem

ios - 如何使用 UIButton 清除 UITextView

iphone - 我可以只向某些人分发我的 iPhone 应用程序吗?

iphone - 如何确定用户是否按下/选择了 uitextfield?

iphone - 如何使用 TWRequest 将地理定位附加到推文

ios - Objective-C - 即使应用了 sqlite3_close(),sqlite3_open 内存泄漏

iphone - 将 BarButtonItem 添加到 Popover 中的导航 Controller

ios - 用户界面按钮 |即使在主线程中,setTitle 也会花费大量时间