【问题标题】:Adjust font size of NSMutableAttributedString proportional to UILabel's frame height调整 NSMutableAttributedString 的字体大小与 UILabel 的框架高度成比例
【发布时间】:2017-09-01 07:44:12
【问题描述】:

在我的项目中,我使用的是 swift 3.0。现在我正在使用以下类 (UILabel 子类) 根据 UILabel 框架高度调整字体大小。 当 UILabel 框架发生变化时,layoutSubviews 会重新计算比例字体大小。

class Label: UILabel {

    // FIXME: - properties
    var fontSize: CGFloat = 0
    var frameHeight: CGFloat = 0

    // FIXME: - proportional font size adjustment
    override func layoutSubviews() {
        super.layoutSubviews()
        font = font.withSize(frame.size.height * (fontSize / frameHeight))
    }
}

如何使用:

private let id: Label = {
        let label = Label()
        label.textAlignment = .left
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
        label.textColor = UIColor(hex: 0x212121, alpha: 1)
        label.fontSize = 17
        label.frameHeight = 20
        label.clipsToBounds = true
        return label
    }()

现在我想在 UILabel 中将 String 的某些部分显示为 BOLD TEXT 并保留在 REGULAR TEXT 中。所以我在这个线程上找到了一些帮助:Making text bold using attributed string in swift

我正在为 NSMutableAttributedString 使用“Prajeet Shrestha's”扩展。

// "Prajeet Shrestha's" extension
extension NSMutableAttributedString {
    func bold(_ text:String) -> NSMutableAttributedString {
        let attrs:[String:AnyObject] = [NSFontAttributeName : UIFont(name: "AvenirNext-Medium", size: 12)!]
        let boldString = NSMutableAttributedString(string:"\(text)", attributes:attrs)
        self.append(boldString)
        return self
    }

    func normal(_ text:String)->NSMutableAttributedString {
        let normal =  NSAttributedString(string: text)
        self.append(normal)
        return self
    }
}

但是当 UILabel 帧发生更改时,我不知道如何更改此 NSMutableAttributedString 的字体大小?

任何帮助。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    试试这个

    来源Looping Through NSAttributedString Attributes to Increase Font SIze

     mutableStringObj?.enumerateAttribute(NSFontAttributeName, in: NSRange(location: 0, length: mutableStringObj?.length), options: [], usingBlock: {(_ value: Any, _ range: NSRange, _ stop: Bool) -> Void in
                if value {
                    var oldFont: UIFont? = (value as? UIFont)
                    var newFont: UIFont? = oldFont?.withSize(CGFloat(oldFont?.pointSize * 2))
                    res?.removeAttribute(NSFontAttributeName, range: range)
                    res?.addAttribute(NSFontAttributeName, value: newFont, range: range)
                }
            })
    

    【讨论】:

    • 感谢您的回答。会尽力让你知道
    【解决方案2】:

    尝试像这样使用标签属性 adjustsFontSizeToFitWidth 和 minimumScaleFactor:

    label.adjustsFontSizeToFitWidth = true
    label.minimumScaleFactor = 0.2
    

    那么你还需要增加这样的行数,而不是 10

    label.numberOfLines = 10
    

    【讨论】:

    • 我不想根据 UILabel 的框架宽度进行调整。我希望字体大小与 UILabel 的框架高度成比例
    • 感谢您有兴趣回答。但请阅读我的问题的完整场景。我想重新更改 NSMutableAttributedString 字体大小。
    【解决方案3】:

    终于想出答案了。

    我创建了单独的自定义 UILabel 子类,如下所示:

    class AttrLabel: UILabel {
    
        // FIXME: - properties
        var fontSize: CGFloat = 0
        var frameHeight: CGFloat = 0
    
        // FIXME: - proportional font size adjustment
        override func layoutSubviews() {
            super.layoutSubviews()
    
            guard let oldAttrText = attributedText else {
                return
            }
    
            let mutableAttributedText = NSMutableAttributedString(attributedString: oldAttrText)
            mutableAttributedText.beginEditing()
    
            mutableAttributedText.enumerateAttribute(NSFontAttributeName, in: NSRange(location: 0, length: mutableAttributedText.length), options: []) { (_ value: Any?, _ range: NSRange, _ stop: UnsafeMutablePointer<ObjCBool>) in
                if let attributeFont = value as? UIFont {
                    let newFont = attributeFont.withSize(self.frame.size.height * (self.fontSize / self.frameHeight))
                    mutableAttributedText.removeAttribute(NSFontAttributeName, range: range)
                    mutableAttributedText.addAttribute(NSFontAttributeName, value: newFont, range: range)
                }
            }
    
            mutableAttributedText.endEditing()
            attributedText = mutableAttributedText
        }
    }
    

    如何使用:

    private let id: AttrLabel = {
        let label = AttrLabel()
        label.textAlignment = .left
        label.numberOfLines = 1
        label.fontSize = 17
        label.frameHeight = 20
        label.clipsToBounds = true
        return label
    }()
    

    设置属性文本

    let idStr = NSMutableAttributedString()
    id.attributedText = idStr.attrStr(text: "BOLD TEXT: ", font: UIFont.systemFont(ofSize: 17, weight: .semibold), textColor: UIColor(hex: 0x212121, alpha: 1)).attrStr(text: "REGULAR WEIGHT TEXT.", font: UIFont.systemFont(ofSize: 17, weight: .regular), textColor: UIColor(hex: 0x212121, alpha: 1))
    

    我修改的 NSMutableAttributedString 的“Prajeet Shrestha's”扩展

    扩展 NSMutableAttributedString {

    func attrStr(text: String, font: UIFont, textColor: UIColor) -> NSMutableAttributedString {
            let attributes: [String: Any] = [
                    NSFontAttributeName: font,
                    NSForegroundColorAttributeName: textColor
            ]
            let string = NSMutableAttributedString(string: text, attributes: attributes)
            self.append(string)
            return self
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 2019-09-18
      相关资源
      最近更新 更多