让UITextView文字垂直居中
2015-09-03 17:20
246 查看
让UITextView和UITextField同样拥有垂直居中的属性,建议单独一个类继承自UITextView
只需要初始化UITextView之后用KVO监听 "contentSize" 属性即可
_textView = [[XHMessageTextView
alloc]
init];
_textView.delegate =
self;
_textView.placeHolder =
@"我来评论...";
// _textVie.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//byliuchao
_textView.frame =
CGRectMake(10,
2, KWIDTH -
70, 38);
_textView.font = [UIFont
systemFontOfSize:14];
_textView.textColor =
FONTCOLOR;
_textView.placeHolderTextColor = [UIColor
colorWithRed:189/255.0
green:193/255.0
blue:199/255.0
alpha:0.5];
_textView.layer.borderColor = [UIColor
colorWithRed:189/255.0
green:193/255.0
blue:199/255.0
alpha:0.5].CGColor;
_textView.layer.cornerRadius =
5.0;
//end
[_textView
addObserver:self
forKeyPath:@"contentSize"options:NSKeyValueObservingOptionNew
context:nil];
_textView.layer.borderWidth =
0.5;
_textView.layer.masksToBounds =
YES;
_textView.returnKeyType =
UIReturnKeyDone;
[dockview
addSubview:_textView];
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary
*)change context:(void *)context
{
CGFloat topCorrect = ([_textView
bounds].size.height - [_textView
contentSize].height);
topCorrect = (topCorrect <0.0 ?
0.0 : topCorrect);
_textView.contentOffset = (CGPoint){.x =
0, .y = -topCorrect /
2};
}
只需要初始化UITextView之后用KVO监听 "contentSize" 属性即可
_textView = [[XHMessageTextView
alloc]
init];
_textView.delegate =
self;
_textView.placeHolder =
@"我来评论...";
// _textVie.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//byliuchao
_textView.frame =
CGRectMake(10,
2, KWIDTH -
70, 38);
_textView.font = [UIFont
systemFontOfSize:14];
_textView.textColor =
FONTCOLOR;
_textView.placeHolderTextColor = [UIColor
colorWithRed:189/255.0
green:193/255.0
blue:199/255.0
alpha:0.5];
_textView.layer.borderColor = [UIColor
colorWithRed:189/255.0
green:193/255.0
blue:199/255.0
alpha:0.5].CGColor;
_textView.layer.cornerRadius =
5.0;
//end
[_textView
addObserver:self
forKeyPath:@"contentSize"options:NSKeyValueObservingOptionNew
context:nil];
_textView.layer.borderWidth =
0.5;
_textView.layer.masksToBounds =
YES;
_textView.returnKeyType =
UIReturnKeyDone;
[dockview
addSubview:_textView];
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary
*)change context:(void *)context
{
CGFloat topCorrect = ([_textView
bounds].size.height - [_textView
contentSize].height);
topCorrect = (topCorrect <0.0 ?
0.0 : topCorrect);
_textView.contentOffset = (CGPoint){.x =
0, .y = -topCorrect /
2};
}
相关文章推荐
- UITextFiledView页面之间的相互传值
- Android UI设计(引导界面):ViewPager之三PagerTabStrip与PagerTitleStrip添加标题栏的区别
- jquey.fullpage详解
- UINavigaionController中的传值(协议)
- Android UI设计——ViewPage的无限循环(五)
- Arduino LM35温度检测
- RYU的GUI安装
- 黑马程序员—————Java基础--------GUI
- Android UI设计——ViewPage中自定义标题栏(四)
- UI触屏的操作初识
- 使用eventqueue.invokelater()好处、原因
- UIAlertView
- ios开发 UI阶段第八周
- UINavigationController
- 区间离散化+线段树区间求最值poj 3368 Frequent values
- Drupal安装百度编辑器Ueditor for wysiwyg
- iOS软件开发之在UIScrollView上添加手势不能触发解决办法
- this class is not key value coding-compliant for the key progressVIew
- 1056. Mice and Rice (25)
- [翻译]Cell 中的视图控制器