跳过正文
  1. Posts/

计算文字长度

··2 分钟

2018.7.9

官方文档

方法定义
#

- (NSRect)boundingRectWithSize:(NSSize)size 
                       options:(NSStringDrawingOptions)options 
                    attributes:(NSDictionary<NSAttributedStringKey, id> *)attributes 
                       context:(NSStringDrawingContext *)context;

参数定义
#

size
#

绘制的限制size,计算出来的值不会超过这个大小。

options
#

一些配置项。定义如下:

typedef NS_OPTIONS(NSInteger, NSStringDrawingOptions) {
    NSStringDrawingUsesLineFragmentOrigin = 1 << 0, // The specified origin is the line fragment origin, not the base line origin
    NSStringDrawingUsesFontLeading = 1 << 1, // Uses the font leading for calculating line heights
    NSStringDrawingUsesDeviceMetrics = 1 << 3, // Uses image glyph bounds instead of typographic bounds
    NSStringDrawingTruncatesLastVisibleLine NS_ENUM_AVAILABLE(10_5, 6_0) = 1 << 5, // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.

} NS_ENUM_AVAILABLE(10_0, 6_0);

实际测试使用NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading可以满足需求,NSStringDrawingUsesLineFragmentOrigin是必须的,NSStringDrawingUsesFontLeading加不加在测试的时候没发现区别,但是在stackoverflow相关讨论里加上了,留个坑,后面知道为什么了来补充吧。

attributes
#

字体

context
#

上下文

注意事项
#

  1. 如果是多行文字,options要加上NSStringDrawingUsesLineFragmentOrigin
  2. 返回的值是小数,需要调用ceil向上取整
  3. 得到的宽度可能比实际宽

代码示例
#

+ (CGSize)getTextLabelSize:(NSString *)message {
    if ([message length] > 0) {
        // 文本框的最大宽度
        float maxWidth = 200;
        CGRect textRect = [message
                           boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)
                           options:(NSStringDrawingUsesLineFragmentOrigin |
                                    NSStringDrawingUsesFontLeading)
                           attributes:@{
                                        NSFontAttributeName :
                                            [UIFont systemFontOfSize:16]
                                        }
                           context:nil];
        textRect.size.height = ceilf(textRect.size.height);
        textRect.size.width = ceilf(textRect.size.width);
        return CGSizeMake(textRect.size.width, textRect.size.height);
    } else {
        return CGSizeZero;
    }
}

相关文章

UITextField控制输入长度

··1 分钟
2018.7.6 有些时候会有控制输入框文字长度的需求,记录一个简单的思路。 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (string.length == 0) { return YES; } NSInteger limit = 15; // 文本的最大长度 NSString *newStr = [textField.text stringByAppendingString:string]; // 修改之后的新字符串 NSInteger newStrLength = newStr.length; newStrLength -= [textField textInRange:[textField markedTextRange]].length; // 去掉高亮内容,输入中文拼音的情况 if (newStrLength > limit) { // 处理composed character, 比如emoji NSString *tempStr = [newStr substringWithRange:[newStr rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, limit)]]; textField.text = tempStr; return NO; } return YES; } 有两个坑注意下:

property or synthsize

··1 分钟
记录时间:2018.7.5 @property (nonatomic, retain) NSObject *var; 生成var的set、get方法的方法声明 生成var的set、get方法的实现(早期版本编译器不生成) 生成成员变量_var(早期版本编译器不生成) @synthsize var = _var

Tagged Pointer小记

··3 分钟
记录时间:2018.7.4 本文使用的测试环境是arm64架构真机 为了探究Tagged Pointer本质,可以查看runtime源码,主要看文件objc-internal.h。

URLWithString return nil

··1 分钟
记录时间:2018.7.3 问题描述 # 在使用URLWithString生成NSURL时,如果出现中文,会导致返回的NSURL为nil。代码如下:

iOS消息转发小记

··2 分钟
消息转发流程图 如果类接收到无法处理的消息,会触发消息转发机制,一共有三个步骤,接受者在每一步中均有机会处理消息。步骤越往后,处理消息的代价就越大,所以最好再第一步就处理完。