Mil*_*loo 2 uitextview nsattributedstring nsmutablearray ios swift
当我点击加粗按钮时,我有一个文本编辑器
我可以用粗体字写这是代码
txtEditor.typingAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.black/, NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: (txtEditor.font?.pointSize)!)]
Run Code Online (Sandbox Code Playgroud)
我想在“中心文本中心对齐”中单击一个按钮(我写的行)时需要一个按钮,而不是其他行
这是代码
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
Run Code Online (Sandbox Code Playgroud)
但是在将如何在UITextview中添加这2行代码以输入居中对齐之后呢?
尝试这个。
extension UITextView {
func centerText() {
self.textAlignment = .center
let fittingSize = CGSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude)
let size = sizeThatFits(fittingSize)
let topOffset = (bounds.size.height - size.height * zoomScale) / 2
let positiveTopOffset = max(1, topOffset)
contentOffset.y = -positiveTopOffset
}
}
Run Code Online (Sandbox Code Playgroud)
这样使用
@IBAction func btnClick(_ sender: Any) {
textView.centerText()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1974 次 |
| 最近记录: |