目前,我在 UITextField 中的文本在尾部被截斷(例如“Hello, Wor...”)。我希望從前沿截斷文本(例如“...lo,World!”)。
uj5u.com熱心網友回復:
我相信使用NSMutableParagraphStyle's lineBreakModewithNSMutableAttributedString可能有效
let longString = "Hello World"
// Just for demo purposes, you can use auto layout etc
let textfield = UITextField(frame: CGRect(x: 40, y: 200, width: 30, height: 100))
textfield.layer.borderWidth = 2
textfield.layer.borderColor = UIColor.gray.cgColor
// Use a paragraph style to define the truncation method
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingHead
// Create an attributed string and apply the paragraph style
let attributedString = NSMutableAttributedString(string: longString)
attributedString.addAttribute(.paragraphStyle,
value: paragraphStyle,
range:NSMakeRange(0, attributedString.length))
// Set the text field's attributed text as the attributed string
textfield.attributedText = attributedString
這給出了這樣的東西,這就是我認為你想要的:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/440610.html
標籤:IOS 迅速 uikit uitextfield
