CATextLayer设置字体居中

2018-04-08  本文已影响655人  沃伦盖茨

水平方向上居中使用

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;

上一篇下一篇

猜你喜欢

热点阅读