代码之家  ›  专栏  ›  技术社区  ›  Francisco Romero

如何使用FontAwesome制作一个包含多行的UIButton?

  •  0
  • Francisco Romero  · 技术社区  · 8 年前

    我需要一个 UIButton 具有多于一条线。第一行将有一个图标 FontAwesome 第二个是解释图标的单词。

    此外,每行中两行的字体大小必须不同。

    以下是我目前拥有的:

    @IBOutlet weak var btnProfile: UIButton!
    
    let paraStyle = NSMutableParagraphStyle()
    paraStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
    paraStyle.alignment = NSTextAlignment.center
    
    let icon = NSMutableAttributedString(string: "\u{f082}", attributes: [NSFontAttributeName: UIFont.init(name: "FontAwesome", size: 40)])
    let text = NSMutableAttributedString(string:"\nProfile", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 12.0)])
            
    icon.append(text)
    icon.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSRange(location:0,length: icon.length))
            
    btnProfile.setAttributedTitle(icon, for: .normal)
    

    但我得到了以下错误:

    由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[_SwiftValue renderingMode]:发送到实例的选择器无法识别

    我也尝试过使用带有审讯符号的正方形,而不是 "\u{f082}" 但问题是一样的。

    我知道问题在最后两行,因为如果我对它们进行注释,应用程序不会抛出任何异常。

    我也尝试过使用故事板:

    enter image description here

    而且效果几乎很好。两行都显示为图标+文本,但文本具有图标的字体和字体大小,我希望它们不同。以下是截图:

    enter image description here

    我做错了什么?我不在乎我是用代码还是故事板来解决这个问题。

    提前感谢!

    1 回复  |  直到 4 年前
        1
  •  1
  •   Joe    8 年前

    在Swift 3。

    override func viewDidLoad() {
        super.viewDidLoad()
    
         //applying the line break mode
        btnProfile?.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping;
    
        let buttonText: NSString = "⭐️ Favourite\nProfile"
    
        //getting the range to separate the button title strings
        let newlineRange: NSRange = buttonText.range(of: "\n")
    
        //getting both substrings
        var substring1: NSString = ""
        var substring2: NSString = ""
    
        if(newlineRange.location != NSNotFound) {
            substring1 = buttonText.substring(to: newlineRange.location) as NSString
            substring2 = buttonText.substring(from: newlineRange.location) as NSString
        }
    
    
        //assigning diffrent fonts to both substrings
        let font:UIFont? = UIFont(name: "Chalkduster", size: 50.0)
        let attrString = NSMutableAttributedString(string: substring1 as String, attributes: NSDictionary(object: font!, forKey: NSFontAttributeName as NSCopying) as? [String : Any])        
        let font1:UIFont? = UIFont(name: "Noteworthy-Light", size: 30.0)
        let attrString1 = NSMutableAttributedString(string: substring2 as String, attributes: NSDictionary(object: font1!, forKey: NSFontAttributeName as NSCopying) as? [String : Any]) 
    
        //appending both attributed strings
        attrString.append(attrString1)
    
        //assigning the resultant attributed strings to the button
        btnProfile.setAttributedTitle(attrString, for: UIControlState.normal)
        btnProfile.titleLabel?.textAlignment = .center
    
    }
    

    输出:

    enter image description here