JanR
JanR

Reputation: 121

CATextLayer is ignoring CTParagraphStyleSetting attributes

I'm trying to write a custom UITextView with the ability to highlight parts of text as search-results. To do this I'm putting my own CATextLayer in place of the normal text-layer and want to use NSMutableAttributedString to draw the text on the CATextLayer with highlighted parts of the text.

The problem is, that I can't set the Linespacing for the text. I can set attributes to the like font and fontcolor of the NSMutableAttributedString through the [attrString setAttributes:attributes range:range]; but all attributes in the CTParagraphStyleSetting are ignored.

- (void)redrawSelectableTextLayer
{
attrString = [[NSMutableAttributedString alloc] initWithString:self.text];

//Write the attributes of the attributed String
NSRange range = NSMakeRange(0, attrString.length);
NSMutableDictionary* attributes = [NSMutableDictionary dictionaryWithCapacity:3];

[attributes setObject:(id)[UIColor redColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName];

CTFontRef aFont = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL);
if(aFont){
    [attributes setObject:(id)aFont forKey:(NSString*)kCTFontAttributeName];
}
CTParagraphStyleSetting settings[1];
CGFloat linespace = 140.0;
CTLineBreakMode lbm = kCTLineBreakByCharWrapping;
settings[0].spec = kCTParagraphStyleSpecifierLineBreakMode;
settings[0].valueSize = sizeof(CTLineBreakMode);
settings[0].value = &lbm;
CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 1);
[attributes setObject:(id)style forKey:(NSString*)kCTParagraphStyleAttributeName];
CFRelease(style);

[attrString setAttributes:attributes range:range];

//Set current Size of view
CGFloat width = originalTextLayer.frame.size.width - 18.0;
CGFloat height = [attrString boundingHeightForWidth:width];
selectedTextLayer.frame = CGRectMake(9.0f, 11.0f, width, height);

//Draw the attributed String to the CATextLayer
selectedTextLayer.string = attrString;
}

Does the CATextLayer ignore these settings by default or is there something I'm doing wrong?

Upvotes: 1

Views: 803

Answers (1)

JanR
JanR

Reputation: 121

CATextLayer does not support CAParagraphStaleSetting attributes (I have no idea where to find a complete list of the supported attributes).

I've now written my custom CALayer implementation to get what I wanted.

Upvotes: 3

Related Questions