如何为 UITextField 实现像 NSLineBreakByTruncatingHead 这样的东西? [英] How to achieve something like NSLineBreakByTruncatingHead for UITextField?

查看:120
本文介绍了如何为 UITextField 实现像 NSLineBreakByTruncatingHead 这样的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 UITextField 实现与 NSLineBreakByTruncatingHead 完全一样的东西,如下所示.让我们假设原文是:

I need to achieve something exactly like NSLineBreakByTruncatingHead for UITextField as shown here. Let's assume the original text is:

这是不能在 UITextField 中显示的长文本

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

我需要它:

...不能在 UITextField 中显示

...cannot be shown inside a UITextField

但目前我得到的是:

这是无法...的长文本

This is the long text that cannot...

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

simply the truncation at the beginning. The lineBreakMode property is not given for UITextField. How can I achieve it?

推荐答案

我接受了解决方案 here 并修改它以截断字符串的头部而不是尾部.知道它只在未编辑字段时显示省略号.

I took the solution here and modified it to truncate the head of a string instead of the tail. Know that it only shows the ellipsis when the field is not being edited.

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

NOTE: This solution is for iOS 7+ only. To use in iOS 6, use sizeWithFont: instead of sizeWithAttributes: in the NSString+TruncateToWidth.m file.
Added support for 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;
}

这篇关于如何为 UITextField 实现像 NSLineBreakByTruncatingHead 这样的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆