Ios 辞职UITextField';第一反应者
我在自定义的Ios 辞职UITextField';第一反应者,ios,objective-c,uikit,uitextfield,nsattributedstring,Ios,Objective C,Uikit,Uitextfield,Nsattributedstring,我在自定义的UITableViewCell中有一个UITextField,我想从头部而不是尾部截断它的文本 我正在awakeFromNib中设置换行模式: - (void)awakeFromNib { [super awakeFromNib]; NSMutableDictionary* textAttributes = [self.textField.defaultTextAttributes mutableCopy]; NSMutableParagraphStyle* parag
UITableViewCell
中有一个UITextField
,我想从头部而不是尾部截断它的文本
我正在awakeFromNib
中设置换行模式:
- (void)awakeFromNib
{
[super awakeFromNib];
NSMutableDictionary* textAttributes = [self.textField.defaultTextAttributes mutableCopy];
NSMutableParagraphStyle* paragraphStyle = [self.textField.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingHead;
[textAttributes setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
[textAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
self.textField.defaultTextAttributes = textAttributes;
}
设置时,保留文本字段(重新指定第一响应者)似乎会导致使用nslinebreakbytruncingtrail
更改发生在textfieldbydendediting:
和textfieldbydendediting:
之间:当我在两种方法中设置断点时,第一种方法中的换行模式是NSLineBreakByTruncatingHead
但第二种方法中的换行模式是NSLineBreakByTruncatingTail
有没有一种方法可以设置断线模式并使其保持不变?我知道,这个问题并不新鲜,但我从几个小时以来就一直在努力解决这个问题,最后我发现它工作的唯一方法是以下(希望它能帮助其他人解决这个问题) 覆盖
UITextField
的drawTextInRect
方法,并在代码中的其他位置设置defaulttexttributes
(我在自定义UITextField
子类中的init
方法中执行此操作):
。。。以及其他代码:
// get defaultTextAttributes of the TextField
NSMutableDictionary* textAttributes = [self.defaultTextAttributes mutableCopy];
// get default paragraph style from the defaultTextAttributes
NSMutableParagraphStyle* paragraphStyle = [textAttributes[NSParagraphStyleAttributeName] mutableCopy];
// change the lineBreakMode as desired
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingHead;
// put changed paragraphStyle into textAttributes
[textAttributes setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
// set the defaultTextAttributes of the textField
self.defaultTextAttributes = textAttributes;
我知道,这个问题并不新鲜,但我从几个小时以来就一直在努力解决这个问题,最后我发现唯一可行的方法是以下几点(希望它能帮助其他人解决这个问题) 覆盖
UITextField
的drawTextInRect
方法,并在代码中的其他位置设置defaulttexttributes
(我在自定义UITextField
子类中的init
方法中执行此操作):
。。。以及其他代码:
// get defaultTextAttributes of the TextField
NSMutableDictionary* textAttributes = [self.defaultTextAttributes mutableCopy];
// get default paragraph style from the defaultTextAttributes
NSMutableParagraphStyle* paragraphStyle = [textAttributes[NSParagraphStyleAttributeName] mutableCopy];
// change the lineBreakMode as desired
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingHead;
// put changed paragraphStyle into textAttributes
[textAttributes setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
// set the defaultTextAttributes of the textField
self.defaultTextAttributes = textAttributes;