我正在研究iOS上的NSAttributedString
。 我有一个模型,它将人的名字和姓氏作为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
中会出现非常诡异的情况,因为名字将以标签单元格标签的正常文本大小打印,而姓氏则会非常小。
有没有办法实现我想要的效果?
感谢您的帮助!