我在 iOS 7 中使用 Text Kit,我发现 NSTextContainer
禁区有很多奇怪之处。
我有两个 View :一个UITextView
和一个简单的可拖动UIView
;随着 UIView
的移动,我从 UIView
的框架(调整到 UITextView
的坐标空间内)创建了一条贝塞尔曲线路径,然后我更新 UITextView
的 NSTextContainer
的 exclusionPaths
数组 - 非常简单。
在第一个屏幕截图中,您可以看到 Text Kit 很好地将文本环绕在一个矩形禁区周围:
但是,当用户将换行符引入 UITextView
时,TextKit 似乎认为禁区在垂直方向上要大得多 - 似乎与换行符创建的空白一样高。贝塞尔曲线路径完全相同,所以这似乎是一个 Text Kit 问题(除非我做错了什么)。
代码:
ViewController.h:
@interface ViewController : UIViewController<UITextViewDelegate>
@property (nonatomic, strong) IBOutlet UITextView *textView;
@property (nonatomic, strong) IBOutlet UIView *dragView;
@end
ViewController.m:
-(void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self.dragView addGestureRecognizer:panRecognizer];
[self updateExclusionZone];
}
-(void)move:(UIPanGestureRecognizer *)pan
{
[self.view bringSubviewToFront:[pan view]];
if ([pan state] == UIGestureRecognizerStateBegan) {
NSLog(@"pan began");
}
self.dragView.center = [pan locationInView:self.view];
[self updateExclusionZone];
if ([pan state] == UIGestureRecognizerStateEnded) {
NSLog(@"pan ended");
}
}
-(void)updateExclusionZone
{
CGRect dragViewFrame = self.dragView.frame;
CGRect exclusionRect = [self.view convertRect:dragViewFrame toView:self.textView];
UIBezierPath *exclusion = [UIBezierPath bezierPathWithRect:exclusionRect];
self.textView.textContainer.exclusionPaths = @[exclusion];
}
有什么想法吗?
最佳答案
我今天遇到了同样的问题。
如果您同时设置editable
和selectable
,似乎会出现此错误。如果只选择一个或没有选择,它将按预期呈现。两者都是默认选中的。
如果您需要这两个选项,只需在代码中设置它们即可。
_textView.textContainer.exclusionPaths = exclusionPaths;
_textView.attributedText = attrString;
_textView.editable = YES;
_textView.selectable = YES;
关于objective-c - iOS 7 UITextView 中的换行符打破了 Text Kit 禁区换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963648/