本文介绍了如何在UITextField中将用作leftView的UILabel与textField的文本垂直对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 UILabel
作为 UITextField的
。问题是textField的文本高于标签的。 leftView
I'm using a UILabel
as the leftView
of a UITextField
. The issue is that the textField's text is higher than the label's.
这是我到目前为止使用的代码
This is the code I've used so far
UILabel *startsWith = [[UILabel alloc] init];
startsWith.font = [UIFont systemFontOfSize:14];
startsWith.textColor = [UIColor blackColor];
startsWith.backgroundColor = [UIColor clearColor];
startsWith.text = @"Text";
[startsWith sizeToFit];
self.textField.leftViewMode = UITextFieldViewModeAlways;
self.textField.leftView = startsWith;
我尝试过略微改变标签的框架,但它不起作用......
I've tried slightly changing the label's frame but it didn't work...
如何对齐两个文本?
推荐答案
您可以创建容器视图你在其中定位 UILabel
1px。
You could create a container view in which you position the UILabel
1px up.
UIView * v = [[UIView alloc] init];
v.backgroundColor = [UIColor clearColor];
UILabel *startsWith = [[UILabel alloc] init];
startsWith.font = self.textfield.font;
startsWith.textAlignment = self.textfield.textAlignment;
startsWith.textColor = [UIColor blackColor];
startsWith.backgroundColor = [UIColor clearColor];
startsWith.text = @"Text";
[startsWith sizeToFit];
startsWith.frame = CGRectOffset(startsWith.frame, 0, -1);
v.frame = startsWith.frame;
[v addSubview:startsWith];
self.textfield.leftViewMode = UITextFieldViewModeAlways;
self.textfield.leftView = v;
这篇关于如何在UITextField中将用作leftView的UILabel与textField的文本垂直对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!