gpt4 book ai didi

ios - drawRect 方法不绘制

转载 作者:行者123 更新时间:2023-11-29 10:49:51 26 4
gpt4 key购买 nike

我一直在使用 UITextField 的子类在我的文本字段末尾绘制一个“m3”字符串,但有些东西不起作用。我知道正在调用该方法,因为我已经使用 NSLog 对其进行了测试,我做错了什么?

我的类(class):

// 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];

}

最佳答案

UITextField 在其自身之上绘制了很多东西。背景图将被所有这些东西掩盖。要对此进行测试,您可以以编程方式删除所有 subview 并查看您的“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"
}

关于ios - drawRect 方法不绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20955167/

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