美文网首页
图文混排小例子

图文混排小例子

作者: 禅牧 | 来源:发表于2016-04-28 10:10 被阅读79次

    @IBOutlet weak var demoLabel: UILabel!

    override func viewDidLoad() {

                       super.viewDidLoad()

                     //定义两个字符串

        let attrStr = NSAttributedString(string: "李亚飞", attributes:              [NSForegroundColorAttributeName : UIColor.redColor()])

    let attrStr1 = NSAttributedString(string: "很不错", attributes: [NSForegroundColorAttributeName : UIColor.blueColor()])

    //图文混排

    let attacment = NSTextAttachment()

    attacment.image = UIImage(named: "d_aini")

    let font = demoLabel.font

    attacment.bounds = CGRectMake(0, -4, font.lineHeight, font.lineHeight)

    let attrImageStr = NSAttributedString(attachment: attacment)

    let attrMStr = NSMutableAttributedString()

    attrMStr.appendAttributedString(attrStr)

    attrMStr.appendAttributedString(attrImageStr)

    attrMStr.appendAttributedString(attrStr1)

    demoLabel.attributedText = attrMStr

    }

    显示结果

    相关文章

      网友评论

          本文标题:图文混排小例子

          本文链接:https://www.haomeiwen.com/subject/ecfhrttx.html