2017-04-06 73 views
0

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

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字符串的某一部分作为文字加粗,并在剩余的常规文本。所以,我发现这个线程一些帮助:Making text bold using attributed string in swift

我使用“Prajeet什雷斯塔的”扩展NSMutableAttributedString。

// "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 
    } 
} 

但我没有得到我怎样才能改变这种NSMutableAttributedString的字体大小,当的UILabel框架发生变化?

任何帮助appeciated。

回答

1

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

label.adjustsFontSizeToFitWidth = true 
label.minimumScaleFactor = 0.2 

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

label.numberOfLines = 10 
+0

我不想根据UILabel的边框宽度进行调整。我想字体大小成正比UILabel的框架高度 – appleBoy21

+0

感谢显示在回答的兴趣。但请阅读我的问题的完整场景。我想重新更改NSMutableAttributedString字体大小。 – appleBoy21

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

感谢您的回答。会尝试让你知道 – appleBoy21

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什雷斯塔的”扩展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 
    } 
} 
相关问题