我正在使用UITextField的子类在文本字段的末尾绘制一个“m3”字符串,但有些东西不起作用。我知道这个方法正在调用,因为我已经使用NSLog测试了它,我做错了什么?
我的课程:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
NSString *strM3 = [[NSString alloc] initWithFormat:@" m³"];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text alignment
paragraphStyle.alignment = NSTextAlignmentRight;
NSDictionary *attributes = @{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0],
NSParagraphStyleAttributeName:paragraphStyle};
[strM3 drawInRect:rect withAttributes:attributes];
}
答案 0 :(得分:2)
UITextField有很多东西在它自己的上面绘制。背景图将被所有这些事情掩盖。要对此进行测试,您可以以编程方式删除所有子视图,并查看是否显示“m3”。
for(UIView *subview in textField.subviews) [subview removeFromSuperview];
我认为您必须设置默认文本,然后在输入新字符时修改输入文本。设置UITextViewDelegate并重载函数shouldChangeCharactersInRange
:
- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
//Do some string manipulation in here to remove the "m3", then add the user's input, then replace the "m3"
}