Skip to content

Instantly share code, notes, and snippets.

@vendruscolo
Last active December 18, 2015 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vendruscolo/5809924 to your computer and use it in GitHub Desktop.
Save vendruscolo/5809924 to your computer and use it in GitHub Desktop.
- (void)setTextAlignment:(NSTextAlignment)textAlignment {
NSTextAlignment oldTextAlignment = self.textAlignment;
[super setTextAlignment:textAlignment];
if (textAlignment != oldTextAlignment) {
// update link attributes
if (self.linkAttributes) {
NSMutableDictionary *mutableLinkAttributes = [self.linkAttributes mutableCopy];
NSMutableDictionary *mutableActiveLinkAttributes = [self.activeLinkAttributes mutableCopy];
if ([NSMutableParagraphStyle class]) {
NSMutableParagraphStyle *paragraphStyle = [[mutableLinkAttributes objectForKey:(NSString *)kCTParagraphStyleAttributeName] mutableCopy];
paragraphStyle.alignment = textAlignment;
[mutableLinkAttributes setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
[mutableActiveLinkAttributes setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
} else {
CTTextAlignment ctTextAlignment = CTTextAlignmentFromUITextAlignment(textAlignment);
CTParagraphStyleSetting paragraphStyles[1] = {
{.spec = kCTParagraphStyleSpecifierAlignment, .valueSize = sizeof(CTTextAlignment), .value = (const void *)&ctTextAlignment}
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphStyles, 1);
// am I overwriting old settings?
[mutableLinkAttributes setObject:(__bridge id)paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
[mutableLinkAttributes setObject:(__bridge id)paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
CFRelease(paragraphStyle);
}
self.linkAttributes = [NSDictionary dictionaryWithDictionary:mutableLinkAttributes];
self.activeLinkAttributes = [NSDictionary dictionaryWithDictionary:mutableActiveLinkAttributes];
}
[self setNeedsFramesetter];
[self setNeedsDisplay];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment