Search code examples
ioscore-plotnsattributedstring

CPTTextLayer - multiple lines NSAttributedString


I am trying to use NSAttributedString in CPTAnnotation with line break. When using the code below, only the "firstString..." is displayed. When removing the \n sign, both strings are properly displayed (sizes and colors proper), but in one line.

Is it possible to use something similar to label's numberOfLines=0 (enables line breaking) on CPTTextLayer used in annotation?

    NSMutableAttributedString* finalString = [firstAttrStr mutableCopy];
    NSMutableAttributedString *newLineAttrStr = [[NSMutableAttributedString alloc]initWithString:@"\n"];
    [finalLabel appendAttributedString:newLineAttrStr];
    [finalLabel appendAttributedString:secondAttrStr];

    //additional setup in here
    CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithAttributedText:finalLabel];

    textLayer.fill            = [CPTFill fillWithColor:[CPTColor whiteColor]];
    textLayer.cornerRadius    = 3.0;
    textLayer.paddingLeft     = 0.5;
    textLayer.paddingTop      = 0.5;
    textLayer.paddingRight    = 0.5;
    textLayer.paddingBottom   = 0.5;
    textLayer.hidden          = NO;

    //add shadow
    textLayer.masksToBounds = NO;
    textLayer.shadowOffset = CGSizeMake(0, 0);
    textLayer.shadowRadius = 5;
    textLayer.shadowOpacity = 0.3;

    _highlightAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:plotSpace anchorPlotPoint:@[middlePoint, yAnnotationPosition]];
    [_highLightPlot addAnnotation:_highlightAnnotation];

    CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithAttributedText:finalString];
    _highlightAnnotation.contentLayer = textLayer;

It looks like that:

enter image description here enter image description here


Solution

  • Thanks to suggestions of Eric, I solved it. The problem was with the font: Antenna. When using other fonts, annotation is displayed properly.