2014-02-10 8 views

답변

7

내가 아는 한 UITextView 세로 정렬 방법은 없습니다. 그러나하여 contentOffset 당신이 수직 중심으로 얻을 수있는 텍스트 업데이트하여 : 당신은 수평 및 수직 정렬을 변경하고자 할 때 당신은 어떻게 당신이 무엇을 의미하는지에 대한 컨텍스트를 제공해야

[textView setTextAlignment:NSTextAlignmentCenter]; 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [textView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [textView removeObserver:self forKeyPath:@"contentSize"]; 
} 

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    UITextView *tv = object; 
    CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0; 
    topCorrect = (topCorrect < 0.0 ? 0.0 : topCorrect); 
    tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect}; 
} 
+0

이 코드는 어디에 있습니까? (ViewController.m?) – Cindeselia

+0

@Cindeselia 코드가 현재 작성된 방식으로, 텍스트보기가있는 위치에 코드를 추가해야합니다. – Gad

+0

contentOffset을 변경하는 것이 텍스트보기의 "선택 가능"플래그가 선택되어있는 경우에만 작동하는 것으로 보입니다. – Maiaux

0

,

을 UITextview가있다 컨테이너 및 다른 UITextview/Imageview 등과 정렬 할 수있는 프레임 속성을 포함 할 수 있습니다. frame 속성의 원점과 크기는보기를 맞추기 위해 수정할 수 있습니다.

UITextview 내의 텍스트에는 NSTextAlignmentLeft, NSTextAlignmentJustified로 설정할 수있는 textalignment라는 속성이 있습니다. 제안 된대로 컨테이너의 내용 오프셋 속성을 조정할 수도 있지만 프레임 조정이 더 간단합니다. 스위프트와

-4
self.qtyField.textAlignment = NSTextAlignmentCenter; 
5

수직으로 정렬 중간 :있는 viewDidLoad에서

:보기 컨트롤러에서 그리고 다른 곳

textField.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil) 

:에 textField가 실제까지 연결되어

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) { 

    var topCorrect : CGFloat = (textField.frame.height - textField.contentSize.height); 
    topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect/2 
    textField.contentOffset = CGPoint(x: 0, y: -topCorrect) 

} 

텍스트 필드입니다.

+0

제대로 작동하지만 여하튼 unwind segue로 작동하지 않습니다 ... 어떤 이유입니까? – SKYnine

24

이것은 신속한 솔루션이며 내용 삽입 및 오프셋이 약간 변경되었으므로 이제는 다릅니다. 애플은 컨텐츠 오프셋 세트이 약간 수정 된 솔루션은 이제 대신 오프셋의 콘텐츠 삽입에 상단을 설정하는 데 필요한 작동하는 방법을 변경 6.

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    textView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil) 
} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 
    textView.removeObserver(self, forKeyPath: "contentSize") 
} 

이 솔루션은 엑스 코드 7 베타 버전에서 작동합니다.

/// Force the text in a UITextView to always center itself. 
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { 
    let textView = object as! UITextView 
    var topCorrect = (textView.bounds.size.height - textView.contentSize.height * textView.zoomScale)/2 
    topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect; 
    textView.contentInset.top = topCorrect 
} 
+1

IOS 9/Swift 2 덕분에 나에게 큰 도움이되었습니다! – Kitson

+0

굉장 .. 나를 위해 일했습니다. 거의 2 시간을 낭비했다. –

+0

감사합니다, 저를 위해 일했습니다 내 시간을 절약했습니다 –

관련 문제