我正在深入研究NSAttributedString
在 iOS 上。我有一个模型,返回一个人的名字和姓氏NSAttributesString
。 (我不知道在模型中处理属性字符串是否是一个好主意!?)我希望以常规方式打印名字,而姓氏应以粗体打印。我不想要的是设置文本大小。到目前为止我发现的只是这样:
- (NSAttributedString*)attributedName {
NSMutableAttributedString* name = [[NSMutableAttributedString alloc] initWithString:self.name];
[name setAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]} range:[self.name rangeOfString:self.lastname]];
return name;
}
然而,这当然会覆盖姓氏的字体大小,这在字体中给出了非常有趣的外观UITableViewCell
其中名字将以单元格标签的常规文本大小打印,而姓氏将打印得非常小。
有什么办法可以实现我想要的吗?
感谢您的帮助!
您可以通过设置将字符串加粗而不更改其其他属性StrokeWidth
属性带有一个negative value.
Objective-C:
[name setAttributes:@{NSStrokeWidthAttributeName : @-3.0} range:NSRangeFromString(name.string)];
Swift:
name.setAttributes([.strokeWidth: NSNumber(value: -3.0)], range: NSRangeFromString(name.string))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)