美文网首页
iOS Swift 根据宽度计算字体大小

iOS Swift 根据宽度计算字体大小

作者: KingWorld | 来源:发表于2021-11-22 16:40 被阅读0次

    背景:因为UI给的是背景图,要在背景图中间位置显示一个富文本Label

    image.png

    String宽度计算与高度计算

    extension String{
        /// String的宽度计算
        /// - Parameter font: 设定字体前提
        public func widthWith(font: UIFont) -> CGFloat {
            let str = self as NSString
            return str.size(withAttributes: [NSAttributedString.Key.font:font]).width
        }
        /// String的高度计算
        /// - Parameters:
        ///   - width: 设定宽度前提
        ///   - lineSpacing: 设定行高前提
        ///   - font: 设定字体前提
        public func heightWith(width: CGFloat, lineSpacing: CGFloat = 1.5, font: UIFont) -> CGFloat {
            
            let str = self as NSString
            let paragraphStyle = NSMutableParagraphStyle.init()
            paragraphStyle.lineSpacing = lineSpacing
            let size = str.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin,.usesFontLeading], attributes: [NSAttributedString.Key.paragraphStyle:paragraphStyle,NSAttributedString.Key.font:font], context: nil).size
            return size.height
        }
        /// String的宽度计算
        /// - Parameter font: 设定字体前提
        public func widthHeight(_ size:CGSize,font: UIFont,attributes : [NSAttributedString.Key : Any]? = nil) -> CGFloat {
            let str = self as NSString
            let s = str.boundingRect(with: size, options: [.usesLineFragmentOrigin,.usesFontLeading], attributes: [NSAttributedString.Key.font : font], context: nil).size.height
            return s
        }
    }
    

    设置富文本

    //设置富文本
      func setAttrString(firstStr : String , secStr : String) -> NSAttributedString {
            let attrString = NSMutableAttributedString(string: firstStr + secStr)
            let strSubAttr1: [NSMutableAttributedString.Key: Any] = [.font: UIFont(name: "PingFangSC", size: 24) ?? UIFont.systemFont(ofSize: 24),.foregroundColor: UIColor.color(hexString: "#DD1827")]
            attrString.addAttributes(strSubAttr1, range: NSRange(location: 0, length: firstStr.count))
            //如果价格位数太多就将字体降低
            let fontSize = calculateStringWidth(code: firstStr, price: secStr)
            
            let strSubAttr2: [NSMutableAttributedString.Key: Any] = [.font: UIFont(name: "PingFangSC", size: CGFloat(fontSize)) ?? UIFont.systemFont(ofSize: CGFloat(fontSize)),.foregroundColor: UIColor.color(hexString: "#DD1827")]
            attrString.addAttributes(strSubAttr2, range: NSRange(location: firstStr.count, length: secStr.count))
            return attrString
        }
    
    //根据宽度来计算title 字体大小
        func calculateStringWidth(code:String,price:String)-> CGFloat {
            //136是title可放置的宽度
            let standardWidth = (SCREEN_WIDTH - 30) / 345 * 136
            let codeWidth = code.widthWith(font: UIFont(name: "PingFangSC", size: 24) ?? UIFont.systemFont(ofSize: 24))
            //leftWidth是价格可放置的宽度
            let leftWidth = standardWidth - codeWidth
            var fontSize : CGFloat = 48
            repeat{
                fontSize -= 1
            } while price.widthWith(font: UIFont(name: "PingFangSC", size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)) > leftWidth
            return fontSize
        }
    

    相关文章

      网友评论

          本文标题:iOS Swift 根据宽度计算字体大小

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