在UITextField的居中占位符文本旁边对齐UIImage图标的最佳方法是什么?例如:
我一直试图将此复制一段时间,但似乎无法达到预期的效果。
我已经尝试过使用UITextField的LeftView,但是不能在占位符文本旁边对齐(我想通过更改它的框架向LeftView添加填充但不确定如何我将获得在占位符旁边对齐它所需的值。似乎没有得到占位符文本的坐标。)。我也试图将UITextField子类化,但是我们再次不确定如何获得正确的坐标。
答案 0 :(得分:2)
试试这个
Swift 3.0
txtField.textAlignment = .center
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "searchIcon")
attachment.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
let attachmentStr = NSAttributedString(attachment: attachment)
let myString = NSMutableAttributedString(string: "")
myString.append(attachmentStr)
let myString1 = NSMutableAttributedString(string: "Find a Location (Zip Code or Name)")
myString.append(myString1)
txtField.attributedPlaceholder = myString
<强>目标C 强>
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"searchIcon"];
attachment.bounds = CGRectMake(0, 0, 10, 10);
NSAttributedString *attachmentStr = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:@""];
[myString appendAttributedString:attachmentStr];
NSMutableAttributedString *myString1 = [[NSMutableAttributedString alloc] initWithString:@"Find a Location (Zip Code or Name)"];
[myString appendAttributedString:myString1];
txtField.attributedPlaceholder = myString;