iOS 8에서는 UITextView의 하위보기로 UIImageView를 추가하려고합니다. 여기에 표시된 것과 비슷하지만 이미지 아래의 텍스트가 있습니다.전체 너비 및 텍스트 컨테이너의 상단에 위치하는 경우 UITextView textContainer 제외 경로가 실패합니다.
나는 다른 장치에, 나는 이미지가 다르게 화면의 크기에 따라 위치 수 있기 때문에 제외 경로를 사용하고 싶어.
그러나 제외 경로를 만드는 데 사용 된 CGRect의 Y 출발점이 0이고 textView의 전체 너비를 차지하는 경우 제외가 실패하고 텍스트가 제외 경로 내에 표시되므로 텍스트가 제외 경로에 표시됩니다. 그 스크린 샷에서 볼 수 있듯이 imageView 뒤에 표시됩니다.)
- (void)viewDidLoad {
[super viewDidLoad];
// set up the textView
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UITextView *textView = [[UITextView alloc] initWithFrame:frame];
[textView setFont:[UIFont systemFontOfSize:36.0]];
[self.view addSubview:textView];
textView.text = @"I wish this text appeared below the exclusion rect and not within it.";
// add the photo
CGFloat textViewWidth = textView.frame.size.width;
// uncomment if you want to see that the exclusion path DOES work when not taking up the full width:
// textViewWidth = textViewWidth/2.0;
CGFloat originY = 0.0;
// uncomment if you want to see that the exclusion path DOES work if the Y origin isn't 0
// originY = 54.0;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo_thumbnail"]];
imageView.frame = CGRectMake(0, originY, textViewWidth, textViewWidth);
imageView.alpha = 0.7; // just so you can see that the text is within the exclusion path (behind photo)
[textView addSubview:imageView];
// set the exclusion path (to the same rect as the imageView)
CGRect exclusionRect = [textView convertRect:imageView.bounds fromView:imageView];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:exclusionRect];
textView.textContainer.exclusionPaths = @[exclusionPath];
}
가 나는 또한 NSTextContainer를 서브 클래스와 -lineFragmentRectForProposedRect 방법을 무시했지만, 거기 Y 원점을 조정 :
내가 다음으로는 "단일보기"엑스 코드 템플릿을 사용하여 간단한 응용 프로그램을 구축이를 테스트하려면 어느 쪽도 도움이되지 않는 것 같습니다.사용자 정의 NSTextContainer을 사용하려면, 내가 UITextView가있는 viewDidLoad()에서이 같은 스택 설정 :
다음// set up the textView stack
NSTextStorage *textStorage = [[NSTextStorage alloc] init];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
CGSize containerSize = CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX);
CustomTextContainer *textContainer = [[CustomTextContainer alloc] initWithSize:containerSize];
[layoutManager addTextContainer:textContainer];
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UITextView *textView = [[UITextView alloc] initWithFrame:frame textContainer:textContainer];
[textView setFont:[UIFont systemFontOfSize:36.0]];
[self.view addSubview:textView];
textView.text = @"I wish this text appeared below the exclusion rect and not within it.";
나는이 같은 CustomTextContainer의 Y 원점을 조정 ...하지만이 같은 화려 단지 실패 :
- (CGRect)lineFragmentRectForProposedRect:(CGRect)proposedRect atIndex:(NSUInteger)characterIndex writingDirection:(NSWritingDirection)baseWritingDirection remainingRect:(CGRect *)remainingRect {
CGRect correctedRect = proposedRect;
if (correctedRect.origin.y == 0) {
correctedRect.origin.y += 414.0;
}
correctedRect = [super lineFragmentRectForProposedRect:correctedRect atIndex:characterIndex writingDirection:baseWritingDirection remainingRect:remainingRect];
NSLog(@"origin.y: %f | characterIndex: %lu", correctedRect.origin.y, (unsigned long)characterIndex);
return correctedRect;
}
난이 (내가 뭔가를 분명 누락하지 않는 한) 내가보고해야 애플 버그로 간주 될 수도있을 것 같군요하지만 아무도 해결 방법이 있다면 그것은 매우 감사하겠습니다.
동일한 현상이 발생합니다 (iOS8). 이것은 나에게 애플 버그처럼 보인다. 성공한 해결 방법을 찾은 사람이 있으면 이야기하십시오! –
또 다른 해결 방법은 스토리 보드 레이아웃을 사용하고 다른 화면 크기 클래스에 대해 이미지, 텍스트 및 제약 조건을 다르게 배치하는 것입니다. – Tim
아니요, 버그가 아닙니다. exclusionPaths를 두 번 확인하고 rect가 textView.textContainer에 포함되어 있는지 확인하면됩니다. @GeoffH,'textview.textContainerInset' 및'textView.textContainer.lineFragmentPadding' 및'textView.contentInset'도 검사해야합니다. –