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;