Swift版本:
let str:String = "wenzi";
let text = "\(str) button pressed" //运用\符号可以拼接字符创;
// 创建一个属性字符创,并且初始化;
let styleText = NSMutableAttributedString (string : text);
// 创建文字的属性:是一个字典类型;
let Attribut = [NSFontAttributeName:UIFont.boldSystemFontOfSize(lable.font.pointSize)]
// 获取需要属相显示的字符串的范围;首先先强转为Objec-c的NSString;
let nameRange = (text as NSString).rangeOfString(str);
// 然后给属性字符串赋值;
styleText.setAttributes(Attribut, range: nameRange)
// 传给lable标签显示;
lable.attributedText = styleText;
Object-c版本:
NSString *title = @"Left";
NSString *plainText = @"Left button preesed";
NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc]
initWithString:plainText];
//字典类型的赋值;
NSDictionary *attributes =
@{
NSFontAttributeName : [UIFont boldSystemFontOfSize:_statusLabel.font.pointSize]
};
NSRange nameRange = [plainText rangeOfString:title];
[styledText setAttributes:attributes range:nameRange];
_statusLabel.attributedText = styledText;
两者的语法差别不是很大,所以带上基本都是差不多的;