具有边到边排除路径的UiTextView会导致所有文本消失

时间:2014-02-25 08:42:41

标签: ios ios7

我有一个UITextView,我想为其添加一个自定义UIView,并使文字在上下流动:

+----------------+
|------txt-------|
|                |
|     UIView     |   
|                |
|------txt-------|
|----------------|
|----------------|
+----------------+

我尝试通过添加UITextView宽度和UIView高度的排除路径来实现此目的:

CGRect exclusionFrame=CGRectMake(0, 
                                CGRectGetMinY(element.frame),
                                self.textView.textContainer.size.width,
                                CGRectGetHeight(element.frame)); 
self.textView.textContainer.exclusionPaths=@[[UIBezierPath bezierPathWithRect:exclusionFrame]];

但由于某种原因,如果排除路径的宽度等于textView的宽度,则UIView(aka element)下面的所有文本都会消失。当我将排除路径的宽度减少一个时,文本会在UiView旁边显示一个单字母列,如下所示:

+----------------+
|------txt-------|
|a               |
|d     UIView    |   
|e               |
|------txt-------|
|----------------|
|----------------|
+----------------+

为什么我不能创建边缘到边缘排除路径,是否有解决这种困境的方法?

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,幸运的是我只是放入\ n \ r \ n \ r来解决它。作弊我知道,但它的确有效。

答案 1 :(得分:0)

最终成为故事板。我只从视图中取出UiTextView并以编程方式初始化它似乎解决了这个问题:

-(void) viewDidLoad{
    self.textView=[[UITextView alloc] init];
    self.textView.translatesAutoresizingMaskIntoConstraints=NO;
    [self.view addSubview:self.textView];

    NSDictionary* views = @{@"textView": self.textView};
    NSArray* vContrains=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[textView]-50-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:views];
    NSArray* hContrains=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[textView]-50-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:views];
    [self.view addConstraints:vContrains];
    [self.view addConstraints:hContrains];

} 

答案 2 :(得分:0)

textView.textContainer.lineFragmentPadding = 0 textView.textContainerInset = UIEdgeInsetsZero textView.contentInset = UIEdgeInsetsZero
可能会有所帮助。

答案 3 :(得分:0)

我用swift解决了类似的问题,该解决方案是在使用switch和objective-c示例读取不同的线程之后得出的。

我有一个Scrolling Enabled = true的UITextView。 没有滚动,我没有问题,但是我需要此功能。

  1. 我使用了标准设置,并启用了滚动= true
let path = UIBezierPath(rect: myview.frame)
textView.textContainer.exclusionPaths = [path]
self.textView.addSubview(myview)
  1. 当我更改UITextView文本时,我禁用了滚动
func setText( message: String ) {
    textView.isScrollEnabled = false
    textView.text = message
    textView.isScrollEnabled = true
}

这解决了我的问题。希望对您有所帮助。