我有一个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-------|
|----------------|
|----------------|
+----------------+
为什么我不能创建边缘到边缘排除路径,是否有解决这种困境的方法?
答案 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。
没有滚动,我没有问题,但是我需要此功能。
let path = UIBezierPath(rect: myview.frame)
textView.textContainer.exclusionPaths = [path]
self.textView.addSubview(myview)
func setText( message: String ) {
textView.isScrollEnabled = false
textView.text = message
textView.isScrollEnabled = true
}
这解决了我的问题。希望对您有所帮助。