swift 3.0
//定义富文本即有格式的字符串
let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()
//邱学伟
let qiuxuewei : NSAttributedString = NSAttributedString(string: "邱学伟", attributes: [ NSBackgroundColorAttributeName : UIColor.red,NSForegroundColorAttributeName : UIColor.green, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 28.0)]) //(string: "邱学伟")
//是
let shi : NSAttributedString = NSAttributedString(string: "是", attributes: [NSForegroundColorAttributeName : UIColor.blue, NSFontAttributeName : UIFont.systemFont(ofSize: 10.0)])
//大帅哥
let dashuaige : NSAttributedString = NSAttributedString(string: "大帅哥", attributes: [NSForegroundColorAttributeName : UIColor.lightGray, NSFontAttributeName : UIFont.systemFont(ofSize: 42.0)])
//笑脸图片
let smileImage : UIImage = UIImage(named: "d_hehe")!
let textAttachment : NSTextAttachment = NSTextAttachment()
textAttachment.image = smileImage
textAttachment.bounds = CGRect(x: 0, y: -4, width: 22, height: 22)
attributedStrM.append(qiuxuewei)
attributedStrM.append(shi)
attributedStrM.append(dashuaige)
attributedStrM.append(NSAttributedString(attachment: textAttachment))
label.attributedText = attributedStrM
}
3.0
//定义富文本即有格式的字符串
let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()
//折扣后的价
let currentPriceString : NSAttributedString = NSAttributedString(string: "¥9.9 ", attributes: [ NSBackgroundColorAttributeName : UIColor.clear,NSForegroundColorAttributeName : UIColor.red, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 20.0)])
//原价 给价格加上中划线
let oldPriceStr = "¥24.9"
let oldPriceString: NSMutableAttributedString = NSMutableAttributedString(string: oldPriceStr, attributes: [NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName : UIFont.systemFont(ofSize: 12.0)])
oldPriceString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber.init(value: 1), range: NSRange(location: 0, length: oldPriceStr.length))
attributedStrM.append(currentPriceString)
attributedStrM.append(oldPriceString)
goodsPriceLabel.attributedText = attributedStrM
中划线
swift 4.0
//定义富文本即有格式的字符串
let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()
//折扣后的价
let currentPriceString : NSAttributedString = NSAttributedString(string: "¥9.9 ", attributes: [ NSAttributedStringKey.backgroundColor : UIColor.clear,NSAttributedStringKey.foregroundColor : UIColor.red, NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 20.0)])
//原价 给价格加上中划线
let oldPriceStr = "¥24.9"
let oldPriceString: NSMutableAttributedString = NSMutableAttributedString(string: oldPriceStr, attributes: [NSAttributedStringKey.foregroundColor : UIColor.lightGray, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12.0)])
oldPriceString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber(value: 1), range: NSMakeRange(1, oldPriceStr.count-1))//主要这一句
attributedStrM.append(currentPriceString)
attributedStrM.append(oldPriceString)
goodsPriceLabel.attributedText = attributedStrM
oc版
添加中划线:
UILabel * strikeLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 50, 30))]; NSString *textStr = [NSString stringWithFormat:@"%@元", primeCost]; //中划线 NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]}; NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic]; // 赋值 strikeLabel.attributedText = attribtStr; [self.view addSubview:strikeLabel];
添加下划线:
UILabel *underlineLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 50, 30))]; NSString *textStr = [NSString stringWithFormat:@"%@元", primeCost]; // 下划线 NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]}; NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic]; //赋值 underlineLabel.attributedText = attribtStr; [self.view addSubview:underlineLabel];