Swift归属标题语法

时间:2016-09-20 04:44:09

标签: ios swift swift2 swift3 swift2.3

我试图做这样的事情:

let introText = "This is sample "
let facebookText = "Text"
loginButton.setTitle("\(introText)\(facebookText)", forState: .Normal)
loginButton.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial", size: 15.5)!, range: NSMakeRange(0, introText.characters.count))
loginButton.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial-Bold", size: 15.5)!, range: NSMakeRange(introText.characters.count, facebookText.characters.count))
loginButton.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, introText.characters.count + facebookText.characters.count))

以这种形式:

loginButton.setAttributedTitle(NSMutableAttributedString(
    string: "\(introText)\(facebookText)",
    attributes: [
        // attributes go here
        NSForegroundColorAttributeName: UIColor.colorFromCode(0x151515),
    ]), forState: UIControlState.Normal)

我可以这样做吗?我无法弄清楚如何在包含的属性中包含范围。

修改

很抱歉,如果它不明显,但我的代码所做的是创建一个读取"这是样本文本"单词Text加粗,如图所示。我试图将此代码重写为我显示的第二种语法形式,如果它甚至可能的话。

谢谢!

2 个答案:

答案 0 :(得分:0)

如果您正在寻找带有粗体部分的属性文本,这将为您完成。

extension String {

  func getPartOfStringBold(boldPart:String)-> NSAttributedString{
    return getPartOfStringBold(boldPart, font: UIFont(name: "Taz-Bold", size: 13)!)
 }

//Make your string bold
  func getPartOfStringBold(boldPart:String, font:UIFont)-> NSAttributedString{
    let attributtedString = NSMutableAttributedString(string: self)
    let attrs = [NSFontAttributeName:font]
    let rangePart: NSRange = (attributtedString.string as NSString).rangeOfString(boldPart)
    attributtedString.addAttributes(attrs, range: rangePart)
    return attributtedString
  }

//Make your string Italic
  func getPartOfStringItalic(italicPart:String)-> NSAttributedString{
    let attributtedString = NSMutableAttributedString(string: self)

    if let font = UIFont(name: "Taz-LightItalic", size: 13)
    {
        let attrs = [NSFontAttributeName:font, NSForegroundColorAttributeName:UIColor.blackColor()]
        let rangePart: NSRange = (attributtedString.string as NSString).rangeOfString(italicPart)
        attributtedString.addAttributes(attrs, range: rangePart)
    }
    return attributtedString
  }
}

答案 1 :(得分:0)

据我了解您的问题,如果是,那么您希望将测试的一部分设为粗体,然后这是我为此目的使用的扩展

import UIKit
import Foundation

extension String
{

static func makeTextBold(preBoldText:String, boldText:String, postBoldText:String, fontSzie:CGFloat) -> NSAttributedString {

    let boldAttrs = [NSFontAttributeName : UIFont(name: "HelveticaNeue-Bold", size: fontSzie) as? AnyObject]
    let attributedString = NSMutableAttributedString(string:boldText, attributes:boldAttrs as? [String:AnyObject])

    let lightAttr = [NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: fontSzie) as? AnyObject]
    let finalAttributedText = NSMutableAttributedString(string:preBoldText, attributes:lightAttr as? [String:AnyObject])

    let postText = NSMutableAttributedString(string:postBoldText, attributes:lightAttr as? [String:AnyObject])

    finalAttributedText.appendAttributedString(attributedString)
    finalAttributedText.appendAttributedString(postText)
    // print(finalAttributedText)
    return finalAttributedText
}
}

所以基本上这个函数正在做的是你为文本传递三个参数,为fot大小传递一个参数,它将返回一个属性字符串。

以下是您案例的示例用法

myLabel.attributedText = String.makeTextBold("This is sample", boldText: "Text", postBoldText: "", fontSzie: 21)

输出将是

这是示例文字

我希望这回答这个问题: - )