调整NSMutableAttributedString的字体大小与UILabel的帧高度成比例

时间:2017-04-06 10:24:52

标签: ios swift

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

 <%= form_for :user,url:admin_users_path, method: :post do |f| %>

如何使用

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))
    }
}

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

我正在使用“Prajeet Shrestha”的NSMutableAttributedString扩展名。

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帧更改时,我无法改变如何更改 NSMutableAttributedString 的字体大小?

任何有用的帮助。

3 个答案:

答案 0 :(得分:1)

尝试使用标签属性adjustsFontSizeToFitWidth AND minimumScaleFactor,如下所示:

label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.2

然后你还需要增加像这样的行数而不是10

label.numberOfLines = 10

答案 1 :(得分: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 :(得分:0)

最后拿出一个答案。

我创建了单独的自定义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))

“Prajeet Shrestha”对我修改的NSMutableAttributedString的扩展

扩展名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
    }
}