CATextLayer设置字体居中

水平方向上居中使用

textLayer.alignmentMode = kCAAlignmentCenter;

即可

垂直方向居中

采用NSBaselineOffsetAttributeName基础偏移量

采用富文本形式

例子:

NSString *numStr = [NSString stringWithFormat:@"%d",numerPath];

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];

NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:numStr attributes:@{NSForegroundColorAttributeName:WhiteColor,NSParagraphStyleAttributeName:paragraph,NSBaselineOffsetAttributeName:@(-160)}];

[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:460.0f] range:NSMakeRange(0, numStr.length)];

textLayer.string = attributedStr;

你可能感兴趣的:(CATextLayer设置字体居中)