2013-05-28 5 views
0

주어진보기 너비 크기 (예 : 450, 내가 가지고있는 것은 입니다.)는 rectOftable이 콘텐츠보기의 크기 (너비 & 높이) 인 사각형입니다. 이제 450보다 큰 크기 (예 : 1100)의 문자열을 포함하는 NSAttributedString을 만들었습니다.폭으로 NSAttributedString을 분할하는 방법은 무엇입니까?

내가해야 할 일은이 NSAttributedString을 주어진 너비 (여기서보기의 크기 인 450)와 관련하여 여러 부분 문자열로 나눕니다. 그래서, 여기에 1100/450 = 2 + 1 라인을 표시해야합니다.

현재, 내가 무엇을 할 수 있습니다 만 길이로 NSAttributedString은을 분리 즉 [attrStr attributedSubstringFromRange:NSMakeRange(startIndex, 170)] 경우 170 여기에 현재 길이이지만, 크기가 내가 대신 폭으로 NSAttributedString은 분할하려면 어떻게해야 1100

입니까? 이 문제와 관련하여 아무런 언급이 없습니다.

+0

시작으로 [SO]. 좀 더 가독성있게 만들기 위해 질문 [bit] (http://stackoverflow.com/editing-help)을 원할 수도 있습니다. 형편없는 형식의 질문을 읽고 이해하려고 많은 시간을 할애해야 할 때 도움을 줄 사람이 적습니다. –

답변

0

당신이 묻는 것은 복잡한 문제입니다. 주어진 공간에 들어갈 수있는 것은 글꼴, 글꼴 크기 및 기타 요인에 따라 달라지며 모두 NSAttributedString 길이에 따라 다를 수 있습니다. 이것이 OS X이 모든 것을 처리 할 수있는 클래스를 제공하는 이유입니다. NSTextView, NSText, NSLayoutManager

본문을 직접 깨뜨릴 필요가 있습니까? 그렇지 않으면 제공된 클래스를 사용하십시오. 그렇다면 당신은해야 할 독서가 있습니다!

1

여기가 (예컨대 [를 NSTextView textContainer] 등)를 사용하여 수행 NSTextContainer있어 방법 :

// remember the orginal values 
BOOL widthTracksTextView = [textContainer widthTracksTextView]; 
BOOL heightTracksTextView = [textContainer heightTracksTextView]; 
NSSize containerSize = [textContainer containerSize]; 
CGFloat lineFragmentPadding = [textContainer lineFragmentPadding]; 

// set these as we need them 
[textContainer setWidthTracksTextView:NO]; 
[textContainer setHeightTracksTextView:NO]; 
[textContainer setContainerSize:screenFrame.size]; 
[textContainer setLineFragmentPadding:0.0]; 

// this cause glyph generation and layout 
(void) [textView.layoutManager glyphRangeForTextContainer:textContainer]; 

// return the size that the view would need to be in order to display 
// all the glyphs that are currently laid into the container 
NSRect layoutRect = [textView.layoutManager usedRectForTextContainer:textContainer]; 
layoutRect.size.width = ceil(layoutRect.size.width); 
layoutRect.size.height = ceil(layoutRect.size.height); 

// restore orginal values 
[textContainer setContainerSize:containerSize]; 
[textContainer setWidthTracksTextView:widthTracksTextView]; 
[textContainer setHeightTracksTextView:heightTracksTextView]; 
[textContainer setLineFragmentPadding:lineFragmentPadding]; 
관련 문제