iOS开发之TextView常用属性

基本属性:

//textView尺寸和位置
        let textViewWidth: CGFloat = 223
        let textViewHeight: CGFloat = 198
        let textViewTopView: CGFloat = 240
        let textViewFrame = CGRect(x: 22, y: textViewTopView, width: textViewWidth, height: textViewHeight)
        
        let textView = UITextView(frame: textViewFrame)
        
        textView.text =  "    我们的生活几乎离不号码,如电话号码、手机号码、车牌号码、门牌号码等等。号码为什么会影响一个人的运势?其实号码也包含有一定的吉凶数理,这就像姓名会影响运势命运的意义是一样的。"
        

效果:0d2b98640f9a884d94906650a6f33458362.jpg

加上几个常用属性

 //textView是否可编辑,是否可选中
        textView.allowsEditingTextAttributes = false
        textView.isSelectable = true
        
        //字体颜色,背景颜色,字体大小,字体对齐方式
        textView.textColor = UIColor.green
        textView.backgroundColor = UIColor.orange
        textView.font = UIFont.systemFont(ofSize: 12.0)
        textView.textAlignment = .center

效果:466a59d1bb920300ba8674709c291679fab.jpg

改为富文本

// 创建可变属性字符串
        let attribute = NSMutableAttributedString(string: textView.text)
        // 设置段落样式
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        paragraphStyle.lineSpacing = 10
        // 设置属性字典
        let dic = [NSAttributedString.Key.foregroundColor: UIColor.red,
                   NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 22.0),
                   NSAttributedString.Key.paragraphStyle: paragraphStyle]
        // 添加属性字典
        attribute.addAttributes(dic, range: NSMakeRange(16, textView.text.count - 16))
        // 设置textView的attributedText
        textView.attributedText = attribute

效果:4e7e039888a411232e388b30ccbcdf10920.jpg

设置文字与边框间距,显示边框

//设置文本内容与边框的间距
        textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)

        //显示边框
        textView.layer.borderColor = UIColor.cyan.cgColor
        textView.layer.borderWidth = 2.0

效果:1ca92cbbe7660aff5ac0290727efb2d6a60.jpg

完整代码:

//: A UIKit based Playground for presenting user interface
  
import UIKit
import PlaygroundSupport

class MyViewController : UIViewController ,UITextViewDelegate{
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        self.view = view
        
        //textView尺寸和位置
        let textViewWidth: CGFloat = 223
        let textViewHeight: CGFloat = 198
        let textViewTopView: CGFloat = 240
        let textViewFrame = CGRect(x: 22, y: textViewTopView, width: textViewWidth, height: textViewHeight)
        
        let textView = UITextView(frame: textViewFrame)
        
        textView.text =  "    我们的生活几乎离不号码,如电话号码、手机号码、车牌号码、门牌号码等等。号码为什么会影响一个人的运势?其实号码也包含有一定的吉凶数理,这就像姓名会影响运势命运的意义是一样的。"
        
        //textView是否可编辑,是否可选中
        textView.allowsEditingTextAttributes = false
        textView.isSelectable = true
        
        //字体颜色,背景颜色,字体大小,字体对齐方式
        textView.textColor = UIColor.green
        textView.backgroundColor = UIColor.orange
        textView.font = UIFont.systemFont(ofSize: 12.0)
        textView.textAlignment = .center
        
        
        // 创建可变属性字符串
        let attribute = NSMutableAttributedString(string: textView.text)
        // 设置段落样式
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        paragraphStyle.lineSpacing = 10
        // 设置属性字典
        let dic = [NSAttributedString.Key.foregroundColor: UIColor.red,
                   NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 22.0),
                   NSAttributedString.Key.paragraphStyle: paragraphStyle]
        // 添加属性字典
        attribute.addAttributes(dic, range: NSMakeRange(16, textView.text.count - 16))
        // 设置textView的attributedText
        textView.attributedText = attribute
        
        //设置文本内容与边框的间距
        textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)

        //显示边框
        textView.layer.borderColor = UIColor.cyan.cgColor
        textView.layer.borderWidth = 2.0
        
        textView.delegate = self//将ViewController当前对象赋值给TextView控件的delegate委托属性
        view.addSubview(textView)
   
    }
    
    
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

转载于:https://my.oschina.net/u/4197015/blog/3100211

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值