Swift4 2 UITextView基本用法

先看效果图

github源码 github.com/daomoer/YYS… 初始化一个UITextView

let textView = UITextView(frame: CGRect(x:30, y:200, width:UIScreen.main.bounds.width-60, height:200))
textView.delegate = self
self.view.addSubview(textView)
复制代码

UITextView属性设置

//字体颜色
textView.textColor = UIColor.purple
//内容部分链接样式
textView.linkTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange,
                               NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]
//边框
textView.layer.borderColor = UIColor.red.cgColor
textView.layer.borderWidth = 1.5
//字体大小
textView.font = UIFont.systemFont(ofSize: 16)
//内容可编辑
textView.isEditable = false
//内容可选
textView.isSelectable = true
//边框圆角设置
textView.layer.masksToBounds = true
textView.layer.cornerRadius = 5.0
//自适应高度
textView.autoresizingMask = UIViewAutoresizing.flexibleHeight
复制代码

设置内容文本富文本样式显示

//设置富文本
let attributeString:NSMutableAttributedString=NSMutableAttributedString(string: "这是一条测试textview的字符串数据,欢迎访问 http://www.baidu.com")
//设置字体颜色
attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.green, range: NSMakeRange(0, attributeString.length))
//文本所有字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(0, attributeString.length))
//文本0开始5个字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "HelveticaNeue-Bold", size: 26)!, range: NSMakeRange(0, 5))
//设置字体颜色
attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.white, range: NSMakeRange(0, 3))
//设置文字背景颜色
attributeString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.orange, range: NSMakeRange(3, 3))
//赋值富文本
textView.attributedText = attributeString
复制代码

设置文本内容对齐方式

textView.textAlignment = NSTextAlignment.center
/*
 textView.textAlignment = NSTextAlignment.left
 textView.textAlignment = NSTextAlignment.center
 */
复制代码

给文中的特殊部分加上链接,比如手机号,网址邮箱等(需要注意的是如果使用了给特殊部分加上连接要先使内容不可编辑textView.isEditable = false)

textView.dataDetectorTypes = UIDataDetectorTypes.link
/*
 textView.dataDetectorTypes = UIDataDetectorTypes.link
 textView.dataDetectorTypes = UIDataDetectorTypes.address
 textView.dataDetectorTypes = UIDataDetectorTypes.calendarEvent
 textView.dataDetectorTypes = UIDataDetectorTypes.shipmentTrackingNumber
 textView.dataDetectorTypes = UIDataDetectorTypes.flightNumber
 textView.dataDetectorTypes = UIDataDetectorTypes.lookupSuggestion
 textView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
 */
复制代码

设置键盘类型

textView.keyboardType = UIKeyboardType.asciiCapable
/*
 textView.keyboardType = UIKeyboardType.default
 textView.keyboardType = UIKeyboardType.numbersAndPunctuation
 textView.keyboardType = UIKeyboardType.URL
 textView.keyboardType = UIKeyboardType.numberPad
 textView.keyboardType = UIKeyboardType.phonePad
 textView.keyboardType = UIKeyboardType.namePhonePad
 textView.keyboardType = UIKeyboardType.emailAddress
 textView.keyboardType = UIKeyboardType.decimalPad
 textView.keyboardType = UIKeyboardType.twitter
 */
复制代码

键盘返回类型

textView.returnKeyType = UIReturnKeyType.continue
/*
 textView.returnKeyType = UIReturnKeyType.default
 textView.returnKeyType = UIReturnKeyType.go
 textView.returnKeyType = UIReturnKeyType.google
 textView.returnKeyType = UIReturnKeyType.join
 textView.returnKeyType = UIReturnKeyType.next
 textView.returnKeyType = UIReturnKeyType.route
 textView.returnKeyType = UIReturnKeyType.search
 textView.returnKeyType = UIReturnKeyType.send
 textView.returnKeyType = UIReturnKeyType.yahoo
 textView.returnKeyType = UIReturnKeyType.done
 textView.returnKeyType = UIReturnKeyType.emergencyCall
 textView.returnKeyType = UIReturnKeyType.continue
 */
复制代码

当选择内容文本的时候,顶部会出现对选择内容操作的segment,可以选择自定义segment

let mail = UIMenuItem(title: "邮件", action: #selector(ViewController.onMail))
let weixin = UIMenuItem(title: "微信", action: #selector(ViewController.onWeiXin))
let menu = UIMenuController()
menu.menuItems = [mail,weixin]
}
@objc func onMail(){

}
    
@objc func onWeiXin(){
        
}
复制代码

下面为UITextView的各个代理方法

// 开始编辑文本的时候触发该方法
func textViewDidBeginEditing(_ textView: UITextView) {

}
// 结束编辑的时候触发该方法
func textViewDidEndEditing(_ textView: UITextView) {

}

//如果返回false,文本视图不能编辑
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
    return true
}

//如果返回false,表示编辑结束之后,文本视图不可再编辑
func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
    return true
}

//文本视图内容改变时,触发本方法,如果是回车符号,则textView释放第一响应值,返回false
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if (text ==  "\n") {
        textView.resignFirstResponder()
        return false;
    }
    return true
}

//文本视图改变后触发本代理方法
func textViewDidChange(_ textView: UITextView) {
    
}
//文本视图 改变选择内容,触发本代理方法
func textViewDidChangeSelection(_ textView: UITextView) {
    
}
//链接在文本中显示。当链接被点击的时候,会触发本代理方法
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool{
    return true
}
//文本视图允许提供文本附件,文本附件点击时,会触发本代理方法   return true
func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    return true
}
复制代码

转载于:https://juejin.im/post/5c88e4ea5188251bbf2ee9e3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,您需要将 YYLabel 添加到 UITableViewCell 中。 您可以通过在 UITableViewCell 子类中创建 YYLabel 实例并将其添加到 contentView 中来完成此操作。 您可以使用自动布局或手动布局将 YYLabel 放置在所需的位置上。 接下来,您需要创建 NSMutableAttributedString,其中包含 UITextView 和其他文本。 然后,您可以使用 NSMutableAttributedString 的 insert 方法将 UITextView 作为附件添加到文本中。 以下是一个示例代码片段: ```swift let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 50)) textView.text = "This is a UITextView" let attachment = NSTextAttachment() attachment.bounds = CGRect(x: 0, y: 0, width: 100, height: 50) attachment.setAttachmentContent(textView) let attributedString = NSMutableAttributedString(string: "This is a YYLabel with a UITextView attachment") attributedString.insert(NSAttributedString(attachment: attachment), at: 21) yyLabel.attributedText = attributedString ``` 最后,您需要处理 UITextView 和 UITableView 之间的交互。您需要将 UITextView 的 isEditable 属性设置为 false,以防止用户编辑文本。您还需要在 UITableViewDelegate 中实现 heightForRowAt 和 estimatedHeightForRowAt 方法来动态计算 UITableViewCell 的高度,以适应包含 UITextView 的 YYLabel。 以下是一个示例 UITableViewDelegate 实现: ```swift class MyTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cell") } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell // Configure the cell... return cell } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MyTableViewCell cell.configure(with: "This is a YYLabel with a UITextView attachment") return cell.systemLayoutSizeFitting(CGSize(width: tableView.frame.width, height: UIView.layoutFittingCompressedSize.height)).height } override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } } ``` 请注意,此示例实现了一个名为 MyTableViewCell 的 UITableViewCell 子类,该子类包含一个名为 yyLabel 的 YYLabel 实例。 该子类还实现了一个名为 configure(with:) 的方法,该方法接受一个字符串参数,并在 YYLabel 中设置带有 UITextView 附件的 NSMutableAttributedString。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值