//
// ViewController.swift
// ImageText
//
// Created by fe on 2017/4/3.
// Copyright © 2017年 fe. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//展示文本的标签
@IBOutlet weak var textLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let str1 = NSAttributedString(string: "今天的月亮很圆", attributes: [NSForegroundColorAttributeName : UIColor.red])
let str2 = NSAttributedString(string: "晚安", attributes: [NSForegroundColorAttributeName : UIColor.purple])
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "w_yueliang")
let font = textLabel.font
attachment.bounds = CGRect(x: 0, y: -4, width: (font?.lineHeight)!, height: (font?.lineHeight)!)
let imageStr = NSAttributedString(attachment: attachment)
let mstr = NSMutableAttributedString()
mstr.append(str1)
mstr.append(imageStr)
mstr.append(str2)
textLabel.attributedText = mstr
}
}
效果如下图: