免费开源的iOS开发学习平台

UITextView详解:1-UITextView的基本属性

UITextView类继承自UIScrollView, 所以它拥有UIScrollView类的属性和方法,支持上下滑动, 因此适合长文本的输入。

UITextView的属性介绍

UITextView与UITextField的属性大同小异,这里我们介绍几个UITextView常用属性。

  • 文字内容
@property(null_resettable,nonatomic,copy) NSString *text;
  • UITextView的背景颜色。
@property(nullable, nonatomic,copy) UIColor  *backgroundColor;
  • 文字字体颜色
@property(nullable,nonatomic,strong) UIColor *textColor;
  • 文本对齐方式
@property(nonatomic) NSTextAlignment textAlignment;
  • 文本框内边距设置。
@property(nonatomic, assign) UIEdgeInsets textContainerInset;
  • 当文字超过视图的边框时是否允许滑动
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;  
  • 选中文字范围
@property(nonatomic) NSRange selectedRange;

示例代码

下方的示例代码中创建了一个UITextView对象,并对其基本属性进行了设置。

- (void)viewDidLoad {
    [super viewDidLoad];
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    textView.center =CGPointMake([UIScreen mainScreen].bounds.size.width/2, 200) ;
    textView.backgroundColor = [UIColor lightGrayColor];
    textView.textAlignment = NSTextAlignmentCenter;
    textView.scrollEnabled = YES;
    textView.textColor = [UIColor redColor];
    textView.keyboardType = UIKeyboardTypeNamePhonePad;
    textView.returnKeyType = UIReturnKeyDone;
    textView.text = @"99iOS--苹果iOS开发进阶之路";
    textView.font = [UIFont systemFontOfSize:20.0];
    textView.contentInset = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f);
    [self.view addSubview:textView];
}

运行效果:

示例代码

https://github.com/99ios/10.2.1