2017-05-31 116 views
-1

鏈接和不同的字體樣式我一直在努力實現這一目標:字符串與斯威夫特3

基本上是:一個非常簡單的文本行包括2個鏈接。當文本的其他部分處於常規狀態時,這兩個鏈接處於半角狀態。

正試圖使用​​故事板將所有內容都完成到同一個字符串中。

令人驚訝的是,它似乎很難實現。我無法理解爲什麼那麼簡單應該如此艱難。期待實現這一目標的故事板視圖...

Please view the images by clicking this link

謝謝大家的幫助!

昆汀

回答

1

我不能幫你的故事板,但您可以通過編程這樣來做:

先爲您的標籤現在

yourLabel: UILabel = 
{ 
    let label = UILabel() 

    return label 
}() 

,有一些文字加粗其餘不是,你需要聲明一個NSMutableAttributedString,然後將它附加到另一個字符串上,如下所示:

yourLabel: UILabel = 
{ 
    let label = UILabel() 

    let attributedText = NSMutableAttributedString(string: "Your Text Here", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]) 

    attributedText.append(NSAttributedString(string: "Your Bold Blue Text Here", attributes: [NSForegroundColorAttributeName: UIColor.blue, NSFontAttributeName: UIFont.boldSystemFont(ofSize: 14)])) 

    label.attributedText = attributedText 

    return label 
}() 

您現在可以將其中一個字符串設置爲鏈接/按鈕,而另一個則不可以。

希望它有幫助。