UITextView의 NSAttributedString에 글꼴을 설정하면 줄 간격이 무시됩니다.
iOS 6에서 속성 문자열을 UITextView로 설정하려고합니다. 문제는 속성 문자열에 글꼴 속성을 설정하려고하면 줄 간격이 무시된다는 것입니다. 그러나 글꼴을 설정하지 않고 기본 글꼴을 사용하면 줄 간격이 작동합니다.
NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.minimumLineHeight = 50;
// setting the font below makes line spacing become ignored
[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
mainTextView.attributedText = attrString;
무슨 일인지 아십니까?
UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"strigil" attributes:attrsDictionary];
업데이트 : addAttribute:
내 앱에서 방법 을 사용하려고했지만 iOS 6 시뮬레이터에서 작동하지 않는 것 같습니다.
NSLog(@"%@", textView.attributedText);
로그에 올바르게 추가 된 속성이 표시되는 것 같지만 iOS 시뮬레이터의보기가 속성과 함께 표시되지 않았습니다.
NSAttributedString 과도 싸우고 있었기 때문에 질문을 찾았습니다. 나를 위해 beginEditing
및 endEditing
메서드는 Attributed String 변경에 명시된 것처럼 트릭을 수행했습니다 . 그 외에도 lineSpacing은 paragraphStyle에 설정됩니다 setLineSpacing
.
따라서 코드를 다음과 같이 변경해 볼 수 있습니다.
NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineSpacing:20] // Or whatever (positive) value you like...
[attrSting beginEditing];
[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
[attrString endEditing];
mainTextView.attributedText = attrString;
이 정확한 코드를 테스트하지는 않았지만 btw는 거의 동일하게 보입니다.
편집하다:
한편, 나는 그것을 테스트했고, 내가 틀렸다면 나를 바로 잡았다. - beginEditing
그리고 - endEditing
호출은 상당히 중요한 것 같다.
iOS 6에는 글꼴이 설정 될 때 줄 높이가 무시되는 버그가있었습니다. Radar : UITextView Ignores Minimum / Maximum Line Height in Attributed String에서 NSParagraphStyle 줄 간격 무시 및 더 긴 버그 분석에 대한 답변을 참조하십시오 .
//For proper line spacing
NSString *text1 = @"Hello";
NSString *text2 = @"\nWorld";
UIFont *text1Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:10];
NSMutableAttributedString *attributedString1 =
[[NSMutableAttributedString alloc] initWithString:text1 attributes:@{ NSFontAttributeName : text1Font }];
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setAlignment:NSTextAlignmentCenter];
[paragraphStyle1 setLineSpacing:4];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attributedString1 length])];
UIFont *text2Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16];
NSMutableAttributedString *attributedString2 =
[[NSMutableAttributedString alloc] initWithString:text2 attributes:@{NSFontAttributeName : text2Font }];
NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle2 setLineSpacing:4];
[paragraphStyle2 setAlignment:NSTextAlignmentCenter];
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle2 range:NSMakeRange(0, [attributedString2 length])];
[attributedString1 appendAttributedString:attributedString2];
이 예제 를 사용 하고 다음과 같이 구현을 변경할 수 있습니다 .
[self enumerateAttribute:NSParagraphStyleAttributeName
inRange:NSMakeRange(0, self.length)
options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
//add your specific settings for paragraph
//...
//...
[self removeAttribute:NSParagraphStyleAttributeName range:range];
[self addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
}];
'IT TIP' 카테고리의 다른 글
Javascript로 jQuery로드 및 jQuery 사용 (0) | 2020.10.26 |
---|---|
데이터베이스의 카디널리티는 무엇입니까? (0) | 2020.10.26 |
테두리의 두께를 백분율로 설정하는 방법은 무엇입니까? (0) | 2020.10.26 |
각도 2 정렬 및 필터 (0) | 2020.10.26 |
복사 또는 rsync 명령 (0) | 2020.10.26 |