swift4.0 根据文字计算带emoji的文字高度

这是一个基本的功能,实现的方法自然也很多了,仁者见仁,智者见智,若觉得我的实现不够好,欢迎指正。

效果图:


img_80ce89e1e0550d5c4c92ec9d7403d876.gif
高度截图.gif
img_96cc92a11aa71268e351a5fa1a7bfb99.png
文字高度截图.png

代码实现:

//
//  TestCell.swift
//  SwiftCalculateHeight
//
//  Created by iOS on 2018/5/24.
//  Copyright © 2018年 weiman. All rights reserved.
//

import UIKit

let screenWidth = UIScreen.main.bounds.size.width

class TestCell: UITableViewCell {
    
    @IBOutlet weak var textL: UILabel!
    
    class func height(text: String) -> CGFloat {
        // 注意这里的宽度计算,要根据自己的约束来计算
        let maxSize = CGSize(width: (screenWidth - 30), height: CGFloat(MAXFLOAT))
        let paraph = NSMutableParagraphStyle()
        let textSize = NSString(string: text).boundingRect(
            with: maxSize,
            options: [.usesFontLeading, .usesLineFragmentOrigin],
            attributes: [.font : UIFont.systemFont(ofSize: 14),
                         .paragraphStyle: paraph],
            context: nil).size
        // 注意,这里的数字也是根据实际情况来的,这里的文字距离上面是10,距离下面也是10,所以加了两个10
        return textSize.height + 10 + 10
    }

}

demo地址:

https://github.com/weiman152/SwiftCalculateHeight/tree/master

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值