UITextField:前导截断

发布于 2025-01-13 09:24:51 字数 83 浏览 0 评论 0原文

目前,我在 UITextField 中的文本在尾部被截断(例如“Hello,Wor...”)。我希望从前缘截断文本(例如“...lo,World!”)。

Currently, my text within a UITextField is truncating at the tail (eg. "Hello, Wor..."). I want the to truncate the text from the leading edge (eg. "...lo, World!").

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2025-01-20 09:24:51

您只需更新文本属性(例如 defaultTextAttributes)与 .paragraphStylelineBreakMode of .byTruncatingHead

不过,我不会仅仅替换 .paragraphStyle 。我会获取它,更新其 lineBreakMode,然后用它更新文本属性:

let oldStyle = textField.defaultTextAttributes[.paragraphStyle, default: NSParagraphStyle()] as! NSParagraphStyle
let style = oldStyle.mutableCopy() as! NSMutableParagraphStyle
style.lineBreakMode = .byTruncatingHead
textField.defaultTextAttributes[.paragraphStyle] = style

这里是 .byTruncatingHead.byTruncatingMiddle 和 < code>.byTruncatingTail,分别为:

在此处输入图像描述

You just have to update the text attributes (e.g., defaultTextAttributes) with a .paragraphStyle with a lineBreakMode of .byTruncatingHead.

I would not just replace the .paragraphStyle, though. I would fetch it, update its lineBreakMode, and then update the text attributes with that:

let oldStyle = textField.defaultTextAttributes[.paragraphStyle, default: NSParagraphStyle()] as! NSParagraphStyle
let style = oldStyle.mutableCopy() as! NSMutableParagraphStyle
style.lineBreakMode = .byTruncatingHead
textField.defaultTextAttributes[.paragraphStyle] = style

Here it is with .byTruncatingHead, .byTruncatingMiddle and .byTruncatingTail, respectively:

enter image description here

老娘不死你永远是小三 2025-01-20 09:24:51

我相信将 NSMutableParagraphStyle's lineBreakModeNSMutableAttributedString 一起使用可能会起作用

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

这给出了类似这样的东西,这就是我认为你想要的:

UITextField 前导截断iOS Swift

I believe using NSMutableParagraphStyle's lineBreakMode with NSMutableAttributedString might work

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

This gives something like this, which is what I think you want:

UITextField Leading Truncation iOS Swift

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文