2017-09-22 2 views
0

레이블이 있고 속성이 지정된 문자열을 추가했습니다. 문자열은 내가 텍스트의 시작 부분에 *를 추가하기 위해 노력하고있어이 텍스트에서 줄 수가 0이고 높이 제한이 없어도 내 레이블이 잘리는 이유는 무엇입니까?

let nameText = "My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer."`My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer.` 

그래서 나는 기인 문자열을 사용하고 코드는

func attributedTextForFeeApplies() -> NSAttributedString { 
    let attributedText = NSMutableAttributedString(string: "* " + nameText) 
    attributedText.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, attributedText.length)) 
    attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 10), range: NSMakeRange(0, attributedText.length)) 

    let superScriptString = "* " 

    attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count)) 
    attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 9), range: NSMakeRange(0, superScriptString.characters.count)) 
    attributedText.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, superScriptString.characters.count)) 

    let superscriptAttributedString = attributedText 
    let paragraph = NSMutableParagraphStyle() 
    paragraph.lineBreakMode = .byTruncatingTail 
    superscriptAttributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: NSMakeRange(0, superscriptAttributedString.length)) 

    return superscriptAttributedString 
} 

내가 준 다음과 같습니다이다 내가 0없이 헤이로 라인의 수를 설정하더라도이 같은 레이블에 제약

enter image description here

레이블이 같이 절단되어 GHT 제약, enter image description here

하지만 코드가이 같은 벌금을하고있다 attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count))이 라인을 사용하지 않는 경우,

enter image description here

그리고 또한에 *을 추가하는 경우 그것은 잘 작동하지만 텍스트의 시작 부분에 사용하는 경우 그냥 작동하지 않습니다 ( attributedText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: NSMakeRange(0, superScriptString.characters.count))를 제거하지 않고) 중간

스크린 샷 :

enter image description here

그리고 글꼴 크기를 늘리면 작동합니다.

나는 NSAttributedString에 문제가 있다고 생각하는데, 그렇지 않다면 나는 무엇이 문제인지 알고 싶다. 누군가 나를 도와주세요.

+0

beacuse는 superscriptAttributedString = attributedText 가합시다 단락 NSMutableParagraphStyle =() = paragraph.lineBreakMode .byTruncatingTail superscriptAttributedString.addAttribute (NSAttributedStringKey.paragraphStyle, 값 : 단락 범위 : NSMakeRange (0 superscriptAttributedString.length)) 당신의 lable은 줄 수에 넣을 수 있습니다. –

+0

나는 당신이 말한 것을 얻지 못했습니다 (당신의 lable은 줄 수에 넣을 수 있습니까?). 좀 더 자세히 설명해 주시겠습니까? –

+0

NSMutableParagraphStyle은 한 줄을 여러 줄로 변환한다는 것을 의미하므로 텍스트 한 줄만 쓰려면 NSMutableParagraphStyle을 사용하지 마십시오. –

답변

0

문제가 발생한 문자열을 할당하는 즉시 높이를 다시 계산해야합니다. 그러나 당신이 당신의 자기 그것을 계산할 필요가없는 빠른 수정이있다. 라벨 끝에 라벨을 붙이면 자동으로 높이를 다시 계산해야합니다. 이러한

// see the \n at the end of your string that will cause the label to recalculate it's height. 
let nameText = "My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer."`My name is Shreesha and Im an iOS developer. My name is Shreesha and Im an iOS developer. \n" 
+0

제 질문은 현재 구현에서도 작동한다고 가정합니다. 하지만 NSAttributedString 또는 UILabel에 문제가있는 것으로 보입니다. 현재 구현이 작동해야하는 부분에 대한 빠른 수정을 원하지 않습니다. 그러나 지금은 가능한 한 빨리 빌드를 릴리스하기 위해 응용 프로그램 내부에 특성 문자열을 추가하지 않았습니다.네 제안이 효과가 있습니다. 그러나 나는 그 문제가 무엇인지 알고 싶다. 나는 그것을 현재의 구현과 함께 조정할 필요가 없다. –

+0

Xcode에는 어떤 문자의 오프셋을 변경하려고 할 때 attributedString의 정확한 크기를 계산할 수없는 버그가 있음을 이미 설명했습니다. 이 경우 문자열을 기준으로 레이블의 높이를 계산해야합니다. –

관련 문제