2017-05-04 87 views
-1

我試圖創建一個屬性串,但下劃線涵蓋了我的文字,而不是顯示它背後的:下劃線涵蓋文字NSAttributedString

enter image description here

是有辦法解決這一問題?我正在使用以下代碼:

let paragraphStyle = NSMutableParagraphStyle() 
paragraphStyle.lineSpacing = 10.0 

let attributes = [NSForegroundColorAttributeName: UIColor.white, 
        NSUnderlineStyleAttributeName: NSUnderlineStyle.styleThick.rawValue, 
        NSUnderlineColorAttributeName: UIColor.red, 
        NSParagraphStyleAttributeName: paragraphStyle] 

let attributedString = NSAttributedString(string: "Story", attributes: attributes) 

謝謝!

編輯:

爲了讓更多的上下文:

我上顯示一個UILabel的屬性串放在一個文件的.xib:

view.textLabel.attributedText = attributedString 

標籤有以下字體: 系統粗體32.0

我在iPhone 6 - iOS 10.3模擬器上運行代碼。

編輯2:

我應該提到的是,標籤可能在某些時候,包含文本的多行。這就是爲什麼numberOfLines設置爲0.

編輯3: 如果有人遇到這個問題 - 好像在iOS 9和10以及UILabel和UITextView上繪製下劃線有很大的區別。我最終不得不通過繼承NSLayoutManager來自己繪製下劃線。

+1

無法重現問題。在我的機器上,下劃線很好地與文本分開,並且不與「y」的下行相交。 https://i.stack.imgur.com/uXs4R.png請顯示更多的代碼,使其具有可重現性。你如何顯示這個屬性字符串?你使用什麼字體?等等。 – matt

+0

同樣在這裏,我也無法在我的機器上覆制您的問題。它與文本很好地分開顯示。 @GrzegorzAperliński – iPeter

+0

(另外我可能會提到你的代碼沒有在我的機器上編譯,需要明確聲明'attributes'屬於類型'[String:Any]'。) – matt

回答

2

是的,存在如您所描述的問題。當你使用多行UILabel時,它顯示出來,因此不僅將numberOfLines設置爲0,而且在其中鍵入多於1行。

let selectedStringAttributes: [String: Any] 
    = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 28), 
     NSForegroundColorAttributeName: UIColor.green, 
     NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue, 
     NSUnderlineColorAttributeName: UIColor.green] 

let label = UILabel(frame: CGRect(x: 100, y: 100, width: 500, height: 100)) 
label.numberOfLines = 0 

label.attributedText = NSAttributedString(string: "String to test underline", attributes: selectedStringAttributes) 

一切都會看起來不錯。

但是,如果你想用這樣的文字:

label.attributedText = NSAttributedString(string: "String to\ntest underline", attributes: selectedStringAttributes) 

或標籤的寬度過短,比:

Not so good

所以對這種行爲的原因當然是bugNSAttributedString 。由於它在雷達提到有一個解決方法

您應該將此屬性添加到您的NSAttributedString

和魔術會發生。

+0

感謝!我實際上已經使用NSLayoutManager的子類實現了自定義繪圖。希望這個問題將來會得到解決。 –

2

在我的機器,顯示一個黑色背景執行的UILabel你的屬性串,它使一個相當不錯的水準的展示:

enter image description here

紅色粗底線是很好的從文本分離,是中斷以允許「y」的下行通過它。

注意您不能將UILabel的font(在Interface Builder中設置)與其attributedText合併。您必須在attributedText中設置整個標籤的文本格式。所以,我的代碼如下所示:

let attributes : [String:Any] = [NSForegroundColorAttributeName: UIColor.white, 
         NSUnderlineStyleAttributeName: NSUnderlineStyle.styleThick.rawValue, 
         NSUnderlineColorAttributeName: UIColor.red, 
         NSFontAttributeName: UIFont.boldSystemFont(ofSize: 32)] 
    let attributedString = NSAttributedString(string: "Story", attributes: attributes) 
    lab.backgroundColor = .black 
    lab.attributedText = attributedString 

(你會發現,我刪除了你的段落行間距的規定,有的只是一條線,所以這一規定增加了什麼不過,我得到了相同的結果甚至。如果我恢復它。)

+0

謝謝!實際上,我注意到在代碼和IB中設置的字體權重之間幾乎沒有什麼區別,至少在渲染時。我終於發現了問題的根源(並編輯了原始條目) - 如果將行數設置爲0,則會出現問題。對不起,我應該提到我正在更改此內容,但並不認爲它是相關的... –

2

而不是使用NSAttributedString,您可以使用此X空間在標籤下面繪製邊界。

let space:CGFloat = 10 

let border = CALayer() 
border.backgroundColor = UIColor.red.cgColor 
border.frame = CGRect(x: 0, y: (label?.frame.size.height)! + space, width: (label?.frame.size.width)!, height: 1) 
label?.layer.addSublayer(border) 
+2

謝謝!我考慮過這樣做,但可能有不止一行文字,所以這不會爲我剪下來。 –

2

所以這是我對這個問題的解決方案。 我認爲這是「更清潔」,更容易。 如果您不明白,請通知我:)

class BottomLineTextField: UITextField { 

    var bottomBorder = UIView() 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     setBottomBorder() 
    } 

    func setBottomBorder() {    
     self.translatesAutoresizingMaskIntoConstraints = false 

     bottomBorder = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) 
     hasError = false 
     bottomBorder.translatesAutoresizingMaskIntoConstraints = false 

     addSubview(bottomBorder) 

     bottomBorder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true 
     bottomBorder.leftAnchor.constraint(equalTo: leftAnchor).isActive = true 
     bottomBorder.rightAnchor.constraint(equalTo: rightAnchor).isActive = true 
     bottomBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true // Set underline height 
    } 
} 
+1

只要只有一行文字,此解決方案就可以。 –

+1

嗨@GrzegorzAperliński,你說得對。 我這樣做只是爲了一行 – ironRoei