我正在使用 CoreText
并且我想让我的 NSMutableAttributedString
始终居中对齐其文本。这是我正在使用的代码,但它不起作用:
CTTextAlignment paragraphAlignment = kCTCenterTextAlignment;
CTParagraphStyleSetting paragraphSettings[1] = {{kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), ¶graphAlignment}};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);
text = [[NSMutableAttributedString alloc] initWithString:@"" attributes:[NSDictionary dictionaryWithObjectsAndKeys: (id)paragraphStyle, (NSString*)kCTParagraphStyleAttributeName, nil]];
有谁知道如何正确执行此操作?
最佳答案
如果您的应用兼容 10.10 或更高版本,则不使用 CoreText 的其他解决方案是使用 NSParagraphStyle。
NSMutableParagraphStyle *rectStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
rectStyle.lineBreakMode = NSLineBreakByClipping;
rectStyle.alignment = NSTextAlignmentCenter;
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:myText attributes:@{NSParagraphStyleAttributeName : rectStyle}];
关于iphone - CoreText 文本水平居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9752893/