Swift iOS TextView将某些文本居中并右对齐



我有一个文本视图,我希望它类似于iphone notes应用程序。当用户点击中心对齐按钮时,我只希望该线上的任何东西都是中心对齐的。我有这个代码,但当我使用它时,它会将整个文本视图文本设置为居中对齐,我只希望我所在的行居中对齐:

@IBAction func centerAlignmentBtn(_ sender: Any) {
self.textView.textAlignment = .center
}

您可以设置对齐方式,然后设置文本写入方向。就像它一样。

self.textView.textAlignment = .justified //set text alignment center and justified
self.textView.makeTextWritingDirectionRightToLeft(true)//if you want set writing direction right to left
self.textView.makeTextWritingDirectionLeftToRight(true)//if you want set writing direction left to right

最新更新