textfield截取字符串
ios7 会崩溃
解:
之前的写法是这样的
正确的写法:
 

先判断markedTextRange是否为nil,   markedTextRange这个属性是啥意思呢

表示文件中当前选中的文本的长度,参考下面一片文章,详细讲解了这个属性的意思:
在解决上述问题的时候你需要判断markedTextRange是不是为Nil,如果为Nil的话就说明你现在没有未选中的字符,可以计算文字长度。否则此时计算出来的字符长度可能不正确
如果选中的长度为nil,即没有选中的长度,那么就正常计算字符串长度,如果超长就截取字符串
如果选中的长度不为nil,就是有选中的,那不用截取字符串,让他正常输入,超出了,真正往textFild填充的时候,, 选中的字符串的长度为nil了,这样就会走if判断,切割字符串
 

 
还写了下面的方法:
 

这个方法的作用是判断,是否需要修改text in range ,是否需要修改某一段文字, 如果不实现这个代理方法,在中文输入法下面,文字长度超出后,点击键盘,还是会联想字符串
里面的判断表示,如果没有选中的文本, 文本的总长度如果超出了限制,就不让它修改 return no
如果没有选中的文本,文本长度没有超出限制,可以修改 return yes
如果有选中的文本,让他修改 return yes

完整代码:
  1. //
  2. // HDFWorkTableAddCheckItemView.h
  3. // newDoctor
  4. //
  5. // Created by songximing on 16/8/20.
  6. // Copyright © 2016年 haodf.com. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. /**
  12. * @author songximing, 16-08-20 16:08:31 22:12 -2
  13. *
  14. * 请输入检查项目View
  15. */
  16. @interface HDFBenchAddCheckItemView : UIView
  17. - (void)show;
  18. @property (nonatomic, copy) HDFStringBlock saveStringBlock; //<!确定按钮保存的字符串
  19.  
  20. /**
  21. * @author songximing, 16-08-20 21:08:01
  22. *
  23. * 请输入检查项目View 的初始化方法
  24. *
  25. * @param frame 必须给frame(跟屏幕相等)
  26. * @param content 带入的文案内容 第一次进入没有传 nil
  27. * @param title 标题 "请输入检查项目" 或者 "请输入检验项目" 两个必须填写一个
  28. * @param placeHolder 站位文字 必须传
  29. *
  30. * @return self
  31. */
  32. - (instancetype)initWithFrame:(CGRect)frame content:(NSString *)content title:(NSString *)title placeHolder:(NSString *)placeHolder;
  33. @property (nonatomic, copy) NSString *cancelString; //!<清空
  34. @end
  1. //
  2. // HDFWorkTableAddCheckItemView.m
  3. // newDoctor
  4. //
  5. // Created by songximing on 16/8/20.
  6. // Copyright © 2016年 haodf.com. All rights reserved.
  7. //
  8.  
  9. #import "HDFBenchAddCheckItemView.h"
  10. #import "NSString+HDFAdditions.h"
  11.  
  12. #define kDescriptionMAXLength 50 //最多50字
  13. #define kTextViewStringWidth kScreenWidth - 50 - 30 // 50是白色View的左右间距 30是textView距离白色view的左右间距
  14. #define kBenchBottomViewHeight 45.0f
  15.  
  16. @interface HDFBenchAddCheckItemView ()<UITextViewDelegate>
  17. @property (nonatomic, strong) UITextView *textView;
  18. @property (nonatomic, strong) UIView *whiteContainerView;
  19. @property (nonatomic, strong) UIButton *cancelButton;
  20. @property (nonatomic, strong) UIControl *bgCoverControl;
  21. @property (nonatomic, copy) NSString *title;
  22. @property (nonatomic, copy) NSString *content; //!<带入的文本内容
  23. @property (nonatomic, copy) NSString *placeHolder; //!<textView站位文字
  24. @end
  25.  
  26. @implementation HDFBenchAddCheckItemView
  27.  
  28. - (void)dealloc {
  29. LOG(@"请输入检查项目View释放了...");
  30. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  31. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  32. }
  33.  
  34. - (instancetype)initWithFrame:(CGRect)frame content:(NSString *)content title:(NSString *)title placeHolder:(NSString *)placeHolder{
  35. if (self = [super initWithFrame:frame]) {
  36. self.title = title;
  37. self.content = content;
  38. self.placeHolder = placeHolder;
  39. [self configUI];
  40. }
  41. return self;
  42. }
  43.  
  44. #pragma mark - UI
  45. - (void)configUI {
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  48.  
  49. // 灰色遮罩
  50. UIControl *bgCoverControl = [UIControl hdf_viewWithSuperView:self constraints:^(MASConstraintMaker *make) {
  51. make.edges.mas_equalTo();
  52. } tap:^(UIGestureRecognizer *sender) {
  53. // [self dismiss];
  54. }];
  55. bgCoverControl.backgroundColor = [UIColor colorWithRed: green: blue: alpha:];
  56. self.bgCoverControl = bgCoverControl;
  57.  
  58. // 白色背景
  59. UIView *whiteContainerView = [UIView hdf_viewWithSuperView:self constraints:^(MASConstraintMaker *make) {
  60. make.centerY.mas_equalTo();
  61. make.centerX.mas_equalTo();
  62. make.width.mas_equalTo();
  63. }];
  64. whiteContainerView.backgroundColor = kWColor;
  65. whiteContainerView.alpha = ;
  66. whiteContainerView.layer.cornerRadius = ;
  67. whiteContainerView.clipsToBounds = YES;
  68. self.whiteContainerView = whiteContainerView;
  69.  
  70. // title
  71. UILabel *titleLabel = [UILabel hdf_labelWithText:@"请输入检查项目" font:kFontWithSize() superView:whiteContainerView constraints:^(MASConstraintMaker *make) {
  72. make.top.mas_equalTo();
  73. make.centerX.mas_equalTo();
  74. }];
  75. titleLabel.textAlignment = NSTextAlignmentCenter;
  76. if (self.title.length > ) { // 设置标题
  77. titleLabel.text = self.title;
  78. }
  79.  
  80. // 文本输入框
  81. self.textView = [UITextView hdf_textViewWithPlaceholder:@" " delegate:self superView:whiteContainerView constraints:^(MASConstraintMaker *make) {
  82. make.top.mas_equalTo(titleLabel.mas_bottom).offset();
  83. make.left.mas_equalTo();
  84. make.right.mas_equalTo(-);
  85. make.height.mas_equalTo(kFontWithSize().lineHeight * );
  86. }];
  87. self.textView.layer.cornerRadius = ;
  88. self.textView.clipsToBounds = YES;
  89. self.textView.layer.borderWidth = 0.5;
  90. self.textView.layer.borderColor = kColorWith16RGB(0xcccccc).CGColor;
  91. self.textView.hdf_placeholder = @"请输入检查项目";
  92.  
  93. self.textView.backgroundColor = kColorWith16RGB(0xf8f8f8);
  94. self.textView.font = kFontWithSize();
  95. self.textView.hdf_placeholderFont = kFontWithSize();
  96. self.textView.hdf_placeholderLabel.numberOfLines = ;
  97. self.textView.hdf_placeholderColor = [UIColor redColor];
  98. [self.textView.hdf_placeholderLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  99. make.top.mas_equalTo(self.textView).offset(7.5);
  100. make.left.mas_equalTo(self.textView).offset();
  101. make.width.mas_equalTo(self.textView.mas_width).offset(-); // 让站位文字换行
  102. }];
  103.  
  104. if (self.content.length > ) { // 带入的文本内容
  105. self.textView.text = self.content;
  106. self.textView.hdf_placeholder = self.placeHolder;
  107. self.textView.hdf_placeholderLabel.hidden = YES;
  108. }else { // 无内容,展示站位文字
  109. self.textView.hdf_placeholder = self.placeHolder;
  110. self.textView.hdf_placeholderLabel.hidden = NO;
  111. }
  112.  
  113. // // textView高度自适应内容
  114. // CGSize textSize = CGSizeZero;
  115. // if (isIOS(7.0)) {
  116. // textSize = [self.textView sizeThatFits:CGSizeMake(kTextViewStringWidth, MAXFLOAT)];
  117. // }
  118. // else{
  119. // textSize = self.textView.contentSize;
  120. // }
  121. // if (kTextViewHeight < textSize.height) {
  122. // [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
  123. // make.height.mas_equalTo(textSize.height);
  124. // }];
  125. // }else {
  126. // [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
  127. // make.height.mas_equalTo(kTextViewHeight);
  128. // }];
  129. // }
  130.  
  131. [self createBottomView];
  132. }
  133.  
  134. - (void)createBottomView {
  135. UIView *bottomBgView = [[UIView alloc]init];
  136. [self.whiteContainerView addSubview:bottomBgView];
  137. [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  138. make.top.equalTo(self.textView.mas_bottom).offset();
  139. make.left.right.equalTo(self.whiteContainerView);
  140. make.height.mas_equalTo(kBenchBottomViewHeight);
  141. }];
  142.  
  143. // 水平分割线
  144. UIView *horizontalLineView = [[UIView alloc]init];
  145. [bottomBgView addSubview:horizontalLineView];
  146. horizontalLineView.backgroundColor = kG5Color;
  147. [horizontalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
  148. make.top.equalTo(bottomBgView);
  149. make.left.right.equalTo(bottomBgView);
  150. make.height.mas_equalTo(kLineHeight);
  151. }];
  152.  
  153. // 垂直分割线
  154. UIView *verticalLineView = [[UIView alloc]init];
  155. [bottomBgView addSubview:verticalLineView];
  156. verticalLineView.backgroundColor = kG5Color;
  157. [verticalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
  158. make.top.bottom.equalTo(bottomBgView);
  159. make.centerX.equalTo(bottomBgView);
  160. make.width.mas_equalTo(kLineHeight);
  161. }];
  162.  
  163. kWeakObject(self)
  164. // 取消 按钮
  165. UIButton *cancelButton = [UIButton hdf_buttonWithTitle:@"取消" superView:bottomBgView constraints:^(MASConstraintMaker *make) {
  166. make.top.equalTo(horizontalLineView.mas_bottom);
  167. make.left.bottom.equalTo(bottomBgView);
  168. make.right.equalTo(verticalLineView.mas_left);
  169. } touchup:^(UIButton *sender) {
  170. [weakObject cancelButtonClick];
  171. }];
  172. [cancelButton setTitleColor:kNavColer forState:UIControlStateNormal];
  173. self.cancelButton = cancelButton;
  174.  
  175. UIButton *confirmButton = [UIButton hdf_buttonWithTitle:@"确定" superView:bottomBgView constraints:^(MASConstraintMaker *make) {
  176. make.top.equalTo(horizontalLineView.mas_bottom);
  177. make.right.bottom.equalTo(bottomBgView);
  178. make.left.equalTo(verticalLineView.mas_right);
  179. } touchup:^(UIButton *sender) {
  180. [weakObject confirmButtonClick];
  181. }];
  182. [confirmButton setTitleColor:kNavColer forState:UIControlStateNormal];
  183.  
  184. [self.whiteContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
  185. make.bottom.equalTo(bottomBgView);
  186. }];
  187. }
  188.  
  189. #pragma mark - 点击事件
  190. - (void)confirmButtonClick {
  191. // 去掉首尾的空格
  192. self.textView.text = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  193.  
  194. // if (self.textView.text.length < 1) {
  195. // [HDFHud showTip:@"请先输入检查项目"];
  196. // return;
  197. // }
  198. [self dismiss];
  199. if (self.saveStringBlock) {
  200. self.saveStringBlock(self.textView.text);
  201. }
  202. }
  203.  
  204. - (void)cancelButtonClick {
  205. // self.textView.text = nil; // 点击取消,清空输入的文字
  206. [self dismiss];
  207. // if (self.saveStringBlock) {
  208. // self.saveStringBlock(self.textView.text);
  209. // }
  210. }
  211.  
  212. #pragma UITextViewDelegate
  213. -(void)textViewDidChange:(UITextView *)textView {
  214. if (self.textView.text.length == ) {
  215. self.textView.hdf_placeholderLabel.hidden = NO;
  216. }else {
  217. self.textView.hdf_placeholderLabel.hidden = YES;
  218. }
  219. // 限制50个字数
  220. if (textView.markedTextRange == nil) {
  221. if (self.textView.text.length > kDescriptionMAXLength) {
  222. // [HDFHud showTip:@"简介最多输入50个字"];
  223. self.textView.text = [self.textView.text substringToIndex: kDescriptionMAXLength]; // MAXLENGTH为最大字数
  224. }
  225. }
  226.  
  227. // // textView高度自适应内容
  228. // CGSize textSize = CGSizeZero;
  229. // if (isIOS(7.0)) {
  230. // textSize = [self.textView sizeThatFits:CGSizeMake(kTextViewStringWidth, MAXFLOAT)];
  231. // }
  232. // else{
  233. // textSize = self.textView.contentSize;
  234. // }
  235. //
  236. // if (kTextViewHeight < textSize.height) {
  237. // [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
  238. // make.height.mas_equalTo(textSize.height);
  239. // }];
  240. // [UIView animateWithDuration:0.25 animations:^{
  241. // [self layoutIfNeeded];
  242. // }];
  243. // }else {
  244. // [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
  245. // make.height.mas_equalTo(kTextViewHeight);
  246. // }];
  247. // }
  248. }
  249.  
  250. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
  251. if (textView.markedTextRange == nil) {
  252. if (range.location >= kDescriptionMAXLength) {
  253. return NO;
  254. }
  255. }
  256. return YES;
  257. }
  258.  
  259. #pragma mark - 键盘相关
  260. // Handle keyboard show/hide changes
  261. - (void)keyboardWillShow: (NSNotification *)notification {
  262.  
  263. CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  264.  
  265. UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  266. if (UIInterfaceOrientationIsLandscape(interfaceOrientation) && NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) {
  267. CGFloat tmp = keyboardSize.height;
  268. keyboardSize.height = keyboardSize.width;
  269. keyboardSize.width = tmp;
  270. }
  271.  
  272. [self.whiteContainerView mas_updateConstraints:^(MASConstraintMaker *make) {
  273. make.centerY.mas_equalTo(-keyboardSize.height * 0.5);
  274. }];
  275. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  276. animations:^{
  277. [self layoutIfNeeded];
  278. }
  279. completion:nil
  280. ];
  281. }
  282.  
  283. - (void)keyboardWillHide: (NSNotification *)notification {
  284. [self.whiteContainerView mas_updateConstraints:^(MASConstraintMaker *make) {
  285. make.centerY.mas_equalTo();
  286. }];
  287. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  288. animations:^{
  289. [self layoutIfNeeded];
  290. }
  291. completion:nil
  292. ];
  293. }
  294.  
  295. - (void)show {
  296. [[UIApplication sharedApplication].keyWindow addSubview:self];
  297. //动画
  298. CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
  299. self.whiteContainerView.transform = transform;
  300.  
  301. [UIView animateWithDuration:0.2 animations:^{
  302. self.bgCoverControl.backgroundColor = [UIColor colorWithRed: green: blue: alpha:0.5];
  303. self.whiteContainerView.alpha = ; // 子控件的透明度跟着变化
  304. CGAffineTransform transform = CGAffineTransformMakeScale(, );
  305. self.whiteContainerView.transform = transform;
  306. }];
  307. }
  308.  
  309. - (void)dismiss {
  310. [UIView animateWithDuration:0.2 animations:^{
  311. self.bgCoverControl.backgroundColor = [UIColor colorWithRed: green: blue: alpha:];
  312. self.whiteContainerView.alpha = ;
  313. } completion:^(BOOL finished) {
  314. [self removeFromSuperview];
  315. }];
  316. }
  317.  
  318. -(void)setCancelString:(NSString *)cancelString {
  319. _cancelString = cancelString;
  320. [self.cancelButton setTitle:cancelString forState:UIControlStateNormal];
  321. }
  322.  
  323. //****以下为解决第一次进入页面,textview不可滚动问题
  324. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  325. NSString * strText = self.textView.text;
  326. CGFloat h = [strText heightWithFont:[UIFont systemFontOfSize:] withLineWidth:kTextViewStringWidth];
  327. self.textView.contentSize = CGSizeMake(, h+);
  328.  
  329. return [super hitTest:point withEvent:event];
  330. }
  331.  
  332. @end

textView截取字符串-医生工作台1期的更多相关文章

  1. ThinkPHP 模板substr的截取字符串函数

    ThinkPHP 模板substr的截取字符串函数在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr($str, $start ...

  2. shell编程常用的截取字符串操作

    1.          常用的字符串操作 1.1.           替换字符串:$ echo ${var/ /_}#支持正怎表达式 / /表示搜索到第一个替换,// /表示搜索到的结果全部替换. ...

  3. [No0000A4]DOS命令(cmd)批处理:替换字符串、截取字符串、扩充字符串、获取字符串长度

    1.替换字符串,即将某一字符串中的特定字符或字符串替换为给定的字符串.举例说明其功能:========================================= @echo off set a ...

  4. js中substr,substring,slice。截取字符串的区别

    substr(n1,n2) n1:起始位置(可以为负数) n2:截取长度(不可以为0,不可以为负数,可以为空) 当n1为正数时,从字符串的n1下标处截取字符串(起始位置),长度为n2. 当n1为负数时 ...

  5. Linux Shell 截取字符串

    Linux Shell 截取字符串 shell中截取字符串的方法很多 ${var#*/} ${var##*/} ${var%/*} ${var%%/*} ${var:start:len} ${var: ...

  6. SQL Server中截取字符串常用函数

    SQL Server 中截取字符串常用的函数: .LEFT ( character_expression , integer_expression ) 函数说明:LEFT ( '源字符串' , '要截 ...

  7. inux中shell截取字符串方法总结

    shell中截取字符串的方法有很多中, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...

  8. Excel怎样从一串字符中的某个指定“字符”前后截取字符及截取字符串常用函数

    怎么样可以从一串字符中的某个指定位置的前或后截取指定个数的字符. 如:12345.6789,我要截取小数点前(或后)的3个字符.怎么样操作, 另外,怎么样从右边截取字符,就是和left()函数相反的那 ...

  9. SQL注入截取字符串函数

    在sql注入中,往往会用到截取字符串的问题,例如不回显的情况下进行的注入,也成为盲注,这种情况下往往需要一个一个字符的去猜解,过程中需要用到截取字符串.本文中主要列举三个函数和该函数注入过程中的一些用 ...

随机推荐

  1. 浅析十三种常用的数据挖掘的技术&五个免费开源的数据挖掘软件

    一.前 沿 数据挖掘就是从大量的.不完全的.有噪声的.模糊的.随机的数据中,提取隐含在其中的.人们事先不知道的但又是潜在有用的信息和知识的过程.数据挖掘的任务是从数据集中发现模式,可以发现的模式有很多 ...

  2. BC#32 1002 hash

    代码引用kuangbin大神的,膜拜 第一次见到hashmap和外挂,看来还有很多东西要学 维护前缀和sum[i]=a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i] 枚举结尾i,然后 ...

  3. poj 3928 树状数组

    题目中只n个人,每个人有一个ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判的ID和技能值都在两个选手之间的时候才能进行一场比赛,现在问一共能组织多少场比赛. 由于排完序之后,先插入的一定 ...

  4. 程序员的恶性循环:加班->没空学习->老是写同等水平代码->无法提升代码质量->老是出BUG->老是需要修改->加班->...

    程序员的恶性循环:加班->没空学习->老是写同等水平代码->无法提升代码质量->老是出BUG->老是需要修改->加班->...

  5. BZOJ 3437: 小P的牧场 斜率优化DP

    3437: 小P的牧场 Description 背景 小P是个特么喜欢玩MC的孩纸... 描述 小P在MC里有n个牧场,自西向东呈一字形排列(自西向东用1…n编号),于是他就烦恼了:为了控制这n个牧场 ...

  6. 编译fdk-aac for ios

    Build all: build-fdk-aac.sh Build for some architectures: build-fdk-aac.sh armv7s x86_64 Build unive ...

  7. setTimeout()和setInterval() 何时被调用执行

    定义 setTimeout()和setInterval()经常被用来处理延时和定时任务.setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,而setInterval()则可以在每隔 ...

  8. 【BZOJ】1055: [HAOI2008]玩具取名(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1055 我竟然都没往dp这个方向想.....百度了下看到标题是dp马上就会转移了QAQ... 设d[i ...

  9. 我的conky配置

    安装conky的方法请看我博客另外一篇文章,这里不再阐述点这里 附上我的配置2013.08.29(吾喷) background no font WenQuanYi Micro Hei:size=10 ...

  10. X.509证书_生成X.509协议的证书

    用法:1. 用NOTE打开,修改按实际情况脚本中的(1)~ (6)处参数2. 找一台含JVM环境的WIN机器3. 双击执行后,会生成一对密钥4. 请确保当前使用的JDK版本为6.0!!! @echo ...