2014-12-18 2 views
1

아래 그림과 같이 UILabel/UITextView를 정렬하려고합니다.IOS - 신문처럼 UILabel을 정당화하십시오.

누구나 텍스트를 그렇게 정당화하는 방법을 알아 냈습니다.

enter image description here

+0

이 질문은 [너무 광범위] (http://stackoverflow.com/help/on-topic)에 NSMutableParagraphStyle 테스트를 사용하는 것입니다. – jurgemaister

+0

@jurgemaister 매우 구체적인 것처럼 보입니다. ibm123은 텍스트보기에서 텍스트를 첨부 된 이미지에서 정당화하는 방법으로 정당화하는 방법을 알고 자합니다. madhu가 한 줄의 코드로 대답 할 수있을만큼 구체적입니다. –

+0

UITextView를 사용해보십시오. 시도하지 않았지만 UILabel에 많은 버그가 있습니다. – Rambatino

답변

3

보십시오 내가 너무 NSTextAlignementJustified 몇 가지 문제를 가지고 당신의 UILabel

label.textAlignment = NSTextAlignmentJustified; 
+0

아니요, 작동하지 않습니다. – ibm123

+0

@ ibm123 텍스트보기가 명확하게 정당화되었습니다. 라벨은 아마도 중심에 위치 할 것입니다 :'''NSTextAlignmentCenter''' –

8

NSTextAlignmentJustified에의 정렬을 설정합니다.

당신은 정렬을 설정하여 제로에 가까운 뭔가 firstLineHeadIndent을 설정하여, NSAttributedString은의 사용과이를 달성 할 수있다 (왜? 흠 ... 잘 모르겠어요) :

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyles.alignment    = NSTextAlignmentJustified; 
paragraphStyles.firstLineHeadIndent  = 0.001f; 
NSString *stringTojustify    = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"; 
NSDictionary *attributes     = @{NSParagraphStyleAttributeName: paragraphStyles}; 
NSAttributedString *attributedString  = [[NSAttributedString alloc] initWithString:stringTojustify attributes:attributes]; 

CGFloat labelWidth = self.view.frame.size.width * 0.7f; 
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.5f - labelWidth * 0.5f, 
                  100, 
                  labelWidth, 
                  800)]; 
myLabel.backgroundColor = [UIColor orangeColor]; 
myLabel.textColor = [UIColor whiteColor]; 
myLabel.numberOfLines = 0; 
myLabel.attributedText = attributedString; 
[myLabel sizeToFit]; 

[self.view addSubview:myLabel]; 
2

Y Bonafons 감사합니다! 스위프트에 새로운이기 때문에, 그것은 당신의 코드를 사용하는 나에게 꽤했다, 그래서 나는 여기 스위프트 2 있음을 입력 해요 :

import UIKit 

class ViewController: UIViewController { 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     let labelWidth = CGFloat(self.view.frame.size.width * 0.7) 

     let explanationLabel = UILabel(frame: CGRectMake(self.view.frame.size.width * 0.5 - labelWidth * 0.5, 100, labelWidth, 800)) 

     let paragraphStyle = NSMutableParagraphStyle() 

     paragraphStyle.alignment = NSTextAlignment.Justified 

     paragraphStyle.firstLineHeadIndent = 15 

     paragraphStyle.paragraphSpacingBefore = 10.0 

     explanationLabel.attributedText = NSAttributedString(string: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum") 

     let mutableAttrStr = NSMutableAttributedString(attributedString: explanationLabel.attributedText!) 

     mutableAttrStr.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, mutableAttrStr.length)) 

     explanationLabel.backgroundColor = UIColor.orangeColor() 

     explanationLabel.textColor = UIColor.whiteColor() 

     explanationLabel.numberOfLines = 0 

     explanationLabel.attributedText = mutableAttrStr 

     explanationLabel.sizeToFit() 

     self.view.addSubview(explanationLabel) 

    } 

} 

난이 도움이되기를 바랍니다!

0

Y. Bonafons의 위대한 답변 외에 인터페이스 작성기를 통해 'First Line Indent'을 설정하여 동일한 결과를 얻을 수 있습니다 (xCode 7.3.1에서 확인).

1

완벽한 업데이트 솔루션은 엑스 코드 7, 아이폰 OS 9

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init]; 
     paragraphStyles.alignment = NSTextAlignmentJustified;  //justified text 
     paragraphStyles.firstLineHeadIndent = 1.0; 
     NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles}; 
     NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: YourString attributes: attributes]; 
     YourLabel.attributedText = attributedString; 
관련 문제