利用HTML标记语言同样可以实现这种效果, 更加方便
在下面这个方法中写你需要的文字的属性, 字体大小, 颜色配置, 然后调用第二个方法直接设置.
NSString *htmlString = [NSString stringWithFormat:@"<strong> <font size=4>收货信息:</font></strong> <font size=4 color='#666666'>暂无收货信息.</font>"];
self.Remark.attributedText = [self htmlStringToAttributedText:htmlString];
- (NSAttributedString *)htmlStringToAttributedText:(NSString *)htmlString {
NSData *dataStr = [htmlString dataUsingEncoding:NSUnicodeStringEncoding];
NSDictionary *dict = @{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:dataStr options:dict documentAttributes:nil error:nil];
return attrStr;
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
label.backgroundColor = [UIColor cyanColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:14];
CGFloat price = 123.45;
NSString *priceStr = [NSString stringWithFormat:@"¥%.2f",price];
NSString *str =[NSString stringWithFormat:@"应付金额:%@",priceStr];
NSRange range = [str rangeOfString:priceStr];
NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc] initWithString:str];
// 添加需要改变的字体的属性
NSDictionary *dict = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:[UIColor orangeColor]};
[finalStr addAttributes:dict range:range];
label.attributedText = finalStr;
[self.view addSubview:label];
效果:
finalString.png
网友评论