如何在UITextView中水平和垂直alignment文本?
如何在UITextView中水平和垂直alignment文本? 我想alignmentUITextView中的文本与水平alignment和垂直alignment。 有没有自定义的方法? 请帮我解决…..
据我所知,没有内置的UITextView
垂直alignment的方法。 但是,通过更新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}; }
这是一个Swift解决scheme,现在不同了,因为内容插入和偏移量已经稍微改变了。 这个解决scheme在Xcode 7 beta 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") }
苹果已经改变了内容偏移和插入的工作方式,现在需要稍微修改的解决scheme来设置内容插入的顶部而不是偏移量。
/// 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 }
垂直alignment中间与斯威夫特:
在viewDidLoad中:
textField.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
然后在你的视图控制器的其他地方:
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) }
其中textField连接到视图中的实际文本字段。
你应该提供你想要改变水平和垂直alignment方式的内容,
UITextview有一个容器和一个框架属性,您可以与另一个UITextview / Imageview等alignment框架属性有一个原点和大小,您可以修改以调整您的意见。
UITextview中的文本有一个名为textalignment的属性,您可以将其设置为NSTextAlignmentLeft,NSTextAlignmentJustified,如上所示,您也可以调整容器的内容偏移属性,但是我发现调整框架更直截了当。
self.qtyField.textAlignment = NSTextAlignmentCenter;
- 如何解决“自动合成属性myVar”的警告将使用合成的实例variables“_myVar”,而不是现有的实例variables“myVar”“?
- 如何parsing数组的JSON数组在Swift中
- 如何使用Apache Cordova 3.4.0在本地存储文件
- Firebase – 构build数据库的正确方法
- Swift 3:DispatchQueue.main.async {}和DispatcQueue.main.async之间的区别(execute:{})?
- RestKit – 无法显示JSON数据到我的tableView
- iOS开发技巧中的潜水。
- Apple MDM OTA – 在配置文件中嵌入SCEP与PKCS12的身份证书
- Swift比较string选项与非选项