1

我在标签的文本中有一个带有符号的两个单词的字符串,类型为“&”或“/”,如何将第一个带有符号的字符串放在第一行,第二个字符串放在第二行?

这是代码

if(self.labelTitle.text.length> self.labelTitle.bounds.size.width){
     self.labelTitle.numberOfLines =2;

       if([self.labelTitle.text rangeOfString:@"&"].location != NSNotFound){
            NSArray * separateString = [self.labelTitle.text componentsSeparatedByString:@"&"];
            NSString * firstString =[separateString objectAtIndex:0];
            NSString *secondString =[separateString objectAtIndex:1];
            self.labelTitle.text = firstString;
            self.labelTitle.text = secondString;

        }else if ([self.labelTitle.text rangeOfString:@"/"].location != NSNotFound){
            //.....
        }
  }

我哪里错了?

4

1 回答 1

3

在代码示例中,您firstString使用secondString. 您要做的是添加text一次设置属性并\n在字符串之间添加一个:

if(self.labelTitle.text.length> self.labelTitle.bounds.size.width){
     self.labelTitle.numberOfLines =2;

       if([self.labelTitle.text rangeOfString:@"&"].location != NSNotFound){
            NSArray * separateString = [self.labelTitle.text componentsSeparatedByString:@"&"];
            NSString * firstString =[separateString objectAtIndex:0];
            NSString *secondString =[separateString objectAtIndex:1];
            self.labelTitle.text = [NSString stringWithFormat:@"%@\n%@", firstString, secondString];

        }else if ([self.labelTitle.text rangeOfString:@"/"].location != NSNotFound){
            //.....
        }
  }
于 2012-10-24T08:58:14.170 回答