swift UIToolbar

//

//  ToolBarViewController.swift

//  UIControlDemo

//

//  Created by  on 14/12/3.

//  Copyright (c) 2014 马大哈. All rights reserved.

//

 

import UIKit

class ToolBarViewController: BaseViewController ,UIAlertViewDelegate ,UIActionSheetDelegate{

    var toolBar: UIToolbar?

    var popOver: UIPopoverController?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

     

        let alert1BarButton = UIBarButtonItem(title: "提示8.0之前", style: UIBarButtonItemStyle.Plain, target: self, action: "alert1Method")

        let flexLeftBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

        

        let alert2BarButton = UIBarButtonItem(title: "提示8.0之后", style: UIBarButtonItemStyle.Plain, target: self, action: "alert2Method")

        let flexMiddleBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

        

        let shareBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: "ShareMethod:")

        let flexRightBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

        

        let actionBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Compose, target: self, action: "ActionSheetMethod")

        let itemArray = [alert1BarButton,flexLeftBarButton,alert2BarButton,flexMiddleBarButton,shareBarButton,flexRightBarButton,actionBarButton]

        

        toolBar = UIToolbar(frame: CGRectMake(0, self.view.frame.size.height-40, self.view.frame.size.width,40))

        toolBar?.items = itemArray

        self.view.addSubview(toolBar!)

        

    }

    

    

    //  提示1方法

    func alert1Method(){

        /*

        version < 8.0,可以使用提示

        并且执行Delegate (要写UIAlertViewDelegate)

        然后写出协议方法 func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {}

        */

        var alertView = UIAlertView(title: "title", message: "message", delegate: self, cancelButtonTitle: "返回", otherButtonTitles: "确定","跳过")

        alertView.show()

 

    }

    

    func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {

        println("\(buttonIndex)   cancle=0,其他依次索引")

    }

        

    // 提示2方法

    func alert2Method(){

        /*

        version >= 8.0.0 使用此提示

        直接在action操作后追加执行方法即可

        */

        var touchAlertController = UIAlertController(title: "点击提示", message: "alert提示", preferredStyle: UIAlertControllerStyle.Alert)

        touchAlertController.addAction(UIAlertAction(title: "确认", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in

            println("确认,在这里直接写执行的方法即可")

        }))

        

        touchAlertController.addAction(UIAlertAction(title: "返回", style: UIAlertActionStyle.Cancel, handler: { (action: UIAlertAction!) in

            println("返回")

        }))

        self.presentViewController(touchAlertController, animated: true, completion: nil)     

    }

 

    // 分享方法 此时参数类型AnyObject相当于OC中的id(不明确具体传过来的参数是什么控件,目的在于判断是否butttonbarbuttonitem

    func ShareMethod(control:AnyObject){

        

        // 定义一个任意类型的数组(存放string image等)

        var shareContent = [AnyObject]()

        shareContent.append("控件类别App测试")

        shareContent.append(UIImage(named: "test_DiamondInlay_goods_0.jpg")!)//这个图片资源必须存在,否则会crash

     

       let activityViewController = UIActivityViewController(activityItems: shareContent, applicationActivities: nil)

        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {

            presentViewController(activityViewController, animated: true, completion: nil)

        } else if UIDevice.currentDevice().userInterfaceIdiom == .Pad {

            if (popOver?.popoverVisible != nil){               

                popOver?.dismissPopoverAnimated(true)

            }else{

                popOver  = UIPopoverController(contentViewController: activityViewController)

                if control is UIBarButtonItem {

                    popOver?.presentPopoverFromBarButtonItem(control as UIBarButtonItem, permittedArrowDirections: .Any, animated: true)

                } else {

                    var button = control as UIButton

                    popOver?.presentPopoverFromRect(button.frame, inView: self.view, permittedArrowDirections:.Any, animated:true)

                }

            }

        }

    }

    

    func ActionSheetMethod(){

        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {

            //destructiveButtonTitle  显示红色字体,实际使用中可以不写这个

            var actSheet = UIActionSheet(title: "title", delegate: self, cancelButtonTitle: "返回", destructiveButtonTitle: nil, otherButtonTitles: "跳过1","跳过2","跳过3")

            actSheet.showInView(self.view)

        }

    }

    

    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {

        println("\(buttonIndex)   cancle=0,其他依次索引")

    }

        override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

 

转载于:https://www.cnblogs.com/madaha/p/4145995.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用Swift语言编写的富文本编辑器用户界面的示例: ```swift import UIKit class RichTextEditorViewController: UIViewController, UITextViewDelegate { var textView: UITextView! var toolbar: UIToolbar! override func viewDidLoad() { super.viewDidLoad() // 设置文本视图 textView = UITextView(frame: view.bounds) textView.autoresizingMask = [.flexibleWidth, .flexibleHeight] textView.delegate = self view.addSubview(textView) // 设置工具栏 toolbar = UIToolbar(frame: CGRect(x: 0, y: view.bounds.height - 44, width: view.bounds.width, height: 44)) toolbar.autoresizingMask = [.flexibleWidth, .flexibleTopMargin] view.addSubview(toolbar) // 添加工具栏按钮 let boldButton = UIBarButtonItem(title: "B", style: .plain, target: self, action: #selector(boldButtonTapped)) let italicButton = UIBarButtonItem(title: "I", style: .plain, target: self, action: #selector(italicButtonTapped)) let underlineButton = UIBarButtonItem(title: "U", style: .plain, target: self, action: #selector(underlineButtonTapped)) let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) toolbar.items = [boldButton, italicButton, underlineButton, flexibleSpace] // 设置文本视图的样式 let font = UIFont.systemFont(ofSize: 16) let attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: font] textView.typingAttributes = attributes } // 处理工具栏按钮的点击事件 @objc func boldButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.boldSystemFont(ofSize: 16) textView.textStorage.addAttributes(newAttributes, range: range) } @objc func italicButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.italicSystemFont(ofSize: 16) textView.textStorage.addAttributes(newAttributes, range: range) } @objc func underlineButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.single.rawValue textView.textStorage.addAttributes(newAttributes, range: range) } // UITextViewDelegate方法 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { // 处理删除样式 if text == "" { let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.systemFont(ofSize: 16) newAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.none.rawValue textView.textStorage.addAttributes(newAttributes, range: range) } return true } } ``` 这个示例代码包括一个文本视图和一个工具

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值