- 问题描述
使用NSMutableAttributedString设置不同字体,
int a = 50; int b = 10;
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"10元抵现券"];
[attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:a] range:NSMakeRange(0, 2)];
[attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:b] range:NSMakeRange(2, attStr.length - 2)];
// [attStr addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (a - b)) range:NSMakeRange(2, attStr.length - 2)];
label.attributedText = attStr;
文字不能居中对齐;如图:
这里写图片描述- 解决方法
NSMutableAttributedString 添加NSBaselineOffsetAttributeName这个Attribute
[attStr addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (a - b)) range:NSMakeRange(2, attStr.length - 2)];
value = 0.36 * (大字号 - 小字号)
int a = 50;
int b = 10;
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"10元抵现券"];
[attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:a] range:NSMakeRange(0, 2)];
[attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:b] range:NSMakeRange(2, attStr.length - 2)];
[attStr addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (a - b)) range:NSMakeRange(2, attStr.length - 2)];
label.attributedText = attStr;
- 效果图 这里写图片描述
网友评论