Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 了解当UILabel的“多行”属性处于启用状态时,UILabel拆分字符串的位置_Iphone_Ios_Uibutton_Uilabel - Fatal编程技术网

Iphone 了解当UILabel的“多行”属性处于启用状态时,UILabel拆分字符串的位置

Iphone 了解当UILabel的“多行”属性处于启用状态时,UILabel拆分字符串的位置,iphone,ios,uibutton,uilabel,Iphone,Ios,Uibutton,Uilabel,出现问题,需要为UIButton的标题分配一些文本。已将按钮换行模式设置为NSLineBreakByCharWrapping,以便字符串仅由每行末尾的字符分隔。但我需要在行的末尾插入一个连字符,以显示单词的连续性。 这是我试过的- // Initialize the button titleButton.titleLabel.lineBreakMode = NSLineBreakByCharWrapping; titleButton.titleLabel.backgro

出现问题,需要为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];
//初始化按钮
titleButton.titleLabel.lineBreakMode=NSLineBreakByCharWrapping;
titleButton.titleLabel.backgroundColor=[UIColor yellowColor];
[titleButton.titleLabel setFont:[UIFont fontWithName:@“Helvetica Bold”大小:15];
//把绳子系在这里
NSMutableString*标题字符串=[[NSMutableString alloc]initwith字符串:@“abcdefghijklmnopqrs tuvwxyzabefghijklmnopqrstuvxyzabefghijklmnopqrstuvxyzabefghijklmnopqrstuvxyzabd”];
//插入连字符
int titleLength=[标题长度];
int hyphenIndex=19;

while(hyphenIndex您的第二个单词的长度大于行的长度(按钮的宽度),这就是为什么会发生这种情况。

粘贴连字符不是一个好主意。您应该在大小适合按钮宽度的部分拆分字符串,而不是在需要时添加连字符

[String sizeWithFont:font constrainedToSize:Size lineBreakMode:LineBreakMode]
其主要思想是获取初始字符串的一部分(如果单词断开,则添加连字符),检查其宽度是否适合按钮的宽度。如果宽度较小,则尝试较大的部分,如果适合,则尝试处理进一步的部分

它为处理字符串提供了很大的可能性

例如:

- (NSUInteger)lineBreakBeforeIndex:(NSUInteger)index withinRange:(NSRange)aRange
//Returns the appropriate line break when the character at the index won’t fit on the same line as the character at the beginning of the range.

有关如何使用NSParagraphStyle和NSAttributedString获取以连字符分隔的UILabel的信息,请参见我的回答:

这是我尝试过的一些事情,但没有得到准确的结果。错误大约为一个字符…它将为您提供一个包含
UILabel
行的数组。请浏览此链接,我怀疑这将有助于您解决此问题由于lineBreakBeforeIndex方法似乎仅适用于OS X,并且问题被标记为iOS,因此被否决。对于OS X用户来说,这里仍然有一个有用的答案:)