override func touchesBegan(touches:Set, withEvent event: UIEvent) {
if let brush = selfbrush {
brushlastPoint = nil
//之前一直是用这个方法来获取的
//brushbeginPoint = touchesanyObject()!locationInView(self)
//现在上面那个touches里面没有anyObject()这个方法,所以我们需要做类型转换
brushbeginPoint = (touches as NSSet)anyObject()!locationInView(self)
}
用TextFormat类,这个类是用来控制TextField里文字的样式的,具体参数。自己查一下API,你说的调整文字大小,我可以举一个例子: var tf:TextField = new TextField();var tfor:TextFormat
比如说,你们也许希望我们这个应用中还有以下这些自动的功能:被波浪号(~)括起来的文本会显示艺术字体。被下划线(_)括起来的文本会显示斜体。被破折号(-)括起来的文本会在其上划一道横线。所有大写字体会显示红色。如何用TextKit实现上述这些功能就是这篇文章将要向同学们介绍的内容。在学习这些之前,大家需要先理解在TextKit中,文本的存储系统是如何工作的。下面的图展示了TextKit中对文本进行存储、渲染、显现的基本流程:在开发过程中,如果你创建了UITextView、UILabel或UITextField,那么同时Apple会在幕后自动创建上图中的这些类。你可以使用这些类默认实现的行为,也可以自己实现这些类,从而达到你想要的一些对文本控制的行为。让我们来看看这些类的作用:NSTextStorage的功能是存储文本,并通过字符串属性渲染文本。当文本内容发生任何变化的时候,它会告知布局管理器(LayoutManager)。你们也许脑海中已经有了一点想法,那就是实现一个NSTextStorage的子类,以便实现当文本更新时动态改变文本属性的功能,Bingo!想法基本正确(在后文中你将会看到它的运用)。NSLayoutManager的功能是从NSTextStorage中获取文本并在屏幕上渲染文本。它将作为你应用中的布局引擎。NSTextContainer的功能是在屏幕上描绘一个几何图形区域,要显示的文本就是在这个区域内进行渲染的。每个TextContainer一般都和一和UITextView相关联。你可以实现一个NSTextContainer的子类,描绘一个复杂的几何图形,让文本在其中进行渲染。如果想要在我们的应用中实现动态文本格式的功能,那么就需要实现一个NSTextStorage的子类,然后根据用户键入的字符动态的设置文本格式属性。
我们会设计实现一个简单的输入法,可以输入 点 和 划,改变了键盘结构,删除字符然后隐藏自己。这个范例通过代码来生成的用户界面。当然,我们同样也可以使用Nib文件来生成界面-这个会在教程的末尾涉及。加载Nib文件会对性能有负面影响。创建一个工程打开Xcode6 ,创建一个“Single Page Application”,然后选择Swift为编程语言。添加一个文本区域打开Mainstoryboard然后拖拽一个文本区域从组件库里。我们会使用这个来测试我们设计的键盘。将这个文本区域居中,然后添加必要的contraints。Hint: 如果你调用 textFieldbecomeFirstResponder()在viewDidLoad ,这个键盘会自动在应用打开时d出。添加这个键盘扩展从导航器中选择这个项目文件,然后通过按+按钮添加一个新的target。选择Application Extension然后使用Custom Keyboard模板,命名它为MorseCodeKeyboard。这会创建一个名为 MorseCodeKeyboard 新文件夹,包括2个文件 KeyboardViewControllerswift 和 InfoPlist。接下来打开 KeyboardViewControllerswift,为了在不同的键盘之间进行切换,这个键盘模板文件会有一个按钮。在 viewDidLoad 方法中放入一个新的方法,命名为 addNextKeyboardButton。func addNextKeyboardButton() { selfnextKeyboardButton = UIButtonbuttonWithType(System) as UIButton var nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: selfnextKeyboardButton, attribute: Bottom, relatedBy: Equal, toItem: selfview, attribute: Bottom, multiplier: 10, constant: -100) selfviewaddConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint]) }为了更好的梳理代码的结构,创建一个新的方法名为addKeyboardButtons,然后在 viewDidLoad 中调用它。虽然这里只有几个按钮,但是在真实项目中,将会有更多的按钮。在 addKeyboardButtons 中调用 addNextKeyboardButton。class KeyboardViewController: UIInputViewController { override func viewDidLoad() { superviewDidLoad() addKeyboardButtons() } func addKeyboardButtons() { addNextKeyboardButton() } }点现在我们来添加点按钮,创建一个UIButton!类型的dotButton属性。class KeyboardViewController: UIInputViewController { var nextKeyboardButton: UIButton! var dotButton: UIButton! }增加一个名为 addDot 的方法。使用系统按钮来初始化名为 dotButton 的属性。增加一个 TouchUpInside 事件回调函数。设置一个大字体然后增加圆角,同时增加约束来限制它距离水平中心50个points,垂直居中。这个代码应该和下面nextKeyboardButton部分类似。func addDot() { // initialize the button dotButton = UIButtonbuttonWithType(System) as UIButton dotButtonsetTitle("", forState: Normal) dotButtonsizeToFit() dotButtonsetTranslatesAutoresizingMaskIntoConstraints(false) // adding a callback dotButtonaddTarget(self, action: "didTapDot", forControlEvents: TouchUpInside) // make the font bigger dotButtontitleLabelfont = UIFontsystemFontOfSize(32) // add rounded corners dotButtonbackgroundColor = UIColor(white: 09, alpha: 1) dotButtonlayercornerRadius = 5 viewaddSubview(dotButton) // makes the vertical centers equa; var dotCenterYConstraint = NSLayoutConstraint(item: dotButton, attribute: CenterY, relatedBy: Equal, toItem: view, attribute: CenterY, multiplier: 10, constant: 0) // set the button 50 points to the left (-) of the horizontal center var dotCenterXConstraint = NSLayoutConstraint(item: dotButton, attribute: CenterX, relatedBy: Equal, toItem: view, attribute: CenterX, multiplier: 10, constant: -50) viewaddConstraints([dotCenterXConstraint, dotCenterYConstraint]) }接下来对于dash,delete 和 hideKeyboard,这个过程都比较类似。这个deleteButton会从proxy使用deleteBackward方法,然后hideKeyboardButton会通过KeyboardViewController使用dismissKeyboard方法。划与dash相关的代码几乎和dotButton代码一致。为了将dashButton按钮在水平方向与 点 按钮对称,只要将水平约束中的常量改变一下符号即可。func addDash() { // set the button 50 points to the left (-) of the horizontal center var dotCenterXConstraint = NSLayoutConstraint(item: dotButton, attribute: CenterX, relatedBy: Equal, toItem: view, attribute: CenterX, multiplier: 10, constant: -50) viewaddConstraints([dashCenterXConstraint, dashCenterYConstraint])回删当被按下时,这个删除按钮会通过textDocumentProxy,使用deleteBackword删除字符。这个布局约束会和nextKeyboardButton对称(Left -> Right, Bottom->Top)func addDelete() { deleteButton = UIButtonbuttonWithType(System) as UIButton deleteButtonsetTitle(" Delete ", forState: Normal) deleteButtonsizeToFit() deleteButtonsetTranslatesAutoresizingMaskIntoConstraints(false) deleteButtonaddTarget(self, action: "didTapDelete", forControlEvents: TouchUpInside) deleteButtonbackgroundColor = UIColor(white: 09, alpha: 1) deleteButtonlayercornerRadius = 5 viewaddSubview(deleteButton) var rightSideConstraint = NSLayoutConstraint(item: deleteButton, attribute: Right, relatedBy: Equal, toItem: view, attribute: Right, multiplier: 10, constant: -100) var topConstraint = NSLayoutConstraint(item: deleteButton, attribute: Top, relatedBy: Equal, toItem: view, attribute: Top, multiplier: 10, constant: +100) viewaddConstraints([rightSideConstraint, topConstraint])隐藏键盘当被按下时,这个hideKeyboardButton会在KeyboardViewController上调用dismissKeyboard。func addHideKeyboardButton() { hideKeyboardButton = UIButtonbuttonWithType(System) as UIButton hideKeyboardButtonsetTitle("Hide Keyboard", forState: Normal) hideKeyboardButtonsizeToFit() hideKeyboardButtonsetTranslatesAutoresizingMaskIntoConstraints(false) hideKeyboardButtonaddTarget(self, action: "dismissKeyboard", forControlEvents: TouchUpInside) viewaddSubview(hideKeyboardButton) var rightSideConstraint = NSLayoutConstraint(item: hideKeyboardButton, attribute: Right, relatedBy: Equal, toItem: view, attribute: Right, multiplier: 10, constant: -100) var bottomConstraint = NSLayoutConstraint(item: hideKeyboardButton, attribute: Bottom, relatedBy: Equal, toItem: view, attribute: Bottom, multiplier: 10, constant: -100) viewaddConstraints([rightSideConstraint, bottomConstraint]) }使用Nib文件如果写约束并非你擅长的方式,你可以创建一个界面文件,然后将它添加到你的inputView。创建一个界面文件右击MorseCodeKeyboard文件组,然后选择创建新文件。选择User Interface,然后View Template,命名为CustomKeyboardInterface。
备注 :可通过更改UIAlertView的alertViewStyle属性来实现输入文字、密码甚至登录框的效果。
说明 :UIAlertViewDelegate协议拥有响应对话框视图的按钮动作的回调方法。还有当文本框内容改变时,调用alertViewShouldEnableOtherButton:方法可以让按钮动态地可用或者不可用。
备注 :按钮显示的次序取决于它们添加到对话框控制器上的次序,但并未成功。
注意 :使用UIAlertView只能在默认视图、文本框视图、密码框视图、登录和密码输入框视图中选择;但UIAlertController的使用更灵活,不必拘泥于内置样式,我们可向对话框中添加任意数目的UITextField对象,并可使用所有的UITextField特性。苹果官方现在并不提倡在iOS 8中使用UIAlertView,取而代之的是UIAlertController。
1、获取字符串中第一个字符
2、获取第二个字符
3、获取字符串中最后一个字符
三、插入 *** 作
1、使用 insert( , at:) 插入一个字符
2、使用 insert(contentsOf:, at:) 插入一个字符串
四、删除 *** 作
1、使用 remove(at: ) 移除某个字符
打印结果
2、移除一段特定的字符串
打印结果
以上就是关于swift怎么获取touch里的点全部的内容,包括:swift怎么获取touch里的点、如何用swift代码修改ios里text field 里注释文字的颜色深浅、如何在Swift中运用Text Kit框架等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)