gpt4 book ai didi

ios - 如何在 UITextField 中的文本上绘制边框

转载 作者:行者123 更新时间:2023-11-29 01:50:53 25 4
gpt4 key购买 nike

我希望能够在 Swift 中的 UITextField 中围绕文本绘制边框,这可能吗?如果可以,它是如何完成的?

最佳答案

尝试使用 NSAttributedString,它可以让您自定义文本。我建议使用一个名为 Attributed String Creator 的有用程序,您可以在应用商店中找到它。这使您可以在编辑器中创建文本并复制和粘贴制作该文本所需的代码。

编辑:

如果你不想下载程序,这里是我制作的一些黑色轮廓文本的片段:

    // Create the attributed string
var myString = NSMutableAttributedString(string:"Example Text")

// Declare the fonts
let myStringFont1 = UIFont(name:"Helvetica", size:14.0)

// Declare the paragraph styles
var myStringParaStyle1 = NSMutableParagraphStyle()
myStringParaStyle1.alignment = NSTextAlignment.Natural
myStringParaStyle1.tabStops = [NSTextTab(textAlignment: NSTextAlignment.Left, location: 28.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 56.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 84.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 112.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 140.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 168.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 196.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 224.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 252.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 280.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 308.000000, options: nil), NSTextTab(textAlignment: NSTextAlignment.Left, location: 336.000000, options: nil), ]


// Create the attributes and add them to the string
myString.addAttribute(NSFontAttributeName, value:myStringFont1!, range:NSMakeRange(0,12))
myString.addAttribute(NSStrokeWidthAttributeName, value:3, range:NSMakeRange(0,12))
myString.addAttribute(NSParagraphStyleAttributeName, value:myStringParaStyle1, range:NSMakeRange(0,12))

编辑 2:

将所有内容放入此文本字段委托(delegate)函数中,并将用于 textField.text 的字符串更改为:

func textFieldDidEndEditing(textField: UITextField) {
//Put all that inside here
}

关于ios - 如何在 UITextField 中的文本上绘制边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31433545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com