{"id":299,"date":"2010-05-25T15:36:39","date_gmt":"2010-05-25T07:36:39","guid":{"rendered":"http:\/\/www.xujiwei.com\/blog\/?p=299"},"modified":"2011-05-29T12:28:04","modified_gmt":"2011-05-29T04:28:04","slug":"limit-text-length-of-nstextfield","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2010\/05\/limit-text-length-of-nstextfield\/","title":{"rendered":"[Cocoa\u5b66\u4e60] \u9650\u5236 NSTextField \u4e2d\u8f93\u5165\u6587\u672c\u7684\u957f\u5ea6"},"content":{"rendered":"<h4>\u9700\u6c42<\/h4>\n<p>NSTextField \u4f5c\u4e3a\u4e00\u4e2a\u5e38\u7528\u7684\u8f93\u5165\u63a7\u4ef6\uff0c\u6709\u4e9b\u5728\u4f7f\u7528\u65f6\u9700\u8981\u53bb\u9650\u5236\u5176\u4e2d\u53ef\u4ee5\u8f93\u5165\u6587\u672c\u7684\u957f\u5ea6\uff0c\u800c NSTextField \u672c\u8eab\u5e76\u6ca1\u6709\u63d0\u4f9b\u8fd9\u4e2a\u529f\u80fd\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u81ea\u5df1\u53bb\u60f3\u529e\u6cd5\u6765\u5b9e\u73b0\u4e86\u3002<\/p>\n<h4>\u65b9\u6cd5<\/h4>\n<p>\u5728\u7f51\u4e0a\u627e\u4e86\u4e00\u4e0b\uff0c\u53d1\u73b0\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f NSFormatter \u5b9e\u73b0\u4e00\u4e2a\u5b50\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u53c2\u8003\u4e86\u4e00\u4e0b\u627e\u5230\u7684\u4ee3\u7801<sup><a href=\"#ref-1\">[1]<\/a><\/sup>\uff0c\u518d\u5c06\u4e3b\u8981\u7684 isPartialStringValid \u65b9\u6cd5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u6539\u4e86\u4e00\u4e0b\u3002<\/p>\n<pre lang=\"objc\">\r\n\r\n- (BOOL)isPartialStringValid:(NSString **)partialStringPtr\r\n       proposedSelectedRange:(NSRangePointer)proposedSelRangePtr\r\n              originalString:(NSString *)origString\r\n       originalSelectedRange:(NSRange)origSelRange\r\n            errorDescription:(NSString **)error \r\n{\r\n    int size = [*partialStringPtr length];\r\n    if ( size > maxLength ) {\r\n        if (origSelRange.location == [origString length]) {\r\n            \/\/ \u5982\u679c\u4fee\u6539\u7684\u4f4d\u7f6e\u5728\u539f\u6765\u5b57\u7b26\u4e32\u7684\u6700\u540e\uff0c\u5219\u4e0d\u505a\u4fee\u6539\uff0c\u53ea\u662f\u62d2\u7edd\u5185\u5bb9\u4fee\u6539\r\n        } else {\r\n            \/\/ \u5982\u679c\u4fee\u6539\u7684\u4f4d\u7f6e\u5728\u539f\u6765\u5b57\u7b26\u4e32\u7684\u4e2d\u95f4\uff0c\u5c31\u6839\u636e\u5269\u4f59\u7684\u53ef\u7528\u7684\u957f\u5ea6\u628a\u65b0\u589e\u52a0\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u622a\u53d6\r\n            int preLen = origSelRange.location + (maxLength - [origString length]) + origSelRange.length;\r\n            *partialStringPtr = [NSString stringWithFormat:@\"%@%@\",\r\n                                 [*partialStringPtr substringToIndex:preLen],\r\n                                 [origString substringFromIndex:origSelRange.location+origSelRange.length]];\r\n            \r\n            (*proposedSelRangePtr).location = preLen;\r\n        }\r\n        \r\n        return NO;\r\n    }\r\n    return YES;\r\n}\r\n<\/pre>\n<h4>\u53c2\u8003\u8d44\u6599<\/h4>\n<ul>\n<li id=\"ref-1\"><a href=\"http:\/\/stackoverflow.com\/questions\/827014\/how-to-limit-nstextfield-text-length-and-keep-it-always-upper-case\">How to limit NSTextField text length and keep it always upper case?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 NSTextField \u4f5c\u4e3a\u4e00\u4e2a\u5e38\u7528\u7684\u8f93\u5165\u63a7\u4ef6\uff0c\u6709\u4e9b\u5728\u4f7f\u7528\u65f6\u9700\u8981\u53bb\u9650\u5236\u5176\u4e2d &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2010\/05\/limit-text-length-of-nstextfield\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168],"tags":[59,143],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/299"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}