Line breaks do not work in UILabel

I load the help text from plist and display it as UILabels hosted in a UIScrollView. The following is the code sequence:

    UILabel *sectionDetailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(34, myOriginForThisSection, 286, 20)] autorelease];
    sectionDetailLabel.backgroundColor = [UIColor clearColor];
    sectionDetailLabel.numberOfLines = 0;
    sectionDetailLabel.font = [UIFont systemFontOfSize:12];
    sectionDetailLabel.textColor = [UIColor blackColor];
    sectionDetailLabel.textAlignment = UITextAlignmentLeft;
    sectionDetailLabel.lineBreakMode = UILineBreakModeWordWrap;

    [baseScrollView addSubview:sectionDetailLabel];

    [sectionDetailLabel setText:myStringForThisSection];
    [sectionDetailLabel sizeToFit];

code>

While any “long” text is wrapped correctly in several lines, I cannot manually insert any line breaks using the newline characters '\ n' in 'myStringForThisSection'. I see only the characters '\' and 'n' printed in UILabel, wherever I want a line break.

, , , , numberOfLines 0, LineBreakMode sizeToFit ( UILabel sizeWithFont:). , , , , - UILabel. , ?

. - baseScrollView, myStringForThisSection myOriginForThisSection - , .

+5
3

UILabel escape-\n. , / . char, .

unichar newLine = '\n';
NSString *singleCR = [NSString stringWithCharacters:&newLine length:1];
[myStringForThisSection insertString:singleCR atIndex:somePlaceIWantACR];

, myStringForThisSection , .

+8

XCode 4.3

:

unichar chr[1] = {'\n'};
NSString *cR = [NSString stringWithCharacters:(const unichar *)chr length:1];

- :

self.myLabel.text = [NSString stringWithFormat:@"First Label Line%@Second Label Line", cR];
+3

Scott DoctorG ( ), , , XML .

:

+(NSString)escapeXml:(NSString*)string {
    return [string stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
}
+1

All Articles