2014-12-30 5 views
11

밑줄이 다른 일반 밑줄 메커니즘과 마찬가지로 텍스트 아래에 있도록하고 싶습니다. 그러나 NSAttributed 문자열은 "g의"와 구멍 "Y의"NSUnderlineStyleAttributeName 밑줄 간격

예를 잎 : enter image description here

이 보일 것입니다 방법 : enter image description here

가 어떻게 밑줄 및 레이블 사이의 간격을 증가시킬 수있다?

+0

내가 아는 한 당신은 할 수 없다. 그러나 심지어 그렇다 : 왜 두 번째 버전이 필요한가? 그것은 훨씬 읽기 쉽습니다. 특히 작은 글꼴 크기. – DarkDust

+0

@DarkDust 왜 내가 원하는가? 그것이 예상되는 밑줄 동작입니다. Microsoft Word 또는 Google 문서를 열면 그게 네가 얻는거야! – MobileMon

답변

9

NSAttributedString 또는 CoreText로 동작을 제어 할 수있는 방법은 없습니다 (밑줄을 직접 그려야하는 것은 제외). NSAttributedString에는 해당 옵션이 없습니다 (및 CoreText hasn't got one, either).

Apple 시스템에서 첫 번째 버전 (틈이 있음)은 Apple에서 제공하는 것으로 "Safari, TextEdit 등과 같은 응용 프로그램과 같이 시스템에서 사용되는"예상되는 동작입니다.

틈이없는 밑줄이 필요하다면 줄 바꿈없이 문자열을 그리고 직접 그려야합니다 (in one of my projects and I can tell you it's hard : this file, 밑줄로 검색 참조).

0

UILabel의 하단에 맞춰 높이 1과 너비가 레이블 인 줄 (UIView)을 추가했습니다.

let label = UILabel() 
    label.text = "underlined text" 

    let spacing = 2 // will be added as negative bottom margin for more spacing between label and line 

    let line = UIView() 
    line.translatesAutoresizingMaskIntoConstraints = false 
    line.backgroundColor = label.textColor 
    label.addSubview(line) 
    label.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[line]|", metrics: nil, views: ["line":line])) 
    label.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[line(1)]-(\(-spacing))-|", metrics: nil, views: ["line":line]))