2017-04-06 16 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のStringの一部を示したいと思います。だから私はこのスレッドでいくつかの助けを見つけた:Making text bold using attributed string in swift

私はNSMutableAttributedStringのために "Prajeet Shrestha"拡張を使用しています。 UILabelフレームの変更が発生した場合しかし、私は、私はこの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 
    } 
} 

を得ていないのですか?

助けが必要です。

答えて

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

最後に答えを出してください。

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 
    } 
} 
は使用方法:次のように

は、私は別々のカスタムUILabelサブクラスを作成し

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

SETTING起因TEXT

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シュレスタさん」拡張

延長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 
    } 
} 
関連する問題