iOS 图文混排(swift)

来源:互联网 发布:xp添加网络共享打印机 编辑:程序博客网 时间:2024/06/06 10:58
////  ViewController.swift//  ImageText////  Created by fe on 2017/4/3.//  Copyright © 2017年 fe. All rights reserved.//import UIKitclass 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            }}
效果如下图:



0 0
原创粉丝点击