美文网首页
iOS Swift字符串处理

iOS Swift字符串处理

作者: 邓布利多教授 | 来源:发表于2022-04-06 09:41 被阅读0次

创建String扩展

源码

// MARK: - 字符串处理
extension String {
    
    func getParagraphStringHeight(lineHeight: CGFloat, font: UIFont, maxWidth: CGFloat) -> CGFloat {
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.maximumLineHeight = lineHeight
        paraStyle.minimumLineHeight = lineHeight
        
        let attri = [NSAttributedString.Key.font: font,
                     NSAttributedString.Key.paragraphStyle: paraStyle]
        
        let size = (self as NSString).boundingRect(with: CGSize(width: maxWidth, height: CGFloat(MAXFLOAT)),
                                                   options: [.usesLineFragmentOrigin, .usesFontLeading],
                                                   attributes: attri,
                                                   context: nil).size
        
        let textHeight = CGFloat(ceilf(Float(size.height)))
        
        return textHeight < lineHeight ? lineHeight : textHeight
    }
    
    func getAttributedText(_ font: UIFont, color: UIColor, lineHeight: CGFloat) -> NSAttributedString {
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.minimumLineHeight = lineHeight
        paraStyle.maximumLineHeight = lineHeight
        
        let attrs = [NSAttributedString.Key.font: font,
                     NSAttributedString.Key.foregroundColor: color,
                     NSAttributedString.Key.paragraphStyle: paraStyle]
        
        return NSAttributedString.init(string: self, attributes: attrs)
    }
    
    func getStringWidth(width: CGFloat, height: CGFloat, font: UIFont) -> CGFloat {
        let size = (self as NSString).boundingRect(with: CGSize(width: width, height: height),
                                                   options: [.usesLineFragmentOrigin, .usesFontLeading],
                                                   attributes: [NSAttributedString.Key.font: font],
                                                   context: nil).size
        
        let textWidth = CGFloat(ceilf(Float(size.width)))
        
        return textWidth
    }
    
    func getAttributedString(subStrings: [String], colors: [UIColor], fonts: [UIFont]) -> NSAttributedString {
        
        let paraStyle = NSMutableParagraphStyle()
        paraStyle.lineBreakMode = .byCharWrapping
        
        let attributedString = NSMutableAttributedString.init(string: self,
                                                              attributes: [NSAttributedString.Key.paragraphStyle: paraStyle,
                                                                           NSAttributedString.Key.kern: 0.0])
        
        if subStrings.count != colors.count, subStrings.count != fonts.count {
            
            return attributedString
        }
        
        for (index, _) in subStrings.enumerated() {
            
            /// 子字符串
            let string = subStrings[index]
            let range = NSString(string: self).range(of: string)
            
            /// 颜色
            let color = colors[index]
            attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range)
            
            /// 字体大小
            let font = fonts[index]
            attributedString.addAttribute(NSAttributedString.Key.font, value: font, range: range)
            
        }
        return attributedString
        
    }
    
}

针对改变字符串字体样式再计算高度方式,限制最大宽度

let size = contentLabel.sizeThatFits(CGSize(width: contentWidth, height: CGFloat(MAXFLOAT)))

相关文章

网友评论

      本文标题:iOS Swift字符串处理

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