2010-07-12 88 views
42

我有一個用於文本編輯的UITextView。默認情況下,它在文本週圍有一個小的邊距。我想增加幾個像素的邊緣。如何在UITextView中設置邊距(填充)?

contentInset屬性爲我提供邊距,但不會更改文本「包裝寬度」。文本以相同的寬度包裹,額外的「邊距」只會導致視圖水平滾動。

有沒有辦法讓一個特定寬度的UITextView顯示更窄的「包裹寬度」的文本?

回答

7

你可以使用一個較小的UITextView,並在後臺放置一個UIView來模擬填充。

+----------+ 
|   | <-- UIView (as background) 
| +--+ | 
| | | <---- UITextView 
| | | | 
| +--+ | 
|   | 
+----------+ 
+1

呀有用的,這就是我現在正在做什麼。對邊緣工作沒問題,但最高邊距有問題。從用戶的角度來看,文本在滾動時在頂部被「截斷」,因爲文本只在UITextView中呈現。 – strawtarget 2010-07-14 16:31:30

+5

而你無法以這種方式正確顯示垂直滾動條。 – an0 2012-01-04 14:16:11

+0

設置'textView.clipsToBounds = NO'將會「修復」頂部的切割,排序。當然,這將無助於正確顯示垂直滾動條。 – villapossu 2013-09-17 00:11:33

1

這是爲我工作。更改textView contentInset和框架插入/位置以獲得正確的填充和自動換行。然後更改textView邊界以防止水平滾動。每次選擇並更改文本時,您還需要重置填充。

- (void)setPadding 
{ 
    UIEdgeInsets padding = UIEdgeInsetsMake(30, 20, 15, 50); 

    textView.contentInset = padding; 

    CGRect frame = textView.frame; 

    // must change frame before bounds because the text wrap is reformatted based on frame, don't include the top and bottom insets 
    CGRect insetFrame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(0, padding.left, 0, padding.right)); 

    // offset frame back to original x 
    CGFloat offsetX = frame.origin.x - (insetFrame.origin.x - (padding.left + padding.right)/2); 
    insetFrame = CGRectApplyAffineTransform(insetFrame, CGAffineTransformMakeTranslation(offsetX, 0)); 

    textView.frame = insetFrame; 

    textView.bounds = UIEdgeInsetsInsetRect(textView.bounds, UIEdgeInsetsMake(0, -padding.left, 0, -padding.right)); 

    [textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO]; 
} 

-(void)textViewDidChangeSelection:(UITextView *)textView 
{ 
    [self setPadding]; 
} 

-(void)textViewDidChange:(UITextView *)textView 
{ 
    [self setPadding]; 
} 
+0

@Nate這不適合我。文本視圖仍然水平滾動,並且內容插入在文本視圖框架之外刷新 – GoGreen 2014-03-24 06:44:33

34

與此擺弄周圍了一會兒後,我發現了另一個解決方案,如果你只開髮針對iOS 6與contentInset設置在頂部和底部邊距:

textView = [[UITextView alloc] init]; 
textView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0); 

爲左右邊距不會立即添加您的純文本,而是使用NSAttributedString而不是正確設置左和右縮進以及NSMutableParagraphStyle

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyle.headIndent = 20.0; 
paragraphStyle.firstLineHeadIndent = 20.0; 
paragraphStyle.tailIndent = -20.0; 

NSDictionary *attrsDictionary = @{NSFontAttributeName: [UIFont fontWithName:@"TrebuchetMS" size:12.0], NSParagraphStyleAttributeName: paragraphStyle}; 
textView.attributedText = [[NSAttributedString alloc] initWithString:myText attributes:attrsDictionary]; 

這讓你與你的文字一個UITextView(在我的情況下,從可變會將myText)與20個像素填充,妥善滾動。

89

從iOS的7起,您可以使用textContainerInset屬性:

Objective-C的

textView.textContainerInset = UIEdgeInsetsMake(0, 20, 0, 20); 

斯威夫特

textView.textContainerInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) 
8

試試這個

UIBezierPath* aObjBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 20)]; 
txtView.textContainer.exclusionPaths = @[aObjBezierPath]; 
0

感謝您的建議。 我開發的MacOS/OSX應用程序時,有這個問題,結束了這一點:

textView.textContainerInset = NSSize(width: 20, height: 20); //Sets margins