gpt4 book ai didi

ios - 将 C 转换为 Swift : Add magnifying glass icon to UITextField

转载 作者:可可西里 更新时间:2023-10-31 23:59:07 26 4
gpt4 key购买 nike

如何在 UITextField 的左侧添加放大镜图标?

我找到了类似问题的答案 here但我无法将其转换为 swift。

答案:

So, here's the code with the unicode character:

UILabel *magnifyingGlass = [[UILabel alloc] init];
[magnifyingGlass setText:[[NSString alloc] initWithUTF8String:"\xF0\x9F\x94\x8D"]];
[magnifyingGlass sizeToFit];

[textField setLeftView:magnifyingGlass];
[textField setLeftViewMode:UITextFieldViewModeAlways];

Edit: For plain look that fits iOS 7 style, add Unicode variation selector \U000025B6.

我的代码:

let searchIconView = UILabel()
// Doesn't work: searchIconView.text = NSString.init(UTF8String: "\xF0\x9F\x94\x8D")
searchIconView.sizeToFit()
searchTextField.leftView = searchIconView
searchTextField.leftViewMode = .Always

最佳答案

您是否有特定原因尝试将图标添加为 UTF8String?如果你有一个放大镜图标作为 PNG 图片,你可以像这样使用 UITextField 的“leftView”属性;

let imageView = UIImageView()
let magnifyingGlassImage = UIImage(named: "magnifyingGlass")
imageView.image = magnifyingGlassImage
//arrange the frame according to your textfield height and image aspect
imageView.frame = CGRect(x: 0, y: 5, width: 45, height: 20)
imageView.contentMode = .ScaleAspectFit
txtField.leftViewMode = .Always
txtField.leftView = imageView

关于ios - 将 C 转换为 Swift : Add magnifying glass icon to UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35056705/

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