gpt4 book ai didi

ios - 如何为 UITextField 实现类似 NSLineBreakByTruncatingHead 的功能?

转载 作者:行者123 更新时间:2023-11-29 10:42:08 25 4
gpt4 key购买 nike

我需要为 UITextField 实现完全类似于 NSLineBreakByTruncatingHead 的功能,如此处所示。假设原文是:

This is the long text that cannot be shown inside a UITextField

我需要它:

...cannot be shown inside a UITextField

但目前我得到的是:

This is the long text that cannot...

只是开头的截断。未为 UITextField 提供 lineBreakMode 属性。我怎样才能实现它?

最佳答案

我采用了解决方案 here 并将其修改为截断字符串的头部而不是尾部。知道它只在字段未被编辑时显示省略号。

注意:此解决方案仅适用于 iOS 7+。要在 iOS 6 中使用,请在 NSString+TruncateToWidth.m 文件中使用 sizeWithFont: 而不是 sizeWithAttributes:
编辑:添加了对 iOS 6 的支持

NSString+TruncateToWidth.h

@interface NSString (TruncateToWidth)
- (NSString*)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font;
@end

NSString+TruncateToWidth.m

#import "NSString+TruncateToWidth.h"

#define ellipsis @"…"

@implementation NSString (TruncateToWidth)

- (NSString*)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font
{
// Create copy that will be the returned result
NSMutableString *truncatedString = [self mutableCopy];

// Make sure string is longer than requested width
if ([self widthWithFont:font] > width)
{
// Accommodate for ellipsis we'll tack on the beginning
width -= [ellipsis widthWithFont:font];

// Get range for first character in string
NSRange range = {0, 1};

// Loop, deleting characters until string fits within width
while ([truncatedString widthWithFont:font] > width)
{
// Delete character at beginning
[truncatedString deleteCharactersInRange:range];
}

// Append ellipsis
[truncatedString replaceCharactersInRange:NSMakeRange(0, 0) withString:ellipsis];
}

return truncatedString;
}

- (CGFloat)widthWithFont:(UIFont *)font
{
if([self respondsToSelector:@selector(sizeWithAttributes:)])
return [self sizeWithAttributes:@{NSFontAttributeName:font}].width;
return [self sizeWithFont:font].width;
}

使用它:

...
// Make sure to import the header file where you want to use it
// assumes instance variable holds your string that populates the field
fieldString = @"abcdefghijklmnopqrstuvwxyz1234567890";
// Size will need to be less than text field's width to account for padding
_myTextField.text = [fieldString stringByTruncatingToWidth:(_myTextField.frame.size.width - 15) withFont:_myTextField.font];
...

// use textFieldShouldBeginEditing to make it animate from the start of the field to the end of the string if you prefer that. I found it a little distracting
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.text = fieldString;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
fieldString = textField.text;
textField.text = [textField.text stringByTruncatingToWidth:(textField.frame.size.width - 15) withFont:textField.font];

return YES;
}

关于ios - 如何为 UITextField 实现类似 NSLineBreakByTruncatingHead 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24032206/

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