1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| //创建UITextView对象 textView = UITextView(frame:CGRect(x: 10.0, y: 120.0, width: 300.0, height: 200.0)) //为方便看到效果,设置背景颜色 textView.backgroundColor = UIColor.gray //添加到视图上 self.view.addSubview(textView) //------------------ 常用属性 //设置textview里面的字体颜色 textView.textColor = UIColor.green //设置文本字体 textView.font = UIFont.systemFont(ofSize: 18);//使用系统默认字体,指定18号字号 textView.font = UIFont(name: "Helvetica-Bold", size: 18)//指定字体,指定字号 //设置它的背景颜色 textView.backgroundColor = UIColor.gray //设置显示内容 textView.text = "http://www.jianshu.com/notebooks/7139633/latest \nhttp://www.jianshu.com/notebooks/7139633/latest \nhttp://www.jianshu.com/notebooks/7139633/latest" //文本对齐方式 textView.textAlignment = NSTextAlignment.left //文本视图设置圆角 textView.layer.cornerRadius = 20 //是否允许点击链接和附件 textView.isSelectable = true //返回键的类型 textView.returnKeyType = UIReturnKeyType.done //键盘类型 textView.keyboardType = UIKeyboardType.default //是否可以滚动 textView.isScrollEnabled = true //自适应高度 textView.autoresizingMask = UIViewAutoresizing.flexibleHeight //设置富文本 let attributeString:NSMutableAttributedString=NSMutableAttributedString(string: "http://www.jianshu.com/notebooks/7139633/latest \nhttp://www.jianshu.com/notebooks/7139633/latest \nhttp://www.jianshu.com/notebooks/7139633/latest") //设置字体颜色 attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSMakeRange(0, attributeString.length)) //文本所有字符字体HelveticaNeue-Bold,16号 attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(0, attributeString.length)) //文本0开始5个字符字体HelveticaNeue-Bold,26号 attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 26)!, range: NSMakeRange(0, 5)) //设置字体颜色 attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: NSMakeRange(0, 3)) //设置文字背景颜色 attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.orange, range: NSMakeRange(3, 3)) //赋值富文本 textView.attributedText = attributeString //选中一段文本 textView.becomeFirstResponder() textView.selectedRange = NSMakeRange(30, 10) //获取内容整体高度 var height:CGFloat = textView.contentSize.height; //为文本的视图中的电话和网址自动加链接 textView.dataDetectorTypes = .phoneNumber; /* UIDataDetectorTypePhoneNumber 电话有链接 UIDataDetectorTypeLink 网址有链接 UIDataDetectorTypeAddress 地址有链接 UIDataDetectorTypeCalendarEvent 日历时间具有链接 iOS10---- UIDataDetectorTypeShipmentTrackingNumber 货物订单号 UIDataDetectorTypeFlightNumber 机票 UIDataDetectorTypeLookupSuggestion 用户信息 --- UIDataDetectorTypeNone UIDataDetectorTypeAll */
|