답변
보십시오 내가 너무 NSTextAlignementJustified
몇 가지 문제를 가지고 당신의 UILabel
label.textAlignment = NSTextAlignmentJustified;
아니요, 작동하지 않습니다. – ibm123
@ ibm123 텍스트보기가 명확하게 정당화되었습니다. 라벨은 아마도 중심에 위치 할 것입니다 :'''NSTextAlignmentCenter''' –
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];
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)
}
}
난이 도움이되기를 바랍니다!
Y. Bonafons의 위대한 답변 외에 인터페이스 작성기를 통해 'First Line Indent'을 설정하여 동일한 결과를 얻을 수 있습니다 (xCode 7.3.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;
- 1. IOS - UITableViewCell에서 UILabel을 어떻게 이동합니까?
- 2. iOS 8에서 uilabel을 회전하는 방법
- 3. 동적으로 다른 UILabel을 실행하기 전에 UILabel을 자릅니다.
- 4. iOS Swift - UILabel을 오른쪽에서 왼쪽으로 자동 스크롤
- 5. Iphone - 인터페이스 빌더에서 텍스트 정렬 방법 (신문처럼)
- 6. 센터 중간 flexbox 품목 및 나머지를 정당화하십시오
- 7. UILabel을 다른보기로 업데이트하십시오.
- 8. UILabel을 사라지게하기
- 9. ios : 백그라운드 이미지 위에 uiLabel을 추가하는 방법은 무엇입니까?
- 10. iOS - UIImage와 UILabel을 동일한 줄에 꼬리말로 넣는 방법은 무엇입니까?
- 11. 한 뷰에서 다른 뷰로 uilabel을 드래그 앤 드롭합니다. -iOS
- 12. iOS에서 천막 UILabel을 만드는 방법은 무엇입니까?
- 13. UILabel을 늘리는 방법?
- 14. UILabel을 업데이트하고 삭제 하시겠습니까?
- 15. colorWithPatternImage : UILabel을 사용하는 방법
- 16. -drawRect에서 UILabel을 그리는 방법 :
- 17. UILabel을 동적으로 업데이트하십시오.
- 18. UILabel을 손으로 만지게 만들기
- 19. 높이 UILabel을 0으로 설정하십시오.
- 20. Xcode UILabel을 뒤집습니다.
- 21. UILabel을 새로 고침
- 22. connectionDidReceiveData에서 UILabel을 업데이트하는 중입니다.
- 23. UILabel을 가운데로 회전
- 24. Xcode에서 UILabel을 찾으려면 어떻게해야합니까?
- 25. 어쨌든 CPTSymbol에 UILabel을 넣었습니까?
- 26. 프로그래밍 방식으로 UILabel을 만들려면
- 27. UILabel을 타이머로 업데이트하지 않습니다.
- 28. MKMapKit UILabel을 annoation으로 사용
- 29. UItableView에서 UILabel을 교환하는 중
- 30. NSAttributedString이 UILabel을 변경하지 않습니다.
이 질문은 [너무 광범위] (http://stackoverflow.com/help/on-topic)에
NSMutableParagraphStyle
테스트를 사용하는 것입니다. – jurgemaister@jurgemaister 매우 구체적인 것처럼 보입니다. ibm123은 텍스트보기에서 텍스트를 첨부 된 이미지에서 정당화하는 방법으로 정당화하는 방법을 알고 자합니다. madhu가 한 줄의 코드로 대답 할 수있을만큼 구체적입니다. –
UITextView를 사용해보십시오. 시도하지 않았지만 UILabel에 많은 버그가 있습니다. – Rambatino