您可以使用 AttributedString,而不是为富文本使用两个不同的标签。这是一个例子:
- (NSMutableAttributedString*)getRichText {
NSString *str1 = @"I am bold ";
NSString *str2 = @"I am simple";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[str1 stringByAppendingString:str2]];
UIFont *font1=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
UIFont *font2=[UIFont fontWithName:@"Helvetica" size:20.0f];
NSInteger l1 = str1.length;
NSInteger l2 = str2.length;
[attString addAttribute:NSFontAttributeName value:font1 range:NSMakeRange(0,l1)];
[attString addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(l1,l2)];
return attString;
}
在 View did load 中,您可以将字符串设置为标签,如下所示:
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
[self.view addSubview:textLabel];
textLabel.attributedText = [self getRichText];
输出:
